使用linq解析xml文件到xml [英] parse xml file using linq to xml

查看:87
本文介绍了使用linq解析xml文件到xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在研究一个解析大量xml文件并从中提取6个值的项目,我认为最简单的方法是使用linq到xml.
这是xml的样子:(所有值的``<''``>''已由编辑器取出.)

XAxisCalib
最高288.93最高
Min-48.08 Min

So i''m working on a project to parse lots of xml files and extract 6 values from them I think the easiest is using linq to xml.
Here is what the xml looks like: ( the ''<'' ''>'' around all values have been taken out by editor.)

XAxisCalib
Max 288.93 Max
Min-48.08 Min

<maxs>200</maxs>           
<dftht>0</dftht>           
<ldve>0</ldve>           
<ejve>0</ejve>



YAxisCalib
最高646.01/最高
最低90.9/最低



YAxisCalib
Max 646.01/Max
Min-90.9/Min

<maxs>200</maxs>           
<dftht>0</dftht>           
<ldve>0</ldve>          
 <ejve>0</ejve>         
<yaxiscalib> </yaxiscalib>


ZAxisCalib
最高16.1最高
最低146.82最低

我需要从文件中提取每个x,y和z校准的最大值和最小值.
有人对此有帮助吗?

我有这个示例,但是如何操作它来提取这6个值.我只想要这些值,并且想将它们保存到屏幕上不显示的文件中


ZAxisCalib
Max 16.1 Max
Min-146.82 Min

I need to extract form the files the max and min of each x,y,and z calib.
Anyone help with this?

I have this example but how do I manipulate it to extract those 6 values. I only want those values and I want to save them to a file not display on screen

string file = @"c:\file.xml";  
XDocument doc = XDocument.Load(file);   
int i = 0;   
var result = from ele in doc.Descendants()                    
             where ele.HasElements == false //read leaf node                       
             select new{ ParentName=ele.Parent.Name, Name=ele.Name , Value=ele.Value };  

foreach (var ele in result )     
{          
    Console.WriteLine(ele.ParentName + " " + ele.Name + " " + ele.Value);    
}

推荐答案

我首先看到的是您的new语句是垃圾.应该是这样的:

The first thing I see is that your new statement is junk. It should be something like this:

var result = from ele in doc.Descendants()
             where ele.HasElements == false //read leaf node
             select new SomeObject(){ ParentName=ele.Parent.Name, Name=ele.Name , Value=ele.Value };



我看到的第二件事是在foreach语句中使用var.只需使用正确的类型.效率更高.

最后,我建议您对变量名使用完整的单词.



The second thing I see is the use of var in your foreach statement. Just use the correct type. It''s much more efficient.

Finally, I recommend that you use complete words for variable names. It makes your code a LOT easier to read.


我可以对xaxis calib"max"做类似的事情,然后重复"min"然后将其ycalib max更改吗?和min然后zcalib max和min.

字符串文件= @"c:\ ScicloneUAC.xml";
XDocument doc = XDocument.Load(file);
int i = 0;
var结果=来自doc.Descendant(XAxisCalib)中的ele
其中ele.HasElements == false

选择新的{Max = ele."Max" .value,};
foreach(结果为var ele){
Console.WriteLine(ele."Max".+");
}
Could I do something like this for the xaxis calib ''max'' and then repeat for ''min'' then change it ycalib max and min then zcalib max and min.

string file = @"c:\ScicloneUAC.xml";
XDocument doc = XDocument.Load(file);
int i = 0;
var result = from ele in doc.Descendant(XAxisCalib)
where ele.HasElements == false

select new { Max=ele."Max".value, };
foreach (var ele in result ) {
Console.WriteLine(ele."Max".+" ");
}


这篇关于使用linq解析xml文件到xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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