使用linq2xml订单XML文件 [英] Order xml file using linq2xml

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

问题描述

以下问题过滤器的XML与LINQ2XML

在成功地过滤(删除节点)的XML文件。我想通过在节点的一些属性命令。

After succesfully filtering (removing nodes) from a xml file. I'd like to order by some attribute in nodes.

XML文件示例:

<Root> 
   <Group Price="50"> 
       <Item Price="60"/> 
       <Item Price="50"/> 
       <Item Price="70"/> 
   </Group> 
   <Group Price="55"> 
       <Item Price="62"/> 
       <Item Price="57"/> 
       <Item Price="55"/> 
   </Group> 
   <Group Price="61"> 
       <Item Price="62"/> 
       <Item Price="61"/> 
       <Item Price="65"/> 
    </Group> 
    <!--More Group Nodes-->  
</Root> 

和我想获得:

<Root> 
   <Group Price="61"> 
       <Item Price="65"/> 
       <Item Price="62"/> 
       <Item Price="61"/> 
    </Group> 
    <Group Price="55"> 
       <Item Price="62"/> 
       <Item Price="57"/> 
       <Item Price="55"/> 
   </Group> 
   <Group Price="50"> 
       <Item Price="70"/> 
       <Item Price="60"/> 
       <Item Price="50"/> 
   </Group> 
   <!--More Group Nodes-->  
</Root>

我目前的code是(混合LINQ2Xml和XPath):

My current code is (Mix LINQ2Xml and XPATH):

'First I remove Group nodes with prices higher than 60 (and their sons).

dim filter as String="./Root/Group[not(translate(@Price, ',.', '.')<=60})]"

elements = doc.XPathSelectElements(filter).OrderByDescending((Function(x) CType(x.Attribute("Price"), Decimal)))

'Remove elements what don't fullfill the condition  (prices higher than 60)                   
elements.Remove()

'After I remove Item nodes with prices higher than 60

filter as String="./Root/Group/Item[not(translate(@Price, ',.', '.')<=60})]"

elements = doc.XPathSelectElements(filter).OrderByDescending((Function(x) CType(x.Attribute("Price"), Decimal)))

'Remove elements what don't fullfill the condition  (prices higher than 60)
 elements.Remove()

正如我以前说过,我成功地过滤,但我不能命令(在这种情况下,降序)。 有没有什么办法可以订购一个步骤组节点和项目节点或我必须做的两个步骤?

As I said before, I'm filtering succesfully but I'm not able to order (descending in this case). Is there any way to order Group Nodes and Item nodes in one step or I have to do it in two steps?

有人告诉我如何使用的XDocument的replaceNodes,但我没有得到任何结果。

Someone told me about using replaceNodes of XDocument but I don't get any results.

再次感谢您的答复。

推荐答案

如果您想订购组下降,那么你可以使用LINQ和OrderByDescending。

If you want to order the group descending then you can use LINQ and OrderByDescending.

XDocument doc = XDocument.Parse("<Root>  <Group Price=\"50\">  <Item Price=\"60\"/>  <Item    Price=\"50\"/>  <Item Price=\"70\"/>  </Group>  <Group Price=\"55\">  <Item Price=\"62\"/>  <Item Price=\"57\"/>  <Item Price=\"55\"/>  </Group>  <Group Price=\"61\">  <Item Price=\"62\"/>  <Item Price=\"61\"/> <Item Price=\"65\"/>  </Group>  <!--More Group Nodes-->   </Root>  ");

IEnumerable<XElement> list = doc.Elements()
                                .Elements("Group")
                                .OrderByDescending(p => Convert.ToInt32(p.Attribute("Price").Value));

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

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