我有以下XML
<DataTable xmlns = "http://schemas.datacontract.org/2004/07/System.Data">
<xs:schema
id = "NewDataSet"
xmlns = ""
xmlns:xs = "http://www.w3.org/2001/XMLSchema"
xmlns:msdata = "urn:schemas-microsoft-com:xml-msdata">
<xs:element
name = "NewDataSet"
msdata:IsDataSet = "true"
msdata:MainDataTable = "Table"
msdata:UseCurrentLocale = "true">
<xs:complexType>
<xs:choice minOccurs = "0" maxOccurs = "unbounded">
<xs:element name = "Table">
<xs:complexType>
<xs:sequence>
<xs:element
name = "Id"
type = "xs:int"
minOccurs = "0"/>
<xs:element
name = "Industry"
type = "xs:string"
minOccurs = "0"/>
<xs:element
name = "ParentId"
type = "xs:int"
minOccurs = "0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata = "urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr = "urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns = "">
<Table diffgr:id = "Table1" msdata:rowOrder = "0">
<Id>1592</Id>
<Industry>Fortune 100</Industry>
<ParentId>1810</ParentId>
</Table>
<Table diffgr:id = "Table2" msdata:rowOrder = "1">
<Id>1601</Id>
<Industry>Oil & Gas Operations</Industry>
<ParentId>0</ParentId>
</Table>
</NewDataSet>
</diffgr:diffgram>
</DataTable>
我需要parentID = 0
的节点但是当我使用以下代码时:
Dim parentNodes As XmlNodeList = xml_node.SelectNodes("/DataTable/diffgr:diffgram/NewDataSet/Table[ParentId=0]")
我收到此错误
需要命名空间管理器或XsltContext。此查询具有前缀,变量或用户定义的函数。
请建议解决方案
答案 0 :(得分:4)
您需要在SelectNodes
之前添加以下内容XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(xmldoc.NameTable);
xmlnsManager.AddNamespace("bk", "urn:Books")
xmlnsManager.AddNamespace("pub", "urn:Publisher")
答案 1 :(得分:1)
对于显示的示例,它应该是:
XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(xmldoc.NameTable);
xmlnsManager.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema")
xmlnsManager.AddNamespace("msdata", "urn:schemas-microsoft-com:xml-msdata")
答案 2 :(得分:1)
您还可以避免使用XmlNamespaceManager。 GetElementsByTagName可以遍历该文档,但需要更多关注,您应该已经了解了xmlDocument的结构:
var nodes = document.GetElementsByTagName("xsl:import");
var href = nodes[0].Attributes["href"];//be sure which node to take