开发者

Converting string to xml nodes

开发者 https://www.devze.com 2023-03-07 10:38 出处:网络
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>..............

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...... whil开发者_如何学Ce loading this string to xmlDocument i'm getting error like "There are multiple root elements".

any solution for this


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!


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.


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.


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号