Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xml Serialization - Render list of objects directly under Root - Xml - Element

The following model class serializes to

    [XmlRoot]
    public class A
    {
        [XmlAttribute]
        public string Period { get; set; }

        public List<C> B { get; set; }

    }

<?xml version=1.0>
<A Period="Today">
 <B>
    <C>
    </C>
    <C>
    </C>
  </B>
</A>

Now, I dont want that <B> (List of objects should directly be listed under XmlRoot)

The resulting XML should look like,

<A Period="Today">
  <C>
  </C>
  <C>
  </C>
</A>

Any ideas how ?

like image 756
now he who must not be named. Avatar asked Feb 24 '26 13:02

now he who must not be named.


1 Answers

Set the list as an XML element. This will force the rendering of only its elements:

[XmlRoot]
public class A
{
    [XmlAttribute]
    public string Period { get; set; }

    [XmlElement("C")]
    public List<C> B { get; set; }

}
like image 54
Andrei V Avatar answered Feb 26 '26 05:02

Andrei V



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!