如何写一个单一的LINQ to XML查询通过所有的子元素和放大器迭代;子元素的所有属性? [英] How to write a single LINQ to XML query to iterate through all the child elements & all the attributes of the child elements?

查看:103
本文介绍了如何写一个单一的LINQ to XML查询通过所有的子元素和放大器迭代;子元素的所有属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发asp.net移动应用程序。我使用XML作为数据库。我查询对XML访问所需元素&放大器;在.NET中使用LINQ to XML属性。我在我的XML文件follwing一部分。

I am developing asp.net mobile application. I am using XML as a database. I am querying on the XML to access the required elements & attributes by using LINQ to XML in .net. I have the follwing part in my XML file.

<VALVES>
     <VALVE NAME="PWV">
      <DISPLAY-NAME>
       Production Master Valve
      </DISPLAY-NAME>
      <COMMANDS USE-TEMPLATE="TRUE" TEAMPLATE-NAME="ValveCommands.xml"></COMMANDS>
    </VALVE>
    <VALVE NAME="PMV" >
     <DISPLAY-NAME>
      Production Wing Valve
     </DISPLAY-NAME>
     <COMMANDS USE-TEMPLATE="TRUE" TEAMPLATE-NAME="ValveCommands.xml"></COMMANDS>
    </VALVE>
</VALVES>

在上面的XML文件,我可以访问子元素节点'阀门'和;其属性'NAME'动态不使用以下code明确指定属性的名称。 因此任何新的属性在子节点'阀门'加我可以访问它,而无需修改现有的code。对于这样的逻辑,我使用以下code。在下面的code ValvesProperties是在类'阀门'定义的哈希表

In the above XML file I can access the the child element node 'VALVE' & its attribute 'NAME' dynamically without explicitly specifying the name of the attribute by using following code. Thus as any new attribute is added in the child node 'VALVE' I can access it without modifying my existing code. For such logic I am using following code. In the following code ValvesProperties is the Hashtable which is defined in the class 'Valves'.

 static int count = 0;
 List<Valves> LstValves = new List<Valves>();

 string AttName = null;
 string AttValue = null;

 XDocument FieldDef = XDocument.Load(@"F:\Shailesh from user OPC\XML\KK3.xml");
 XElement FieldRoot = FieldDef.Element("KK");

 count = FieldRoot.Element("FIELD-DEFINITION").Element("VALVES").Elements("VALVE").Count();
 Valves[] Valveobj = new Valves[count];

 count = 0;

 foreach (var element in FieldRoot.Element("FIELD-DEFINITION").Element("VALVES").Elements())
 {
     Valveobj[count] = new Valves();

     foreach (var attribute in element.Attributes())
     {
         AttName = attribute.Name.ToString();
         AttValue = attribute.Value.ToString();
         Valveobj[count].ValvesProperties.Add(AttName, AttValue);
         LstValves.Add(Valveobj[count]);
     }

     count++;
  }

return LstValves;

类似的逻辑,我需要为上述定义的XML部分。在上面的XML我想将LINQ写入可访问VALVE节点的NAME属性的XML查询(&LT;阀门NAME =PWV&GT; ) ,那么就应该进入DISPLAY-NODE'之间的文本(&LT; /显示名称GT&; DISPLAY-名称&gt;生产主阀和LT ),放大器;那么应该访问COMMAND节点的所有属性动态(&LT;命令使用模板=TRUETEAMPLATE-NAME =ValveCommands.xml&GT;&LT; /命令&GT; )。所有这些我要动态而不显式指定的子节点的属性(类似我写上面的查询方式),我们可以使用LINQ to XML写这样的code的名字以及名字?这将是更好,如果我们可以写在一个单一的逻辑code以上问题(类似我写上述查询的方式)。你能为我提供的任何code或链接,通过它我可以解决上述问题?

The similar logic I need for the above defined XML part. In the above XML I want to write the LINQ to XML query which can access the NAME attribute of the 'VALVE' node (<VALVE NAME="PWV">), then it should access the text between 'DISPLAY-NODE' (<DISPLAY-NAME> Production Master Valve </DISPLAY-NAME>), & then it should access the all the attributes of the 'COMMAND' node dynamically ( <COMMANDS USE-TEMPLATE="TRUE" TEAMPLATE-NAME="ValveCommands.xml"></COMMANDS>). All these I want dynamically without explicitly specifying the name of the child node as well as name of their attributes ( similar to the way I written the above query ) Can we write such a code by using LINQ to XML ? It will be better if we can write code for above issue in a single logic ( similar to the way I written the above query ). Can you provide me the any code or link through which I can resolve the above issue ?

推荐答案

您可以通过刚才的所有元素进行迭代,并获取属性的每个:

You could just iterate through all the elements and retrieve the attributes for each:

var allDescendants = myElement.DescendantsAndSelf();
var allDescendantsWithAttributes = allDescendants.SelectMany(elem =>
    new[] { elem }.Concat(elem.Attributes().Cast<XContainer>()));

foreach (XContainer elementOrAttribute in allDescendantsWithAttributes)
{
    // ...
}

这篇关于如何写一个单一的LINQ to XML查询通过所有的子元素和放大器迭代;子元素的所有属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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