使用 XDocument 循环遍历节点的所有属性
我有以下存储表定义的 xml。如何使用 XDocument (C# 3.5) 循环遍历传递的表名的每一列(每个表仅出现一次)及其属性
例如:如果用户传递CurrencySummary,我想读取每一列及其所有属性,如 HeaderDescription、HeaderName 等我
<?xml version="1.0" encoding="utf-8" ?>
<TableDef>
<CurrencySummary>
<Column Name="Currency" HeaderDescription="Currency" HeaderName="Currency" ColumnType="TableColumnType.Text" IsHidden = "false" Position="0" Width="100" />
<Column Name="ParamortizedValue" HeaderDescription="Par/amortized value" HeaderName="paramortizedvalue" ColumnType="TableColumnType.Number" IsHidden = "false" Position="1" Width="200" />
<Column Name="PercentBondTotal" HeaderDescription="% of bond total" HeaderName="percentbondtotal" ColumnType="TableColumnType.Number" IsHidden = "false" Position="2" Width="150" />
</CurrencySummary>
<CallSchedule>
<Column Name="Calldate" HeaderDescription="Call date" HeaderName="Calldate" ColumnType="TableColumnType.Text" IsHidden = "false" Position="0" Width="100" />
<Column Name="Issue" HeaderDescription="Issue" HeaderName="Issue" ColumnType="TableColumnType.Text" IsHidden = "false" Position="1" Width="100" />
<Column Name="ParamortizedValue" HeaderDescription="Par/amortized value" HeaderName="paramortizedvalue" ColumnType="TableColumnType.Number" IsHidden = "false" Position="2" Width="200" />
<Column Name="PercentBondTotal" HeaderDescription="% of bond total" HeaderName="percentbondtotal" ColumnType="TableColumnType.Number" IsHidden = "false" Position="3" Width="150" />
</CallSchedule>
</TableDef>
试图通过以下方式实现这一目标:(编辑:根据亨克的建议)
var doc = XDocument.Load("TableDefinations.xml");
var cols = doc.Descendants("CurrencySummary").First();
foreach (var col in cols.Elements())
{
foreach (XAttribute at in col.Attributes())
{
//do something with the at.Name and at.Value
}
}
这是有效的方法还是有比这更好的方法?
I have the following xml that stores table definations. How can I loop through each column of the passed tablename (only one occurrence of each table) and their attributes using XDocument (C# 3.5)
Ex: If user passes CurrencySummary, I want to read each column and all it's attributes like HeaderDescription, HeaderName etc.
<?xml version="1.0" encoding="utf-8" ?>
<TableDef>
<CurrencySummary>
<Column Name="Currency" HeaderDescription="Currency" HeaderName="Currency" ColumnType="TableColumnType.Text" IsHidden = "false" Position="0" Width="100" />
<Column Name="ParamortizedValue" HeaderDescription="Par/amortized value" HeaderName="paramortizedvalue" ColumnType="TableColumnType.Number" IsHidden = "false" Position="1" Width="200" />
<Column Name="PercentBondTotal" HeaderDescription="% of bond total" HeaderName="percentbondtotal" ColumnType="TableColumnType.Number" IsHidden = "false" Position="2" Width="150" />
</CurrencySummary>
<CallSchedule>
<Column Name="Calldate" HeaderDescription="Call date" HeaderName="Calldate" ColumnType="TableColumnType.Text" IsHidden = "false" Position="0" Width="100" />
<Column Name="Issue" HeaderDescription="Issue" HeaderName="Issue" ColumnType="TableColumnType.Text" IsHidden = "false" Position="1" Width="100" />
<Column Name="ParamortizedValue" HeaderDescription="Par/amortized value" HeaderName="paramortizedvalue" ColumnType="TableColumnType.Number" IsHidden = "false" Position="2" Width="200" />
<Column Name="PercentBondTotal" HeaderDescription="% of bond total" HeaderName="percentbondtotal" ColumnType="TableColumnType.Number" IsHidden = "false" Position="3" Width="150" />
</CallSchedule>
</TableDef>
I am trying to achieve this by: (edited: as per Henk's suggestion)
var doc = XDocument.Load("TableDefinations.xml");
var cols = doc.Descendants("CurrencySummary").First();
foreach (var col in cols.Elements())
{
foreach (XAttribute at in col.Attributes())
{
//do something with the at.Name and at.Value
}
}
Is this is efficient way or if there is anything better than this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这在一定程度上取决于有多少
以及它们的位置是否重要。It depends a little on how many
<CurrencySummary>
s there are and if their place matters.编辑 OP不在.Net 4上,因此不使用
SelectMany
,如果Single()
不是.Net 4.0替换为First()
看起来应该只有一个
"CurrencySummary"
所以...将遍历唯一的CurrencySummary 元素中所有元素的所有属性,位于表定义.xml。
EDIT OP not on .Net 4, so not using
SelectMany
, ifSingle()
not .Net 4.0 substitute withFirst()
Looks like you there should be only one
"CurrencySummary"
so ...Will iterate through all the attributes of all the elements in the one and only CurrencySummary element, anywhere in the TableDefninations.xml.