SelectNodes返回空集

时间:2018-05-01 09:14:30

标签: c# xml xpath xml-namespaces

我无法从SOAP调用中收到的XML中查找节点和节点列表。返回的XML包含名称空间,我认为这是我遇到麻烦的地方。

这就是我的回忆:

<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pbs="http://www.random.com/API"><SOAP-ENV:Body><pbs:getContiguousListEventsOutput><pbs:nEventCount>1</pbs:nEventCount><eventListContainer xmlns="http://www.random.com/API"><eventList xmlns=""><event timerMarker="false" enabled="true" xmlns="" uid="21985937677965" type="PrimaryVideo"><properties><schedule endType="Duration" endOffset="00:20:19:00" startType="Sequential" startOffset="2018-05-01T08:45:05:14" /><media mediaType="Video" mediaName="20381363" /><mediaStream som="10:00:00:00"><video jobType="Play" /><segment type="Markup"><markup orderNo="1" name="AA 1 Pt" /></segment><allocation type="ListStream"><listStream type="Fixed" listStreamNo="0" /></allocation></mediaStream><event title="FRASIER YR 9 (9): Ep 907" houseId="20381363" reconcileKey="147553763"><classifications><classification category="Programme" classification="Event Type" /><classification category="yes" classification="Segment Previewed" /></classifications><comment>A test note</comment></event><switch rate="Fast" transition="Cut"><source type="Auto"><auto type="MediaStream" /></source><destination type="Auto"><auto type="PGM" /><fixed port="" device="" /></destination><backupSource type="Auto"><auto type="MediaStream" /></backupSource><backupDestination type="Auto"><auto type="PGM" /></backupDestination></switch><features><feature type="AspectRatio"><properties><macro value="4:3" /></properties></feature><feature type="Subtitle"><properties><media mediaType="Subtitle" mediaName="20381363" /><mediaStream><subtitle /><allocation type="ListStream"><listStream type="Fixed" listStreamNo="0" /></allocation></mediaStream></properties></feature></features><auxData><broadcastMaster><TVGuideTime>10:45</TVGuideTime><SegmentNo>1</SegmentNo><SegmentCount>1</SegmentCount><ProgEventID>147551823</ProgEventID></broadcastMaster></auxData></properties><childEvents><event timerMarker="false" enabled="true" uid="21985937677966" type="LOGO_VF"><properties><schedule endType="-ParentEnd" endOffset="00:00:00:00" startType="+ParentStart" startOffset="00:00:00:00" /><event title="LOGO_VF" reconcileKey="148769998" /><mediaStream><cg layer="0" type="Page" /><allocation type="ListStream"><listStream type="Fixed" listStreamNo="0" /></allocation></mediaStream><media mediaType="CG" mediaName="1" /></properties></event></childEvents></event></eventList></eventListContainer<pbs:bMoreEventsAvailable>true</pbs:bMoreEventsAvailable><pbs:nextEventId type="SERVER_UID" value="21985937677967"></pbs:nextEventId></pbs:getContiguousListEventsOutput></SOAP-ENV:Body></SOAP-ENV:Envelope>

我声明了我的命名空间并将它们添加到命名空间管理器中,我可以找到一个节点:

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.LoadXml(getList.soapResult);

XmlNamespaceManager xmlNsMgr = new XmlNamespaceManager(xmlDoc.NameTable);
xmlNsMgr.AddNamespace("pbs", "http://www.random.com/API");
xmlNsMgr.AddNamespace("SOAP-ENV", "http://www.w3.org/2003/05/soap-envelope");

XmlNode _node = xmlDoc.SelectSingleNode("//SOAP-ENV:Envelope/SOAP-ENV:Body/pbs:getContiguousListEventsOutput", xmlNsMgr);

问题是我想得到'event'元素的XmlNodeList,我认为可行的XPath表达式是:

//SOAP-ENV:Envelope/SOAP-ENV:Body/pbs:getContiguousListEventsOutput/eventListContainer/eventList/event

所以我尝试使用它,但它返回一个空集:

 XmlNodeList _list = xmlDoc.SelectNodes("//SOAP-ENV:Envelope/SOAP-ENV:Body/pbs:getContiguousListEventsOutput/eventListContainer/eventList/event", xmlNsMgr);

此时我被困住了。我可以从xml和SelectNodes中删除命名空间装饰,我可以看到这个'event'元素列表。

1 个答案:

答案 0 :(得分:0)

您需要将pbs命名空间添加到XPath的eventListContainer部分。

        XmlNodeList _list = xmlDoc.SelectNodes("//SOAP-ENV:Envelope/SOAP-ENV:Body/pbs:getContiguousListEventsOutput/pbs:eventListContainer/eventList/event", xmlNsMgr);
        Debug.WriteLine(_list[0].OuterXml);

或使用缩短版本:

        _list = xmlDoc.SelectNodes("//pbs:eventListContainer/eventList/event", xmlNsMgr);
        Debug.WriteLine(_list[0].OuterXml);