C# 读取XML时未将对象引用设置为对象的实例
以下是我需要处理的XML文件:C# 读取XML时未将对象引用设置为对象的实例,c#,xml,C#,Xml,以下是我需要处理的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> <
<?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>
然而,对于选择“/Shipping info”,它返回一个空值,我不知道为什么会发生这种情况。如何处理它?您遇到了名称空间问题。试试这个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;
}
}
}
您有一个名称空间问题。试试这个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;
}
}
}
将其添加到代码中
XmlNode root = doc.DocumentElement;
将其添加到代码中
XmlNode root = doc.DocumentElement;
您有一个名称空间,需要对其进行说明:
// 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
见:
您有一个需要说明的名称空间:
// 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
见:
非常感谢,但是我的解决方案到底出了什么问题?正如我在我的解决方案中所说的,你没有考虑命名空间。非常感谢,但是我的解决方案到底出了什么问题?正如我在我的解决方案中所说的,你没有考虑命名空间。谢谢,现在我知道NAMESPACTHACKAN你的问题了,现在我知道命名空间的问题了。