简体   繁体   English

使用 XDocument 循环遍历节点的所有属性

[英]Loop through all the attributes of a node using XDocument

I have the following xml that stores table definations.我有以下 xml 存储表定义。 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)如何使用 XDocument (C# 3.5) 遍历传递的表名的每一列(每个表只出现一次)及其属性

Ex: If user passes CurrencySummary, I want to read each column and all it's attributes like HeaderDescription, HeaderName etc.例如:如果用户通过 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>

I am trying to achieve this by: (edited: as per Henk's suggestion)我试图通过以下方式实现这一目标:(编辑:根据 Henk 的建议)

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?这是有效的方法还是有比这更好的方法?

It depends a little on how many <CurrencySummary> s there are and if their place matters.这在一定程度上取决于有多少<CurrencySummary>以及它们的位置是否重要。

var summ = doc.Descendants("CurrencySummary").First();

foreach (var col in summ.Elements())
   ...

EDIT OP not on.Net 4, so not using SelectMany , if Single() not.Net 4.0 substitute with First() EDIT OP not on.Net 4,所以不使用SelectMany ,如果Single() not.Net 4.0 用First()替代

Looks like you there should be only one "CurrencySummary" so...看起来你应该只有一个"CurrencySummary"所以......

var doc = XDocument.Load("TableDefinations.xml");
foreach(XElement col in doc.Descendants("CurrencySummary").Single().Elements())
{
    foreach(XAttribute at in col.Attributes())
    {
        at...
    }
}

Will iterate through all the attributes of all the elements in the one and only CurrencySummary element, anywhere in the TableDefninations.xml.将遍历 TableDefninations.xml 中任何位置的唯一 CurrencySummary 元素中所有元素的所有属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM