如何编写单个 LINQ to XML 查询来迭代所有子元素和子元素子元素的所有属性?
我正在开发 asp.net 移动应用程序。我使用 XML 作为数据库。我正在查询 XML 以访问所需的元素&在 .net 中使用 LINQ to XML 的属性。我的 XML 文件中有以下部分。
<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 文件中,我可以访问子元素节点“VALVE”&动态地使用其属性“NAME”,无需使用以下代码显式指定属性名称。 因此,当在子节点“VALVE”中添加任何新属性时,我可以在不修改现有代码的情况下访问它。 对于此类逻辑,我使用以下代码。在下面的代码中,ValvesProperties 是在“Vallves”类中定义的哈希表。
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 to XML 查询,该查询可以访问“VALVE”节点的 NAME 属性 (
),然后它应该访问'DISPLAY-NODE' (
), & 之间的文本那么它应该动态访问“COMMAND”节点的所有属性(
) 。所有这些我都想要动态地进行,而无需显式指定子节点的名称及其属性的名称(类似于我编写上述查询的方式)我们可以使用 LINQ to XML 编写这样的代码吗?如果我们可以用单个逻辑编写上述问题的代码(类似于我编写上述查询的方式),那就更好了。您能给我提供可以解决上述问题的任何代码或链接吗?
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>
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;
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以迭代所有元素并检索每个元素的属性:
You could just iterate through all the elements and retrieve the attributes for each: