Object reference not set to an instance of an object when reading XML

时间:2016-04-04 17:16:45

标签: c# xml

Here is the XML file I need to process:

<?xml version="1.0" encoding="UTF-8"?>
<shipment-info xmlns="http://www.canadapost.ca/ws/shipment-v8">
    <shipment-id>11111111</shipment-id>
    <shipment-status>created</shipment-status>
    <tracking-pin>123456789012</tracking-pin>
    <links>
        <link rel="self" href="https://xxx" media-type="application/vnd.cpc.shipment-v8+xml"/>
        <link rel="details" href="https://xxx" media-type="application/vnd.cpc.shipment-v8+xml"/>
        <link rel="group" href="https://xxx" media-type="application/vnd.cpc.shipment-v8+xml"/>
        <link rel="price" href="https://xxx" media-type="application/vnd.cpc.shipment-v8+xml"/>
        <link rel="label" href="https://xxx" media-type="application/pdf" index="0"/>
    </links>
</shipment-info>

And I want to get the tracking pin value, here is the code for doing this:

// xml text
string xml = (xml source above)

// load xml
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

// getting tracking pin
XmlNode node = doc.SelectSingleNode("/shipment-info"); // problem start here -> node return null

Console.WriteLine(node["tracking-pin"].InnerText);

// getting self and label links
node = doc.SelectSingleNode("/shipment-info/links");
foreach (XmlNode child in node)
{
    if (child.Attributes["rel"].Value == "self")
        Console.WriteLine(child.Attributes["href"].Value);
    else if (child.Attributes["rel"].Value == "label")
        Console.WriteLine(child.Attributes["href"].Value);
}

Console.ReadLine();

However, for selecting "/shipment-info" it return a null value, and I don't know why this happen. How to handle it?

3 个答案:

答案 0 :(得分:0)

You have a namespace issue. Try this XML Linq solution

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XNamespace ns = ((XElement)(doc.FirstNode)).Name.Namespace;
            string tracking_pin = doc.Descendants(ns + "tracking-pin").FirstOrDefault().Value;
        }
    }
}

答案 1 :(得分:0)

add that to your code

XmlNode root = doc.DocumentElement;

答案 2 :(得分:0)

You have a namespace for which you need to account:

// load xml
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

System.Xml.XmlNamespaceManager xmlnsManager = new System.Xml.XmlNamespaceManager(doc.NameTable);

//Add the namespaces used in books.xml to the XmlNamespaceManager.
xmlnsManager.AddNamespace("veeeight", "http://www.canadapost.ca/ws/shipment-v8");


// getting tracking pin
XmlNode node = doc.SelectSingleNode("//veeeight:shipment-info", xmlnsManager); // problem start here -> node return null

see:

https://support.microsoft.com/en-us/kb/318545