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?
Danke, jetzt kenne ich das Problem des Namespace –