Question

I have an XML string which has the following structure:

<Element>
   <Property1>Something</Propert1>
   <Property2>SomethingElse</Property2>
</Element>
<Element>
   <Property1>Something2</Propert1>
   <Property2>SomethingElse2</Property2>
</Element>

I would like to serialize this to a List<Element>. I use this code:

XmlSerializer xd = new XmlSerializer(typeof(T));

XDocument xdoc = XDocument.Parse(xmlStringToDesirialize);
T deserializedObject = xd.Deserialize(xdoc.CreateReader()) as T;

Where T is List<Element>. I get an exception saying There are multiple root elements. I understand why this is, but I`m not sure what to do about it.

I was thinking that adding a psudo-root element, like <Elements> might be a good solution, but I don't know how I would go about adding it to the XML document I already have. Or maybe there is an alternative solution altogether.

EDIT: For completeness I am adding code for the full solution I needed for deserialization, in case anyone needs it.

I created a class:

[XmlRoot("myRoot", Namespace = "")]
public class MyRoot
{
   [XmlElement("Element", Namespace = "{The xmlns of the actual class}")]
   public List<Element> Elements {get; set;}

   public MyRoot()
   {
      Elements = new List<Element>();
   }
}

Then I deserialize to this class after adding the tags as suggested by @Richard. Hope this can help someone.

Was it helpful?

Solution

I have an XML string

No you don't. You have something that can be refered to as an "XML Fragment". Specifically because there is not a single root element it is not an XML document. (There is no such thing as "Invalid XML": it is either valid or it is not XML).

XML Parsers require an XML document. But XmlSeriailsie is not just a parse: it includes an XML parser (of course) but also wants to generate an object graph from the content of the XML document making lots of assumptions about type availability and restrictions on the XML to match those types

The easiest approach with normal XML parsers would be to add a root element yourself. eg.:

var xdoc = XDocument.Parse("<myRoot>" + theString + "</myRoot>");

however for XML Deserialisation you will need to modify your available types to include a container that serialises with a myRoot element and then contains the relevant information.

However given the sample XML I see no sign of that looking like an object graph. Why not work with the parsed XML and extract the content using the parser's API?

OTHER TIPS

XML must have 1! root element. So as you said before making <elements> as root node is solution. And XML would look like:

<Elements>    
    <Element>
       <Property1>Something</Propert1>
       <Property2>SomethingElse</Property2>
    </Element>
    <Element>
       <Property1>Something2</Propert1>
       <Property2>SomethingElse2</Property2>
    </Element>
</Elements>

why not create an element then append yours to it

XElement root = new XElement("root");

then append your elements to it

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top