本文介绍了有多个根元素加载XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sRecieved = "<XmlClient>2.0</XmlClient><XmlVersion>3.0</XmlVersion>"
Dim xml As New XmlDocument();
xml.LoadXml(sRecieved);

有多个根元素.....我想xmlclient价值和xmlversion值

There are multiple root elements .....i want xmlclient value and xmlversion value

推荐答案

恩,是的,你的数据的不是的有效的XML文档。 (该错误信息是很清楚的 - 你有多个顶级元素。)你可以的的它加上空根元素的有效文件:

Well yes, your data isn't a valid XML document. (The error message is pretty clear - you've got multiple top-level elements.) You could make it a valid document by adding a dummy root element:

xml.LoadXml("<root>" & sReceived & "</root>")

......但如果​​你改变任何的发送数据的机会,它会更好,如果它发送一个实际的XML文档。

... but if you get the chance to change whatever's sending the data, it would be better if it sent an actual XML document.

编辑:如果你能使用LINQ到XML,而不是的XmlDocument ,将客户端编号和版本号很容易。例如,文本:

If you're able to use LINQ to XML instead of XmlDocument, getting the client number and the version number are easy. For example, as text:

Dim clientVersion = doc.Root.Element("XmlClient").Value
Dim xmlVersion = doc.Root.Element("XmlVersion").Value

编辑:好吧,如果你'重卡与的XmlDocument ,我相信你可以使用:

Okay, if you're stuck with XmlDocument, I believe you could use:

Dim clientVersionNode = doc.DocumentElement.GetElementsByTagName("XmlClient")(0)
Dim clientVersion = (CType(clientVersionNode, XmlElement)).InnerText

(且同样 xmlVersion

这篇关于有多个根元素加载XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:14