2016-04-04 6 views
1

Hier wird die XML-Datei, die ich verarbeiten müssen:Objektverweis auf eine Instanz eines Objekts nicht festgelegt, wenn Lesen von 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> 

Und ich möchte den Tracking-Pin-Wert erhalten, hier ist der Code, dies zu tun :

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

jedoch für die Auswahl von „/ Versand-Info“ es einen Nullwert zurückgeben, und ich weiß nicht, warum dies geschieht. Wie geht man damit um?

Antwort

0

Sie haben einen Namespace, für die Sie berücksichtigen müssen:

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

siehe:

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

+0

Danke, jetzt kenne ich das Problem des Namespace –

0

Sie haben ein Namespace-Problem. Versuchen Sie, diese XML-Linq Lösung

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

Th Ank Sie so sehr, aber was genau ist falsch mit meiner Lösung? –

+0

Wie ich in meiner Lösung sagte, haben Sie den Namespace nicht berücksichtigt. – jdweng

0

hinzufügen, dass, um Ihren Code

XmlNode root = doc.DocumentElement; 
Verwandte Themen