将字符串转换为 xml 节点

发布于 2024-11-08 12:33:15 字数 232 浏览 7 评论 0原文

我有字符串,它包含从 PHP 文件返回的 xml 节点。 就像

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

现在我需要找出每个节点有什么值,即a,b,c...... 将此字符串加载到 xmlDocument 时,我收到类似“有多个根元素”的错误。

任何解决方案

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无敌元气妹 2024-11-15 12:33:15

格式良好的 XML 的基本规则之一是它具有单个根节点。在您的示例中,您有多个根:

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

为了使其格式良好,您必须使这些元素成为单个根的子元素:

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

格式不正确的 XML 文档根本不是真正的 XML 文档,您会发现任何 XML 解析器都无法读取它!

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!

寄离 2024-11-15 12:33:15

将其包装在根元素中。例如:

12...

1< b>2...

然后正常计算。

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.

公布 2024-11-15 12:33:15

这是因为每个元素都处于同一水平。需要有一个包含所有这些的“根”。将它们包装在任意节点中,例如 ...,然后将新字符串加载到 xmlDocument。

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.

苹果你个爱泡泡 2024-11-15 12:33:15

这看起来像 XML,但它不是有效的 XML。在 XML 中,您有一个包含所有其他元素的根元素。

因此,如果 str 中有该字符串,请执行以下操作:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文