1

I'm trying to get "cust_name" and "code" nodes from a web API XML response below.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cust_list xmlns="http://example.com">
    <cust>
        <cust_id>1234</cust_id>
        <cust_name>abcd</cust_name>
        <cust_type>
            <code>2006</code>
        </cust_type>
    </cust>
</cust_list>

I'm writing the response as string to XMLDocument and trying to read from it. Below is my code

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://serviceURI");
request.Method = "GET";
request.ContentType = "Application/XML";

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

using (var reader = new StreamReader(response.GetResponseStream()))
{
    string responseValue = reader.ReadToEnd();
    var doc = new XmlDocument();
    doc.LoadXml(responseValue);

    string node = doc.SelectSingleNode("/cust_list/cust/cust_name").InnerText;
    string node2 = doc.SelectSingleNode("/cust_list/cust/cust_type/code").InnerText;
}

I'm trying to target specific nodes but getting "object reference not set to an instance of an object" error. what am i doing wrong here?

11
  • 1
    This is almost certainly due to the namespace part. Any reason you don't want to use LINQ to XML, which makes namespace-handling rather simpler? Commented Apr 25, 2017 at 15:07
  • Here's the answer: stackoverflow.com/a/4171468/126995 Commented Apr 25, 2017 at 15:18
  • Possible duplicate of XmlDocument.SelectSingleNode and xmlNamespace issue Commented Apr 25, 2017 at 15:19
  • @Jon Skeet this is part of a big application and linq was not used anywhere in the application. isn't there anyway i can get the values of specific nodes by Xpath? Commented Apr 25, 2017 at 15:22
  • @Soonts will the namespace be same all the time? because the service is not managed by us. we are consuming it from a different resource. Commented Apr 25, 2017 at 15:24

1 Answer 1

2
XElement xml = XElement.Parse(xmlString);
XNamespace ns = (string)xml.Attribute("xmlns");
var customers = xml.Elements(ns + "cust")
    .Select(c => new
    {
        name = (string)c.Element(ns + "cust_name"),
        code = (int)c.Element(ns + "cust_type")
            .Element(ns + "code")
    });

In this example an XElement is parsed from the input string.

A Namespace is also created using the attribute xmlns. Note how this is used when selecting elements.

All cust elements in the root element are selected and projected into a new anonymous type that currently declares a string name and an int code (you can extend this as needed).

So for example, to get the name of the first customer you could do the following:

string name = customers.First().name;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer! that worked. but i had to add "First().value" to the end of "string name = customers.First().name.First().Value" for it return actual innerXML value other wise it is returning some object path.

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.