1

I receive this XML from an external source:

<epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
  <response>
    <result code="1300">
      <msg>Command completed successfully; no messages</msg>
    </result>
    <trID>
      <clTRID>TEST-12345</clTRID>
      <svTRID>IRNIC_2011-09-21T13:44:25+04:30_f1u</svTRID>
    </trID>
  </response>
</epp>

I want to extract the value of the code attribute of the result element. I used this code:

XDocument doc = XDocument.Parse(response);
XNamespace ns = "urn:ietf:params:xml:ns:epp-1.0";
string value = doc.Descendants(ns + "result").First().Attribute("code").Value;

However, it throws null value exception, because doc.Descendants(ns + "result") is null.

What's wrong here?

6
  • 1
    Descendants("urn:ietf:params:xml:ns:epp-1.0result")? Is that right? Commented Sep 21, 2011 at 9:21
  • 1
    Looks fine to me - have you inspected'doc' with the debugger to verify that it is the XML document you have included above? Commented Sep 21, 2011 at 9:21
  • @ColinE, yeah, it's valid and everything is as I've posted here. Commented Sep 21, 2011 at 9:22
  • 3
    I have just put this in a unit test and it works fine for me! Commented Sep 21, 2011 at 9:26
  • 2
    @bzlm - the first part is a namespace, not a string - XNamespace. The result element is in the namespace, so this is correct. Commented Sep 21, 2011 at 9:30

4 Answers 4

3

However, it throws null value exception, because doc.Descendants(ns + "result") is null

I would not expect doc.Descendants to ever return a null - it might return an empty sequence, but that wouldn't be a "null value exception". Likewise, First() - it either returns something, or it throws an exception (and while that "something" can be a null, I do not expect Descendants would be yielding nulls). So that leaves .Attribute("code").Value. Now yes; .Attribute("code") can return a null, if an attribute doesn't exist. If you are happy to obtain a null in those cases, I recommend:

string value = (string)doc.Descendants(ns + "result").First().Attribute("code");

the conversion operator handles null (non-existent) attributes.

Sign up to request clarification or add additional context in comments.

Comments

1

No repro. Your code is basically OK.

So: Debug. Break the statement up and check things.

  • is your doc loaded OK ?
  • what does just doc.Descendants(ns + "result").First() deliver?
  • etc

Comments

1

Check response variable because i loaded the xml from an xml file and your code worked great.

In case of testing, you can save the response in an xml file and load it from there.

use XDocument.Load("a.xml") to load an xml file.

Comments

1

Tested this too. Easiest way to test is to download LinqPad, create new query, then for Language select "C# statement(s)" from the dropdown. Click Run, should work with the code provided. I would also check the response variable as suggested by "Rased Dot Net"

string xml =
@"<epp xmlns=""urn:ietf:params:xml:ns:epp-1.0"">
  <response>
    <result code=""1300"">
      <msg>Command completed successfully; no messages</msg>
    </result>
    <trID>
      <clTRID>TEST-12345</clTRID>
      <svTRID>IRNIC_2011-09-21T13:44:25+04:30_f1u</svTRID>
    </trID>
  </response>
</epp>";

XDocument doc = XDocument.Parse(xml);
Console.WriteLine(doc.ToString());
XNamespace ns = doc.Root.Name.Namespace;
Console.WriteLine("Namespace: " + ns.ToString());
string value = doc.Descendants(ns + "result").First().Attribute("code").Value;
Console.WriteLine(value);

Comments

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.