Get value from xml element in c#

前端 未结 2 1251
误落风尘
误落风尘 2021-01-29 02:13

I am trying to get Absoluteentry tag\'s value from the below xml string, but its displaying objectrefrence not set exception

 
<         


        
相关标签:
2条回答
  • 2021-01-29 02:50

    Look at the XML:

    <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
    ...
    

    That's the Envelope element in the namespace with URI "http://www.w3.org/2003/05/soap-envelope".

    Now look at your code:

    doc.Element("Envelope")...
    

    That's looking for an Envelope element that's not in any namespace. You should specify the namespace - and the namespaces of the other elements you're looking for:

    XNamespace env = "http://www.w3.org/2003/05/soap-envelope";
    XNamespace responseNs = "http://www.sap.com/SBO/DIS";
    XDocument doc = XDocument.Parse(xmlstring);
    var result = doc.Element(env + "Envelope")
        .Element(env + "Body")
        .Element(responseNs + "AddResponse")
        .Element(responseNs + "PickListParams")
        .Element(responseNs + "Absoluteentry").Value;
    
    0 讨论(0)
  • 2021-01-29 02:53

    You can use Descendants too. Descendants finds children at any level.

    var result = doc.Element(env + "Envelope")
        .Element(env + "Body")
        .Descendants(responseNs + "Absoluteentry").Value;
    
    0 讨论(0)
提交回复
热议问题