我正在尝试解析以下 XML 示例:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soapenv:Body>
<d2LogicalModel modelBaseVersion="1.0" xmlns="http://datex2.eu/schema/1_0/1_0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://datex2.eu/schema/1_0/1_0 http://datex2.eu/schema/1_0/1_0/DATEXIISchema_1_0_1_0.xsd">
<payloadPublication xsi:type="PredefinedLocationsPublication" lang="en">
<predefinedLocationSet id="GUID-NTCC-VariableMessageSignLocations">
<predefinedLocation id="VMS30082775">
<predefinedLocationName>
<value lang="en">VMS M60/9084B</value>
</predefinedLocationName>
</predefinedLocation>
</predefinedLocationSet>
</payloadPublication>
</d2LogicalModel>
</soapenv:Body>
</soapenv:Envelope>
我特别需要获取顶级predefinedLocation 标记的内容。根据我的计算,正确的 XPath 应该是
/soapenv:Envelope/soapenv:Body/d2LogicalModel/payloadPublication/predefinedLocationSet/predefinedLocation
我正在使用以下 C# 代码来解析 XML: string filename = "content-sample.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filename);
XmlNamespaceManager nsmanager = new XmlNamespaceManager(xmlDoc.NameTable);
nsmanager.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/Envelope");
string xpath ="/soapenv:Envelope/soapenv:Body/d2LogicalModel/payloadPublication/predefinedLocationSet/predefinedLocation";
XmlNodeList itemNodes = xmlDoc.SelectNodes(xpath, nsmanager);
但是,这一直没有结果。任何人都可以对此有所了解,因为我觉得我的头撞在了砖墙上。