1

我有以下 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 &amp; 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。此查询具有前缀、变量或用户定义的函数。

请提出解决方案

4

3 回答 3

5

您需要在 SelectNodes 之前添加以下内容

XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(xmldoc.NameTable);
xmlnsManager.AddNamespace("bk", "urn:Books")
xmlnsManager.AddNamespace("pub", "urn:Publisher")
于 2012-10-18T05:10:57.613 回答
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")
于 2013-12-12T17:18:29.143 回答
1

您也可以避免使用 XmlNamespaceManager。该文档可以通过 GetElementsByTagName 遍历,但需要更多注意,您应该已经了解 xmlDocument 的结构:

var nodes = document.GetElementsByTagName("xsl:import");
var href =  nodes[0].Attributes["href"];//be sure which node to take
于 2017-09-22T20:53:52.677 回答