1

I'm serializing a class that has a list of subclasses. How can I exclude the collection nodes from the xml? An example will illustruat my point.

public class A
{
   public List<Person> People;
}

public class Person
{
  [XmlAttribute]
  public string name;
}

This serializes as:

<A>
   <People>
     <Person name = "a"/>;
     <Person name = "b"/>;
     <Person name = "c"/>;
   </People>
</A>

Now this seems logically correct, however the xml I am trying to replicate looks like this:

<A>
     <Person name = "a"/>;
     <Person name = "b"/>;
     <Person name = "c"/>;
</A>

How can this be achieved?

Many thanks

  • Jason, is there a reason you can't use the default XML serialization? You generally want common elements grouped under a root element to make it easy to parse and also to serialize back and forth. There is probably a way to reach the result you want via serialization, or you could just write out the XML string or create the XML using the XMLSerializer, XDocument, and XmlWriter: http://stackoverflow.com/questions/284324/what-is-the-best-way-to-build-xml-in-c-sharp-code – Matt Mombrea Oct 07 '12 at 14:54
  • Hi Matt, the XML is to be send as a parameter to a webservice and will fail XSD validation if its not exactly like their examples. Its a pretty complex set of classes and I was hoping to avoid building up the XML manually. Is there not some kind of attribute available to me that lets me control the output? – Jason Davidson Oct 07 '12 at 15:19
  • If all else fails I think I will just replace the nodes I dont want with an empty string before passing the xml string through to the webservice - bit of a hack - but thats how I roll :) – Jason Davidson Oct 07 '12 at 15:40

0 Answers0