Walters
Walters

Reputation: 131

How to get SOAP element value

I have the following SOAP response and I want to get the value from the tag element. How can I achieve this using c#?

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Header>
    <SessionId xmlns="http://schemas.test.com/Practice/">10ac349210ac3492000000001565976562345</SessionId>
    <SequenceId xmlns="http://schemas.test.com/Practice/">9852523457481420</SequenceId>
  </S:Header>
  <S:Body>
    <Response xmlns="http://schemas.test.com/Practice/">
      <Attributes>
        <getResponseSubscription xmlns="http://schemas.test.com/test/UserProfile/Practice/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <ID>5643323</ID>
          <name>John Brown Jr</name>
          <address>New York</address>
        </getResponseSubscription>
      </Attributes>
    </Response>
  </S:Body>
</S:Envelope>

I tried the following but it returns null:

var str = XElement.Parse(xml.Response.XmlResponse.ToString()); 
var result = str.Element("Address").Value; 
NormalRoamingValue.Text = result.ToString();

Upvotes: 1

Views: 2603

Answers (2)

jdweng
jdweng

Reputation: 34421

Try following using Xml Linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string response = File.ReadAllText(FILENAME);

            XDocument doc = XDocument.Parse(response);

            List<XElement> getResponseSubscription = doc.Descendants().Where(x => x.Name.LocalName == "getResponseSubscription").ToList();
            XNamespace ns = getResponseSubscription.FirstOrDefault().GetDefaultNamespace();

            var results = getResponseSubscription.Select(x => new
            {
                id = (string)x.Descendants(ns + "ID").FirstOrDefault(),
                name = (string)x.Descendants(ns + "name").FirstOrDefault(),
                address = (string)x.Descendants(ns + "address").FirstOrDefault()
            }).FirstOrDefault();


        }
    }
}

Upvotes: 1

srk
srk

Reputation: 1901

I see a couple issues with your code:

  • The XML node is "address" but you are looking for "Address" (capital "A").
  • The XML node is defined with a namespace, so you need to use that namespace in your search.
  • The Element method looks for direct children to the current node - in this case, direct children to the root element.

Try this code instead:

XNamespace ns = "http://schemas.test.com/test/UserProfile/Practice/";
var result = str.Descendants(ns + "address").First();

// If you need a string result, use the .Value property:
var stringResult = result.Value;

Note that the Descendants method checks for any matching element under the current node, so it is not restricted to only looking at direct children. Also, the First call assumes that there will be exactly one "address" node in the XML. These assumptions may not work for you, but this code will point you in the right direction.

Upvotes: 2

Related Questions