OseeAliz
OseeAliz

Reputation: 69

How to get the Element value from returned Soap based XML Message

I am to get the element value of Soap based retuned XML as below.

XML File:

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <SendToDSSResponse xmlns="http://tempuri.org/">
      <SendToDSSResult>
        <RC>0</RC>
        <RCD></RCD>
        <PCKT>
          <IDNO>1212</IDNO>
          <IDTYPE>051</IDTYPE>
          <NOBOX>121216</NOBOX>
          <NAME>James</NAME>
       </PCKT>
      </SendToDSSResult>
    </SendToDSSResponse>
  </soap:Body>
</soap:Envelope>

Now I want to get the values of IDNO, NoBox and Name. I am trying to use the following code below to get the values but it throws an Exception. What's the correct way to get the element values?

C# Code:

var xDoc = XDocument.Parse(cleanXml); //OR XDocument.Load(filename)      
string Name = xDoc.Descendants("Name").First().Value;

Upvotes: 3

Views: 3844

Answers (2)

user3221822
user3221822

Reputation:

I think you should add XNamespace and then you can read out the specific value from the nodes or tags under the node, try this demo in your ConsoleApplication:

XDocument doc = XDocument.Load("XMLFile1.xml");
var result = doc.Descendants(XNamespace.Get("http://tempuri.org/")+"NAME").First();
Console.WriteLine(result.Value);

Upvotes: 4

AgentFire
AgentFire

Reputation: 9770

Use Root property.

string name = xDoc.Root.Descendants("NAME").First().Value;

Upvotes: 0

Related Questions