[英]in C# how to select specific Data from an XMLDocument?
i got below response through web request 我通过网络请求得到了以下回复
<Shofar Target_="Base" >
<RapidOnSite_Pump Key="1" State="Idle" Amount="20" />
<RapidOnSite_Pump Key="2" State="Idle" Amount="15" />
<RapidOnSite_Pump Key="3" State="Despencing" Amount="25"/>
- <Verb_List>
<Verb_Item Key="1" Execute="RapidOnSite_Pump_AdHoc" Result="OK"
Detail="All Pumps►5" />
</Verb_List>
from this string i only want to select Nodes which State is not Idle. 从该字符串中,我只想选择状态不空闲的节点。
My Code 我的密码
string responseFromServer = MY_RESPONSE
XmlDocument mainxml = new XmlDocument();
mainxml.LoadXml(responseFromServer);
XmlNodeList xnList = mainxml .SelectNodes("/Shofar/RapidOnSite_Pump [@State!='Idle']");
But by this code i didnt get requirments. 但是通过此代码,我没有收到要求。 can anyone please help me?
谁能帮帮我吗?
In the chat you show real xml. 在聊天中,您将显示真实的xml。 It contains the namespace which must be taken into account!
它包含必须考虑的名称空间!
mainxml.LoadXml(responseFromServer);
XmlNamespaceManager manager = new XmlNamespaceManager(mainxml.NameTable);
manager.AddNamespace("ns", "http://ShofarNexus.com");
XmlNodeList xnList = mainxml.SelectNodes("/ns:Shofar/ns:RapidOnSite_Pump [@State!='Idle']", manager);
You can use XElement to get your work done, unless you want to stick to XmlDocument. 您可以使用XElement完成工作,除非您想坚持使用XmlDocument。
XElement rootElem = XElement.Load(new StringReader(xmlStr));
var rapidOnSitePumpWhichAreNotIdle = rootElem.Descendants("RapidOnSite_Pump").Where(e => e.Attribute("State").Value != "Idle");
At the end you will get IEnumerable<XElement>
which can be iterated over. 最后,您将获得
IEnumerable<XElement>
可以对其进行迭代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.