Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Xml Value always getting null

Tags:

c#

parsing

xml

I have the following Xml in my Resources.xmltest:

<?xml version="1.0" encoding="utf-8" ?>   
<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://DFISofft.com/SmartPayments/">
    <Result>0</Result>
    <Message>Pending</Message>
    <PNRef>222131</PNRef>
    <ExtData>InvNum=123</ExtData>   
 </Response>

I've tried several ways to get the values, Result,Message,PNRef,ExtData, out of it and I've had no luck. I always get a null value for the NodePath so it never goes into the loop:

  var XmlDoc = new XmlDocument();

  XmlDoc.LoadXml(Resources.xmltest);
  XmlElement NodePath = (XmlElement) XmlDoc.SelectSingleNode("/Response");

  while (NodePath != null)
  {
    foreach (XmlNode Xml_Node in NodePath)
    {
      Console.WriteLine(Xml_Node.Name + " " + Xml_Node.InnerText);
    }
  }

I've tried this:

  XmlNode node3 = XmlDoc.SelectSingleNode("PNRef");
  Console.WriteLine(node3.Value);

  Console.WriteLine(XmlDoc.InnerXml);

  var tst = XmlDoc.GetElementsByTagName("PNRef");
  Console.WriteLine(tst);

And this:

  NodePath = (XmlElement) XmlDoc.SelectSingleNode("/Response");

  if (NodePath != null)
  {
    foreach (XmlNode node in NodePath)
    {
      Console.WriteLine("NodeName: " + Xml_NodeX.Name);
      Console.WriteLine("NodeValue: " + node.InnerText);
    }
   }

Apparently, I'm not getting the xml read/write. I've done it with DataSets but they do all the work for you.

Can someone point me in the right direction? I've been at this for longer than I should have been already.

Thank you!

like image 886
ErocM Avatar asked Dec 10 '25 09:12

ErocM


2 Answers

Your XML has a XML namespace and you're not paying any attention to it:

<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
          xmlns="http://DFISofft.com/SmartPayments/">
          ******************************************

When selecting from this XML, you need to use that XML namespace!

You're not taking into account the XML namespace (xmlns="http://nts-de-osm1-pxc/webservices/") on the document!

Try this:

 var XmlDoc = new XmlDocument();

 // setup the XML namespace manager
 XmlNamespaceManager mgr = new XmlNamespaceManager(XmlDoc.NameTable);

 // add the relevant namespaces to the XML namespace manager
 mgr.AddNamespace("ns", "http://DFISofft.com/SmartPayments/"); 

 XmlDoc.LoadXml(Resources.xmltest);

 // **USE** the XML anemspace in your XPath !!
 XmlElement NodePath = (XmlElement) XmlDoc.SelectSingleNode("/ns:Response");

  while (NodePath != null)
  {
    foreach (XmlNode Xml_Node in NodePath)
    {
      Console.WriteLine(Xml_Node.Name + " " + Xml_Node.InnerText);
    }
  }
like image 113
marc_s Avatar answered Dec 11 '25 22:12

marc_s


The problem is your XPath query, which doesn't specify a namespace - despite the fact that your document only has a Response element in the "http://DFISofft.com/SmartPayments/" namespace. Ditto the PNRef elements.

Personally I'd use LINQ to XML if I actually wanted to use namespaces if at all possible. For example:

XNamespace ns = "http://DFISofft.com/SmartPayments/";
XDocument doc = XDocument.Load(filename);

foreach (var element in doc.Descendants(ns + "PNRef"))
{
    // Use element here
}

As you can see, LINQ to XML makes it really easy to use namespaces - but of course it requires .NET 3.5 or higher. If I had to use .NET 2.0, I'd either use XmlNamespaceManager or iterate manually and check local names instead of using XPath.

like image 20
Jon Skeet Avatar answered Dec 11 '25 22:12

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!