通过XML文件排序 [英] Sorting Through XML Files

查看:92
本文介绍了通过XML文件排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一个程序,该程序将对特定项目的XML文档进行排序,并返回该项目。我一直在尝试使用XMLNodeList和XMLNodeItem,但没有任何运气。以下是我的XML文档的示例,
以及我试图找到的内容


< NEO>


< section name =" NeoInterface">


< entry name =" ProductVersion">" Some Value"< / entry>


< entry name =" ReleaseDate">" Some Value2"< / entry>


< entry name =" DB Path">" Some Value3"< / entry>


< ; / section>


< / NEO>


非常具体,我正在尝试查找子条目的值(除非我是误解了这个)产品版本。这是我尝试过的,并且取得了"最大的成功"。 with:

 XMLDocument doc = new XMLDocument(); 

doc.Load(" filepath\NEO.xml");

XmlNodeList list = doc.GetElementsByTagName(" entry");

for(int i = 0; i< list.Count; i ++)
{
Console.WriteLine(list [i] .InnerXML);
}

这似乎成功地抛出了每一个"某些价值"。进入一个输出。我已经尝试将这些值(在FOR循环内)移动到一个数组中,然后移动到变量中,但我没有成功将它们分开,以便找到特定的"Product
Version"。 



有人能指出我正确的方向吗?我觉得我很接近。  



 

解决方案

为了提取产品版本,试试这个:


 XmlDocument doc = new XmlDocument(); 
doc.Load(" filepath\NEO.xml");

var neo_interface = doc.SelectSingleNode(" / NEO / section [@ name ='NeoInterface']");
string product_version = neo_interface?.SelectSingleNode(" entry [@ name ='ProductVersion']")?. InnerText;


它使用 XmlDocument 类。


I'm trying to run a program that will sort through an XML document for a specific item, and return that item. I have been experimenting using the XMLNodeList and XMLNodeItem, but aren't having any luck. Here is an example of what my XML Document looks like, and what I"m trying to find

<NEO>

<section name="NeoInterface">

<entry name="ProductVersion">"Some Value"</entry>

<entry name="ReleaseDate">"Some Value2"</entry>

<entry name="DB Path">"Some Value3"</entry>

</section>

</NEO>

Very specifically, I'm trying find the value of the child entry(unless i've misunderstood this) for Product Version. Here is what I have tried, and had the "most success" with:

XMLDocument doc = new XMLDocument();

doc.Load("filepath\NEO.xml");

XmlNodeList list = doc.GetElementsByTagName("entry");

for(int i = 0; i < list.Count; i++)
{
   Console.WriteLine(list[i].InnerXML);
}

This appears to successfully throw every "Some Value" into an output. I have tried moving these values (inside the FOR Loop) into an array, and into variables, but I have no success with breaking them apart, as to find the specific "Product Version". 

Can somebody point me in the right direction? I feel like I"m close.  

 

解决方案

In order to extract Product Version, try this:

XmlDocument doc = new XmlDocument();
doc.Load("filepath\NEO.xml");

var neo_interface = doc.SelectSingleNode( "/NEO/section[@name='NeoInterface']" );
string product_version = neo_interface?.SelectSingleNode( "entry[@name='ProductVersion']" )?.InnerText;

It uses the XmlDocument class.


这篇关于通过XML文件排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆