Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C将XPath与XmlDocument一起使用-无法选择命名空间中的节点(返回null)_C#_Xml_Xpath - Fatal编程技术网

C# C将XPath与XmlDocument一起使用-无法选择命名空间中的节点(返回null)

C# C将XPath与XmlDocument一起使用-无法选择命名空间中的节点(返回null),c#,xml,xpath,C#,Xml,Xpath,我正在尝试做一些应该很简单的事情,但我遇到了可怕的麻烦。我在StackOverflow中尝试了多个类似问题的代码,但都没有用。 我正试图从澳大利亚银行与澳大利亚政府的查询中获得各种信息。以下是匿名返回的XML值: <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www

我正在尝试做一些应该很简单的事情,但我遇到了可怕的麻烦。我在StackOverflow中尝试了多个类似问题的代码,但都没有用。 我正试图从澳大利亚银行与澳大利亚政府的查询中获得各种信息。以下是匿名返回的XML值:

    <?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <ABRSearchByABNResponse xmlns="http://abr.business.gov.au/ABRXMLSearch/">
            <ABRPayloadSearchResults>
                <request>
                    <identifierSearchRequest>
                        <authenticationGUID>00000000-0000-0000-0000-000000000000</authenticationGUID>
                        <identifierType>ABN</identifierType>
                        <identifierValue>00 000 000 000</identifierValue>
                        <history>N</history>
                    </identifierSearchRequest>
                </request>
                <response>
                    <usageStatement>The Registrar of the ABR monitors the quality of the information available on this website and updates the information regularly. However, neither the Registrar of the ABR nor the Commonwealth guarantee that the information available through this service (including search results) is accurate, up to date, complete or accept any liability arising from the use of or reliance upon this site.</usageStatement>
                    <dateRegisterLastUpdated>2017-01-01</dateRegisterLastUpdated>
                    <dateTimeRetrieved>2017-01-01T00:00:00.2016832+10:00</dateTimeRetrieved>
                    <businessEntity>
                        <recordLastUpdatedDate>2017-01-01</recordLastUpdatedDate>
                        <ABN>
                            <identifierValue>00000000000</identifierValue>
                            <isCurrentIndicator>Y</isCurrentIndicator>
                            <replacedFrom>0001-01-01</replacedFrom>
                        </ABN>
                        <entityStatus>
                            <entityStatusCode>Active</entityStatusCode>
                            <effectiveFrom>2017-01-01</effectiveFrom>
                            <effectiveTo>0001-01-01</effectiveTo>
                        </entityStatus>
                        <ASICNumber>000000000</ASICNumber>
                        <entityType>
                            <entityTypeCode>PRV</entityTypeCode>
                            <entityDescription>Australian Private Company</entityDescription>
                        </entityType>
                        <goodsAndServicesTax>
                            <effectiveFrom>2017-01-01</effectiveFrom>
                            <effectiveTo>0001-01-01</effectiveTo>
                        </goodsAndServicesTax>
                        <mainName>
                            <organisationName>COMPANY LTD</organisationName>
                            <effectiveFrom>2017-01-01</effectiveFrom>
                        </mainName>
                        <mainBusinessPhysicalAddress>
                            <stateCode>NSW</stateCode>
                            <postcode>0000</postcode>
                            <effectiveFrom>2017-01-01</effectiveFrom>
                            <effectiveTo>0001-01-01</effectiveTo>
                        </mainBusinessPhysicalAddress>
                    </businessEntity>
                </response>
            </ABRPayloadSearchResults>
        </ABRSearchByABNResponse>
    </soap:Body>
</soap:Envelope>
但它总是返回null,无论我在xpath中输入什么,我都会为节点返回null,并为列表计数0,无论我是否选择具有子节点的内容(值)。 我可以使用GetElementsByTagName获取节点,如示例中返回正确的节点,但我希望使用xpath“正确”选择正确的字段

我也尝试过使用XElement和Linq,但仍然没有成功。XML有什么奇怪的地方吗

我确信它一定很简单,但我已经挣扎了很久。

您需要调用DocumentElement上的SelectSingleNode和SelectNodes。您正在对文档本身调用它们

例如:

XmlNode xnode = xdoc.DocumentElement.SelectSingleNode("//response");

您没有处理文档中存在的名称空间。具体而言,高级别要素:

<ABRSearchByABNResponse xmlns="http://abr.business.gov.au/ABRXMLSearch/">
XDocument

最近,可以利用LINQ的强大功能,这使得使用名称空间更容易在任何深度找到子节点

var xdoc = XDocument.Parse(ipxml);
XNamespace soap = "http://schemas.xmlsoap.org/soap/envelope/";
XNamespace abr = "http://abr.business.gov.au/ABRXMLSearch/";

var xresponse = xdoc.Descendants(abr + "response");
var xlist = xdoc.Descendants(abr + "organisationName");
XDocument+XPath

您还可以使用Linq to Xml,特别是对于更复杂的表达式:

var xdoc = XDocument.Parse(ipxml);
var ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
ns.AddNamespace("abr", "http://abr.business.gov.au/ABRXMLSearch/");

var xresponse = xdoc.XPathSelectElement("//abr:response", ns);
var xlist = xdoc.XPathSelectElement("//abr:mainName/abr:organisationName", ns);

顺便说一下,我注意到您使用了“var”而不是特定类型。这是最佳实践还是您的偏好?我不做太多的编码,所以我会从那些做的人那里学到一些技巧。var是强类型隐式类型,几乎所有函数式语言都是这样做的。使用与否是关键之一。现在几乎所有的代码都使用var——您可以将鼠标悬停在IDE中的变量上以查看类型。这就把注意力转移到给变量一个像样的名字上,我还没有真正做到这一点——blushI几乎是我所有的代码。不想让你有偏见
var xdoc = XDocument.Parse(ipxml);
XNamespace soap = "http://schemas.xmlsoap.org/soap/envelope/";
XNamespace abr = "http://abr.business.gov.au/ABRXMLSearch/";

var xresponse = xdoc.Descendants(abr + "response");
var xlist = xdoc.Descendants(abr + "organisationName");
var xdoc = XDocument.Parse(ipxml);
var ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
ns.AddNamespace("abr", "http://abr.business.gov.au/ABRXMLSearch/");

var xresponse = xdoc.XPathSelectElement("//abr:response", ns);
var xlist = xdoc.XPathSelectElement("//abr:mainName/abr:organisationName", ns);