1

i have the string, it contains xml nodes, returned from the PHP file. It's like

<a>1</a><b>0</b><c>4</c>..............

Now i need to find out what value each node have i.e a, b, c...... while loading this string to xmlDocument i'm getting error like "There are multiple root elements".

any solution for this

4 Answers 4

5

One of the basic rules for well-formed XML that it has a single root node. With your example, you have multiple roots:

<a>1</a>
<b>0</b>
<c>4</c>

To make it well-formed you will have to make these elements a child of a single root:

<root>
    <a>1</a>
    <b>0</b>
    <c>4</c>
</root>

An XML document that is not well-formed is not really an XML document at all and you will find that no XML parser will be able to read it!

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

Comments

3

Wrap it in a root element. E.g:

From <a>1</a><b>2</b>...

To <root><a>1</a><b>2</b>...</root>

Then compute as normal.

Comments

2

That is because each element is at the same level. There need to be a "root" that encloses all of them. Wrap them in a arbitrary node, say <root>...</root> then load the new string to the xmlDocument.

Comments

2

This seems like XML, but it's not valid XML. In XML, you have a root element that wraps all of the other elements.

So, if you have that string in str, do this:

str = String.Format("<root>{0}</root>", str);

Comments

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.