需要xpath表达式以遍历xmldocument

需要xpath表达式以遍历xmldocument

试图解析这个:

<?xml version="1.0" encoding="UTF-8"?>
<directoryresponse xmlns="https://www.sisow.nl/Sisow/REST" version="1.0.0">
    <directory>
        <issuer>
            <issuerid>01</issuerid>
            <issuername>ABN Amro Bank</issuername>
        </issuer>
        <issuer>
            <issuerid>02</issuerid>
            <issuername>ASN Bank</issuername>
        </issuer>
        <issuer>
            <issuerid>04</issuerid>
            <issuername>Friesland Bank</issuername>
        </issuer>
        <issuer>
            <issuerid>05</issuerid>
            <issuername>ING</issuername>
        </issuer>
        <issuer>
            <issuerid>06</issuerid>
            <issuername>Rabobank</issuername>
        </issuer>
        <issuer>
            <issuerid>07</issuerid>
            <issuername>SNS Bank</issuername>
        </issuer>
        <issuer>
            <issuerid>08</issuerid>
            <issuername>RegioBank</issuername>
        </issuer>
        <issuer>
            <issuerid>09</issuerid>
            <issuername>Triodos Bank</issuername>
        </issuer>
        <issuer>
            <issuerid>10</issuerid>
            <issuername>Van Lanschot Bankiers</issuername>
        </issuer>
    </directory>
</directoryresponse>


这是我的代码:

XPath = "//directoryresponse/directory/issuer/issuerid"
Dim nodeList As XmlNodeList = XML.SelectNodes(XPath)


但是nodelist.count = 0 ...为什么?

最佳答案

您的XML具有默认名称空间,因此您的代码应为:
C#:

var doc = new XmlDocument();
doc.Load("a.xml");

XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace("ns", "https://www.sisow.nl/Sisow/REST");

var result = doc.SelectNodes("//ns:issuer/ns:issuerid", mgr);

foreach (XmlElement item in result)
{
    Console.WriteLine(item.InnerText);
}


VB.NET:

Dim doc As New XmlDocument
doc.Load("a.xml")

Dim mgr As New XmlNamespaceManager(doc.NameTable)
mgr.AddNamespace("ns", "https://www.sisow.nl/Sisow/REST")

Dim result As XmlNodeList = doc.SelectNodes("//ns:issuer/ns:issuerid", mgr)

Dim item As XmlElement
For Each item In result
    Console.WriteLine(item.InnerText)
Next

关于c# - 需要xpath表达式以遍历xmldocument,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9349665/

10-13 09:29