问题描述:

I am unable to get the value.

How to get the StatusDescription value? Please tell me.

<WebServiceResponse xmlns="ws.abc.com">

<SubscriptionInfo xmlns="ws.abc.com">

<LicenseStatusCode>0</LicenseStatusCode>

<Amount>0</Amount>

</SubscriptionInfo>

<VerificationResponse xmlns="www.abc.com">

<VerificationResult>

<ServiceStatus>

<StatusNbr>304</StatusNbr>

<StatusDescription>Address Not Found</StatusDescription>

</ServiceStatus>

<ServiceResult />

</VerificationResult>

</VerificationResponse>

</WebServiceResponse>

网友答案:

Your xml contains namepace and using XDocument you can get desired node value following way:

 XDocument doc = XDocument.Parse(xml);

 XNamespace xmlNamespace = "www.abc.com";
 var ResultNode = from a in doc.Descendants(xmlNamespace + "VerificationResponse").Descendants(xmlNamespace + "StatusDescription")
                  select a.Value;

WORKING DOTNET FIDDLE

网友答案:

You can use something like this (using Linq-to-XML)

// define the XML namespace of the <VerificationResponse> node and down
XNamespace wwwabc = "www.abc.com";

// parse your XML into a XDocument
XDocument xdoc = XDocument.Parse(your-xml-input);

// get the <StatusDescription> node(s) from the XDocument
// here I'm assuming there's only one such node, so I'm using
// the .FirstOrDefault() method to fetch it
var statusDescriptionNode = xdoc.Descendants(wwwabc + "StatusDescription").FirstOrDefault();

// if we found a first such node
if (statusDescriptionNode != null)
{
    string statusDescription = statusDescriptionNode.Value;
}

Of course, if this is a really large and complex XML, then the .Descendants() call might not be very efficient (it just searches the entire XML document for nodes of that namespace + node name).

相关阅读:
Top