简体   繁体   English

如何使用xPath或其他方式获取这些最后的XML项目?

[英]How can I get those last XML items using xPath or anything else?

I've used the child elements of the first node PovprecnaPlaca before I noticed I need the child elements of the last one. 在注意到需要最后一个子元素之前,我已经使用了第一个节点PovprecnaPlaca的子元素。

Picture of XML for reference: XML图片供参考:

XML图片供参考

Appreciate all the helpful answers! 感谢所有有用的答案!

XML looks like this: XML看起来像这样:

<PovprecnaPlaca>
   <Datum>2017-07-01T00:00:00</Datum>
   <BrutoPlacaZadnje3Mesece>1603,00</BrutoPlacaZadnje3Mesece>
   <NetoPlacaZadnje3Mesece>1045,12</NetoPlacaZadnje3Mesece>
   <BrutoPlaca>1593,10</BrutoPlaca>
   <NetoPlaca>1039,55</NetoPlaca>
 </PovprecnaPlaca>
 <PovprecnaPlaca>
   <Datum>2017-08-01T00:00:00</Datum>
   <BrutoPlacaZadnje3Mesece>1602,98</BrutoPlacaZadnje3Mesece>
   <NetoPlacaZadnje3Mesece>1045,45</NetoPlacaZadnje3Mesece>
   <BrutoPlaca>1613,62</BrutoPlaca>
   <NetoPlaca>1051,73</NetoPlaca>
  </PovprecnaPlaca>
 </Place>
</PovprecnePlace>

Get Datum node from the last PovprecnaPlaca of each Place . 从每个Place的最后一个PovprecnaPlaca获取Datum节点。

var nodes = xdoc.XPathSelectElements("//PovprecnaPlaca[last()]/Datum");

Get the text inside each node. 获取每个节点内的文本。

List<string> results = nodes.Select(d => d.Value).ToList();

Two reasons why your attemp didn't work: 1) It starts from root node "/" of the document and there's no PovprecnaPlaca node at this level. 尝试失败的两个原因:1)它从文档的根节点“ /”开始,并且在此级别没有PovprecnaPlaca节点。 2) By calling text() it's getting XText which raises an exception in Linq as it expects nodes as a result when calling XPathSelectElements() . 2)通过调用text() ,将得到XText ,这会在Linq中引发异常,因为它期望在调用XPathSelectElements()时得到节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM