3

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 ?

1 Answer 1

5

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; }

}
Sign up to request clarification or add additional context in comments.

4 Comments

This still gives me C node under B node. I don't want B node at all.
@nowhewhomustnotbenamed., I read your question wrong the first time. Updated.
Just curious: Can you tell me from where did you read or know this ? Any references to further dig in to this info please..
@nowhewhomustnotbenamed., I've recently had the same problem, that's why I remembered it. I've searched for question that solved the problem: stackoverflow.com/questions/2006482/…. Check out the attributes listed here: msdn.microsoft.com/en-us/library/…. It might come in handy next time. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.