无法遍历Xpath [英] Not able to traverse through Xpath
本文介绍了无法遍历Xpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨朋友们,
我无法遍历Xpath,直到 / x:GetPositionByAccountResponse / x:GetPositionByAccountResult /
它接收
但我需要当前XML的办公室号码
示例XML和下面给出的代码。
请帮助
Hi Friends,
I am not able to traverse through Xpath, till /x:GetPositionByAccountResponse/x:GetPositionByAccountResult/
its picking up
but i need office number from the current XML
Sample XML and code given below.
Please help
static void Main(string[] args)
{
NameTable nt = new NameTable();
XmlNamespaceManager mgr = new XmlNamespaceManager(nt);
mgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
mgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
mgr.AddNamespace("x", "http://tempuri.org/");
mgr.AddNamespace("y", "http://ebc.mssb.com/");
XmlDocument XMLDoc = new XmlDocument();
XMLDoc.Load("XMLFile1.xml");
string xpath = "/x:GetPositionByAccountResponse/x:GetPositionByAccountResult/y:Message/y:EBCMessageType[2]/y:Message/y:NewDataSet/y:Positions/y:Office";
foreach (XmlNode ele1 in XMLDoc.SelectNodes(xpath, mgr))
{
Console.WriteLine(ele1.InnerXml.ToString());
}
}
<GetPositionByAccountResponse xmlns="http://tempuri.org/">
<GetPositionByAccountResult>
<ReferenceID xmlns="http://ebc.mssb.com">PKCUb5XThXoIzsqAa71</ReferenceID>
<ReceivedTime xmlns="http://ebc.mssb.com">2015-11-09T02:43:41.5535744-05:00</ReceivedTime>
<CompletedTime xmlns="http://ebc.mssb.com">2015-11-09T02:43:41.9246115-05:00</CompletedTime>
<StatusCode xmlns="http://ebc.mssb.com">Success</StatusCode>
<Message xmlns="http://ebc.mssb.com">
<EBCMessageType>
<MessageType>COMMAND_STRING</MessageType>
</EBCMessageType>
<EBCMessageType>
<Message>
<NewDataSet>
<Positions>
<Office>101</Office>
<Account>14716</Account>
<KeyAccount>1999-08-27-17.24.19.730418</KeyAccount>
<TradeControl>0</TradeControl>
</Positions>
</NewDataSet>
</Message>
<MessageType>RAW_DATA</MessageType>
</EBCMessageType>
</Message>
</GetPositionByAccountResult>
</GetPositionByAccountResponse>
推荐答案
嗯,这个一个有用:
Well, this one works:
static void Main(string[] args)
{
var xmlDoc = new XmlDocument();
XMLDoc.Load("XMLFile1.xml");
XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable);
manager.AddNamespace("ns", "http://ebc.mssb.com");
XmlNode root = xmlDoc.DocumentElement;
const string xpath = "//ns:Office";
if (root != null)
{
var xmlNodeList = root.SelectNodes(xpath, manager);
if (xmlNodeList == null) return;
foreach (XmlNode ele1 in xmlNodeList)
Console.WriteLine(ele1.InnerXml);
}
}
这篇关于无法遍历Xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文