帮助选择,XML和c#

本文关键字:XML 选择 帮助 | 更新日期: 2023-09-27 17:53:29

我有以下SOAP响应:

<?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>
        <identifierType>ABN</identifierType>
        <identifierValue>79 142 357 604</identifierValue>
    </identifierSearchRequest>
</request>
<response>
    <dateRegisterLastUpdated>2011-04-26</dateRegisterLastUpdated>
    <dateTimeRetrieved>2011-04-26T14:10:17.8169921+10:00</dateTimeRetrieved>
    <businessEntity>
        <recordLastUpdatedDate>2010-03-05</recordLastUpdatedDate>
        <ABN>
            <identifierValue>79142357604</identifierValue>
            <isCurrentIndicator>Y</isCurrentIndicator>
            <replacedIdentifierValue xsi:nil="true" />
            <replacedFrom>0001-01-01</replacedFrom>
        </ABN>
        <entityStatus>
            <effectiveTo>0001-01-01</effectiveTo>
        </entityStatus>         
        <entityType>
            <entityTypeCode>PUB</entityTypeCode>
        </entityType>
        <mainBusinessPhysicalAddress>
            <stateCode>NSW</stateCode>
            <postcode>2000</postcode>
        </mainBusinessPhysicalAddress>
        </businessEntity>
</response>
</ABRPayloadSearchResults>
</ABRSearchByABNResponse>
</soap:Body>
</soap:Envelope>

我想要得到的是entityTypeCode,但我没有成功。我试过

XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(searchPayload);

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xDoc.NameTable);
nsmgr.AddNamespace("c", "http://abr.business.gov.au/ABRXMLSearch/");
XmlNode entityTypeCode = xDoc.SelectSingleNode("//entityTypeCode", nsmgr);

和各种xpath表达式,但如果xmlnode entityTypeCode始终为空。

建议吗?

帮助选择,XML和c#

使用XmlNode entityTypeCode = xDoc.SelectSingleNode("//c:entityTypeCode", nsmgr);,因为您已经在名称空间管理器中添加了元素的名称空间作为c前缀。