Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Error: There are multiple root elements

Tags:

c#

xml

I am getting XML from a web service. Here is what the XML looks like:

<parent>
    <child>
        Text
    </child>
</parent>
<parent>
    <child>
        <grandchild>
            Text
        </grandchild>
        <grandchild>
            Text
        </grandchild>
    </child>
    <child>
        Text
    </child>
</parent>

etc.

And here is my C# code:

StringBuilder output = new StringBuilder();

// Create an XmlReader
using (XmlReader reader = XmlReader.Create(new StringReader(xoResponse.@return)))
{
    XmlWriterSettings ws = new XmlWriterSettings();
    //ws.Indent = true;
    using (XmlWriter writer = XmlWriter.Create(output, ws))
    {
        // Parse the file and display each of the nodes.
        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element:
                    writer.WriteStartElement(reader.Name);
                    break;
                case XmlNodeType.Text:
                    writer.WriteString(reader.Value);
                    break;
                case XmlNodeType.XmlDeclaration:
                case XmlNodeType.ProcessingInstruction:
                    writer.WriteProcessingInstruction(reader.Name, reader.Value);
                    break;
                case XmlNodeType.Comment:
                    writer.WriteComment(reader.Value);
                    break;
                case XmlNodeType.EndElement:
                    writer.WriteFullEndElement();
                    break;
            }
        }
    }
}

I believe that the error is thrown on the second parent element. How can I avoid this error? Any help is greatly appreciated.enter image description here

like image 863
divided Avatar asked Feb 18 '11 15:02

divided


People also ask

Can we have multiple root elements in XML?

While a properly formed XML file can only have a single root element, an XSD or DTD file can contain multiple roots. If one of the roots matches that in the XML source file, that root element is used, otherwise you need to select one to use.

How many roots elements can we have in XML?

In an XML file, there can only be one root element. The root element must encapsulate all other elements--meaning, these other elements must show up after the opening root tag and before the closing root tag. Here is an example of an XML document with the root element "phonebook".

Does XML require a root element?

An XML document must have a single root element, which contains all other XML elements in the document.

What are root nodes in XML?

The root node is the parent of all other nodes in the document. An immediate descendant of another node. Note that element attributes are not generally considered child elements.


3 Answers

You can do it without modifying the XML stream: Tell the XmlReader to not be so picky. Setting the XmlReaderSettings.ConformanceLevel to ConformanceLevel.Fragment will let the parser ignore the fact that there is no root node.

        XmlReaderSettings settings = new XmlReaderSettings();         settings.ConformanceLevel = ConformanceLevel.Fragment;         using (XmlReader reader = XmlReader.Create(tr,settings))         {              ...         } 

Now you can parse something like this (which is an real time XML stream, where it is impossible to wrap with a node).

<event>   <timeStamp>1354902435238</timeStamp>   <eventId>7073822</eventId> </event> <data>   <time>1354902435341</time>   <payload type='80'>7d1300786a0000000bf9458b0518000000000000000000000000000000000c0c030306001b</payload> </data> <data>   <time>1354902435345</time>   <payload type='80'>fd1260780912ff3028fea5ffc0387d640fa550f40fbdf7afffe001fff8200fff00f0bf0e000042201421100224ff40312300111400004f000000e0c0fbd1e0000f10e0fccc2ff0000f0fe00f00f0eed00f11e10d010021420401</payload> </data> <data>   <time>1354902435347</time>   <payload type='80'>fd126078ad11fc4015fefdf5b042ff1010223500000000000000003007ff00f20e0f01000e0000dc0f01000f000000000000004f000000f104ff001000210f000013010000c6da000000680ffa807800200000000d00c0f0</payload> </data> 
like image 108
Mark Lakata Avatar answered Oct 05 '22 00:10

Mark Lakata


You need to enclose your <parent> elements in a surrounding element as XML Documents can have only one root node:

<parents> <!-- I've added this tag -->     <parent>         <child>             Text         </child>     </parent>     <parent>         <child>             <grandchild>                 Text             </grandchild>             <grandchild>                 Text             </grandchild>         </child>         <child>             Text         </child>     </parent> </parents> <!-- I've added this tag --> 

As you're receiving this markup from somewhere else, rather than generating it yourself, you may have to do this yourself by treating the response as a string and wrapping it with appropriate tags, prior to attempting to parse it as XML.

So, you've a couple of choices:

  1. Get the provider of the web service to return you actual XML that has one root node
  2. Pre-process the XML, as I've suggested above, to add a root node
  3. Pre-process the XML to split it into multiple chunks (i.e. one for each <parent> node) and process each as a distinct XML Document
like image 23
Rob Avatar answered Oct 05 '22 00:10

Rob


Wrap the xml in another element

<wrapper>
<parent>
    <child>
        Text
    </child>
</parent>
<parent>
    <child>
        <grandchild>
            Text
        </grandchild>
        <grandchild>
            Text
        </grandchild>
    </child>
    <child>
        Text
    </child>
</parent>
</wrapper>
like image 39
BenCr Avatar answered Oct 05 '22 00:10

BenCr