3

I'm working with an API that is XML only and it's been ages since I've done anything with XML.

The response is below.

How do I get the value of <status>. I tried doing this:

XmlNodeList results = xmlDoc.GetElementsByTagName("ProcessRequestResult");

But I just end up with InnerText full of XML that I can't figure out how to parse correctly.

<?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>
<ProcessRequestResponse>
<ProcessRequestResult>
    <ConsumerAddEntryResponse>
          <Status>Failed</Status>
3
  • If all you want to do is get the value you should be able to use xmlDoc.Decendants("Status") in a foreach loop. Commented Apr 25, 2013 at 19:00
  • I don't see "descendants" as a method in XmlDocument. Commented Apr 25, 2013 at 19:21
  • Didn't realize you were using XmlDocument and not XDocument, disregard my previous comment. Commented Apr 25, 2013 at 19:37

3 Answers 3

4

How about using Linq To Xml?

var xDoc = XDocument.Parse(xml); //OR XDocument.Load(filename)
string status = xDoc.Descendants("Status").First().Value;

EDIT

The xml I used to test

string xml = @"<?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>
                <ProcessRequestResponse>
                    <ProcessRequestResult>
                        <ConsumerAddEntryResponse>
                                <Status>Failed</Status>
                        </ConsumerAddEntryResponse>
                    </ProcessRequestResult>
                </ProcessRequestResponse>
            </soap:Body>
            </soap:Envelope>";

EDIT 2

 string xml = @"<?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>
                <ProcessRequestResponse xmlns=""http://submission.api.domain/"">
                    <ProcessRequestResult>
                        <ConsumerAddEntryResponse>
                                <Status>Failed</Status>
                        </ConsumerAddEntryResponse>
                    </ProcessRequestResult>
                </ProcessRequestResponse>
            </soap:Body>
            </soap:Envelope>";

var xDoc = XDocument.Parse(xml);
XNamespace ns = "http://submission.api.domain/";
string status = xDoc.Descendants(ns + "Status").First().Value;
Sign up to request clarification or add additional context in comments.

10 Comments

This is what I would have done, Jack uses XmlDocument though.
@HjalmarZ because it's been ages since I've done anything with XML ;)
None of these options are working which is leading me to believe their XML being returned is wrong.
@JackMarchetti the xml you have posted misses many closing tags, I appended them to the xml and then run above code. It worked of course. See the updated answer.
@JackMarchetti I was expecting that question :) see the edit2
|
4

Load your xml with XmlDocument and fetch the desired node with XPath. (I did not test the line but it would look like this)

The document loading process :

var xmlDocument = new XmlDocument();
xmlDocument.Load("someXmlFile.xml");

The node(s) loading process :

//Single node would be :
XmlNode xNode = xmlDocument.SelectSingleNode("//ConsumerAddEntryResponse/Status");

//More than 1 node would be :
XmlNodeList xNodes = xmlDocument.SelectNodes("//ConsumerAddEntryResponse/Status");

7 Comments

Tried that. xnode is null.
@JackMarchetti I changed it. Try it like this
Still null. any other ideas?
Well it should not be. Have you checked if your xm l document is null first?
Yeah my XMLDoc is definitely not null. I think the XML they're sending back isn't properly formatted as if I do an XMLVisualize on the InnerXml from the xmldoc the "nodes" don't seem to line up.
|
0

Also, it's possible to use LINQ2XML

var s = "<ProcessRequestResponse.....";
var doc = XDocument.Parse(s);
string value = doc.Element("ProcessRequestResponse")
.Element("ProcessRequestResult")
.Element("ConsumerAddEntryResponse")
.Element("Status")
.Value;

2 Comments

But OP's xml start with soap:Envelope and soap:Body. Not with ProcessRequestResponse
It does not matter. it's just example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.