【问题标题】:Object reference not set to an instance of an object when reading XML读取 XML 时未将对象引用设置为对象的实例
【发布时间】:2016-04-05 01:29:06
【问题描述】:

这是我需要处理的 XML 文件:

<?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>

我想获取跟踪引脚值,这是执行此操作的代码:

// 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();

但是,对于选择“/shipment-info”,它返回一个空值,我不知道为什么会发生这种情况。如何处理?

【问题讨论】:

    标签: c# xml


    【解决方案1】:

    您有一个需要考虑的命名空间:

    // 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
    

    见:

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

    【讨论】:

    • 谢谢,我知道namespace的问题了
    【解决方案2】:

    您遇到了命名空间问题。试试这个 XML Linq 解决方案

    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;
            }
        }
    }
    

    【讨论】:

    • 非常感谢,但我的解决方案到底有什么问题?
    • 正如我在我的解决方案中所说,您没有考虑命名空间。
    【解决方案3】:

    将其添加到您的代码中

    XmlNode root = doc.DocumentElement;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      相关资源
      最近更新 更多