简体   繁体   中英

Deserialize XML CData with attribute

I have an xml file and I try to write a type for it. At a certain point my brain freezes.

This xml is as minimal as I can write it.

<Level ID="SomeID">
    <Selection Name="AnotherID">
        <Content><![CDATA[SomeData]]></Content>
    </Selection>
</Level>

In cs I want to write a class as a type for the xmlserializer.

public class Level
{
    [XmlAttribute]
    public string ID {get; set;}
    public ??? Selection {get; set;}
    //What is the type of CDATA
    //Where would the Name Attribute go?
}

Somehow the Selection has to be a class with an attribute and also the type of Selection is CData. Whatever CData is it would be a standard type and so I could not set the Name Attribute.

How do I resolve this in the cs class? - the xml is legacy and can not be changed now.

You have a good start there.. this should help you get the rest of the way.

public class Level
{
    [XmlAttribute]
    public string ID {get; set;}
    public Selection Selection {get; set;}
}

public class Selection {
    [XmlAttribute]
    public string Name {get;set;}
    public Content Content {get;set;}
}

public class Content {
    [XmlText]
    public string Data {get;set;}
}

So to access that CDATA text via your object model you would access Level.Selection.Content.Data .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM