在 VB.NET 中读取 XML 节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21371229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading XML Node in VB.NET
提问by George Vaisey
Using VB.NET within VS2008.
在 VS2008 中使用 VB.NET。
Sample XML FIle
示例 XML 文件
<MailListExportXML xsi:schemaLocation="com.efi.planner.mailing.export MonarchMailListExport.xsd" xmlns="com.efi.planner.mailing.export" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MailListFiles>
<MailListFile MailListID="1" MailListName="Test Mail File 11-14-2013.csv" MailListDateTimeStamp="2013-12-04T19:18:00Z" MailListFileLocationPath="c:ImportedMailFiles" StatedFirstRecord="George Vaisey" StatedLastRecord="Mary Smith" TotalMailListRecordCount="1992"/></MailListFiles>
</MailListExportXML>
I am struggling to get value from MailListName to populate to a text box.
我正在努力从 MailListName 获取值以填充到文本框。
回答by mikea80
You can try something like this:
你可以尝试这样的事情:
Dim xmlDoc As XmlDocument = New XmlDocument()
xmlDoc.Load(PATH TO XML FILE)
Dim docNode As XmlNode = xmlDoc.SelectSingleNode("MailListExportXML/MailListFiles/MailListFile[@MailListID=1]")
SOMETEXTBOX.Text = docNode.Attributes.GetNamedItem("MailListName").Value
xmlDoc = Nothing
based on you error, make sure the xmlDoc.Load(... IS THE PATH AND NAME OF THE ACTUAL XML DOCUMENT. below is a MapPath example but you can use the absolute path like "c:\inetpub\wwwroot\webapp\App_Data\Mailing.xml"
根据您的错误,请确保xmlDoc.Load(... 是实际 XML 文档的路径和名称。下面是一个 MapPath 示例,但您可以使用绝对路径,如“c:\inetpub\wwwroot\webapp\App_Data \Mailing.xml"
Dim xml_doc As XmlDataDocument = New XmlDataDocument()
xml_doc.Load(Server.MapPath("~/App_Data/NAME AND PATH OF XML FILE.xml"))
Dim docNode As XmlNode = xml_doc.SelectSingleNode("MailListExportXML/MailListFiles/MailListFile[@MailList??ID=1]")
tbMMInputFile.Text = docNode.Attributes.GetNamedItem("MailListName").Value
xmlDoc = Nothing
回答by har07
You have a default namespace, so you'll need to specify the namespace in XPathquery using XmlNamespaceManager:
您有一个默认命名空间,因此您需要XPath使用XmlNamespaceManager以下命令在查询中指定命名空间:
Dim doc As XmlDocument = New XmlDocument()
doc.Load("path_to_xml_file.xml")
Dim namespaceManager As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
namespaceManager.AddNamespace("d", doc.DocumentElement.NamespaceURI)
Dim node As XmlNode = doc.SelectSingleNode("d:MailListExportXML/d:MailListFiles/d:MailListFile[@MailListID=1]")
MyTextBox.Text = node.Attributes.GetNamedItem("MailListName").Value
Reference : [SO Post about XPath query to select nodes having default namespace]
参考:[关于 XPath 查询以选择具有默认命名空间的节点的 SO 帖子]
UPDATE :
更新 :
My bad, I missed one thing, in previous snippet namespaceManager was not used. So as confirmed by OP, this line in previous snippet :
我的错,我错过了一件事,在之前的代码片段 namespaceManager 中没有使用。因此,正如 OP 所确认的那样,前一个片段中的这一行:
Dim node As XmlNode = doc.SelectSingleNode("d:MailListExportXML/d:MailListFiles/d:MailListFile[@MailListID=1]")
should be like this instead (note namespaceManageras second parameter of SelectSingleNodefunction) :
应该是这样的(注意namespaceManager作为SelectSingleNode函数的第二个参数):
Dim node As XmlNode = doc.SelectSingleNode("d:MailListExportXML/d:MailListFiles/d:MailListFile[@MailListID=1]", namespaceManager)
回答by George Vaisey
Using the example with the "namespacemanager" resolved the problems.
使用带有“namespacemanager”的示例解决了问题。

