如何在C#中使用linq制作xml过滤器 [英] How to make filter of xml with linq in c#

查看:106
本文介绍了如何在C#中使用linq制作xml过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

盖伊,我需要您的帮助.我在bigining有一些这样的XML文件

 <?  xml    版本  ="    编码  ="  utf-8" ? <  订单 > 
  <  订购       104710"    ="  > 
    <  信息 > 
      <  信息    ="   1047101"    ="   mat1"    ="  免费" <  考试 > 
        <  /考试 > 
      <  /Infos  > 
      <  信息    ="   1047102"  MatName   > mat2"    状态  =" 免费"  > 
        <  考试 > 
        <  /考试 > 
      <  /Infos  > 
      <  信息    ="   1047103"  MatName   > mat3"    状态  =" 免费"  > 
        <  考试 > 
        <  /考试 > 
      <  /Infos  > 
      <  信息    ="   1047104"  MatName    mat4"   状态  =" 忙" <  考试 > 
        <  /考试 > 
      <  /Infos  > 
      <  信息    ="   1047105"  MatName   > mat4"    状态  =" 免费"  > 
        <  考试 > 
        <  /考试 > 
      <  /Infos  > 
      <  信息    ="   1047106"  MatName    EX-11445GF " 数量(QTY)   40" 状态  忙" <  考试 > 
        <  /考试 > 
      <  /Infos  > 
    <  /信息 > 
  <  /Order  > 
  <  订购       123788"    ="  > 
    <  信息 > 
      <  信息    ="   1237881"    ="   mat1"    ="  忙"  <  考试 > 
        <  /考试 > 
      <  /Infos  > 
      <  信息    ="   1237882"  MatName   > mat2"    状态  =" 忙" <  考试 > 
        <  /考试 > 
      <  /Infos  > 
      <  信息    ="   1237883"  MatName    mat3"   状态  =" 免费"  > 
        <  考试 > 
        <  /考试 > 
      <  /Infos  > 
      <  信息    ="   1237884"  MatName    mat4"   状态  =" 忙" <  考试 > 
        <  /考试 > 
      <  /Infos  > 
      <  信息    ="   1237885"  MatName    mat4"   状态  =" 免费"  > 
        <  考试 > 
        <  /考试 > 
      <  /Infos  > 
      <  信息    ="   1237886"  MatName    EX-11445GF " 数量(QTY)   40" 状态  忙" <  考试 > 
        <  /考试 > 
      <  /Infos  > 
    <  /信息 > 
  <  /Order  > 
<  /订单 >   



请问我如何获得状态"为忙"的父母"属性,这意味着OrderID优先级,InfosID和MatName.我没有找到一个很好的要求.请问我的要求

  var  InfosAttribut = 来自 xml  in  xdoc.Descendants(" )
                                    其中(字符串)xml.Attribute(" 状态")==  忙碌"
                                    选择 xml.Attributes();


有了这个LinQ请求,我只是获得标签信息" 的属性,但是我想一次获得所有这些东西.有可能吗?
thx

解决方案

在您的LINQ查询中,xml指的是 Infos 元素.您可以执行以下操作:

  var  InfosAttribut = 来自 xml  xdoc.Descendants(" )
                                    其中 xml.Descendants("  ).Any(d = > (字符串)d.Attribute( 状态")==  )
                                    选择 xml.Attributes();



xml现在将引用 Order 元素,并检查是否有标记为Busy的 Infos 元素.


Hi Guy please i Need your help. i have some XML File like this one at the bigining

<?xml version="1.0" encoding="utf-8" ?>
<Orders>
  <Order OrderID ="104710"  Priority="normal">
    <Informations>
      <Infos InfosID="1047101"  MatName="mat1"  Status="Free">
        <exam>
        </exam>
      </Infos>
      <Infos InfosID="1047102" MatName="mat2" Status="Free">
        <exam>
        </exam>
      </Infos>
      <Infos InfosID="1047103" MatName="mat3" Status="Free">
        <exam>
        </exam>
      </Infos>
      <Infos InfosID="1047104" MatName="mat4" Status="Busy">
        <exam>
        </exam>
      </Infos>
      <Infos InfosID="1047105" MatName="mat4" Status="Free">
        <exam>
        </exam>
      </Infos>
      <Infos InfosID="1047106" MatName="EX-11445GF" QTY="40" Status="Busy">
        <exam>
        </exam>
      </Infos>
    </Informations>
  </Order>
  <Order OrderID ="123788"  Priority="normal">
    <Informations>
      <Infos InfosID="1237881"  MatName="mat1"  Status="Busy">
        <exam>
        </exam>
      </Infos>
      <Infos InfosID="1237882" MatName="mat2" Status="Busy">
        <exam>
        </exam>
      </Infos>
      <Infos InfosID="1237883" MatName="mat3" Status="Free">
        <exam>
        </exam>
      </Infos>
      <Infos InfosID="1237884" MatName="mat4" Status="Busy">
        <exam>
        </exam>
      </Infos>
      <Infos InfosID="1237885" MatName="mat4" Status="Free">
        <exam>
        </exam>
      </Infos>
      <Infos InfosID="1237886" MatName="EX-11445GF" QTY="40" Status="Busy">
        <exam>
        </exam>
      </Infos>
    </Informations>
  </Order>
</Orders>



Please how can i get the Parents attributs where Status = "Busy" that means OrderID Priority, InfosID, and MatName. i didnt found a good request. hier ist my request

var InfosAttribut = from xml in xdoc.Descendants("Infos")
                                    where (string)xml.Attribute("Status") == "Busy"
                                    select xml.Attributes();


with this LinQ Request i just get Attribut of Tag "Infos" but i want to get all this Thing in one time. is it possible?
thx

解决方案

In your LINQ query xml refers to the Infos element. You could do this instead:

var InfosAttribut = from xml in xdoc.Descendants("Order")
                                    where xml.Descendants("Info").Any(d => (string)d.Attribute("Status") == "Busy")
                                    select xml.Attributes();



xml will now refer to the Order element, and a check is done whether there is any Infos element marked as Busy.


这篇关于如何在C#中使用linq制作xml过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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