简体   繁体   中英

Xml serialization/deserialization with unnumbered attributes

Although the Title might not make sense (I had a hard time describing this problem to shortly) I will try to explain here.

I have some XML that contains some values that will always be there, but it also contains an undefined number of other variables, Such as this:

<fooElements>
    <fooElement>
        <name>fooElem1</name>
        <barElement>
            <name>foobarElem1</name>
        </barElement> //There's an undefined number of these bar elements.
    </fooElement>
</fooElements>

Is there anyway process those barElements with the fooElement class?

My first though was to place a list inside of the fooElement class and then load the barElement (s) into that, but when i then serialize it back i get:

<fooElements>
    <fooElement>
        <name>fooElem1</name>
        <barList>
            <barElement>
                <name>foobarElem1</name>
            </barElement> //There's an undefined number of these bar elements.
        <barList>
    </fooElement>
</fooElements>

Which is not what i am looking for.

Is there any way i can store this undefined number of barElements inside the fooElement class without it showing in the xml?

Just make your barList an XmlElement . For example:

[XmlElement("barElement")]
public List<BarElementObject> BarElements { get; set; }

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