首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用XElement的位置获取属性名

使用XElement的位置获取属性名
EN

Stack Overflow用户
提问于 2015-08-28 09:22:55
回答 2查看 234关注 0票数 0

数据定义文件中有以下XML:

代码语言:javascript
运行
复制
<PQTemplate documentID="CSTrlsEN" documentType="TransList" templateID="001" 
            templateType="Customer Copy" 
            templateName="C:\CPS\_templates\Mini-Statements\CSTrlsEN.doc">  
    <field pos="5" name="YPTME" descr="Time"  />
    <field pos="6" name="YPDTE" descr="Action Date"  />
    <field pos="7" name="YPBRNO" descr="Branch Number"  />
    <field pos="8" name="YPBNA" descr="Branch Name"  />
    <field pos="9" name="YPTID" descr="Teller ID"  />
    <field pos="10" name="YPISN" descr="Teller Sequence"  />
    <field pos="11" name="YPREF" descr="Customer Reference"  />
    <field pos="12" name="YPCUS" descr="Customer Name"  />
    <field pos="13" name="YPEAN" descr="Account Number"  />
    <field pos="14" name="YPATY" descr="Account Type"  />
    <field pos="15" name="YPCUR" descr="Currency"  />
    <field pos="16" name="YPBAL" descr="Available Balance"  />
</PQTemplate>

我想通过在字段子元素中添加name属性命名的列来构建数据表,但是我在使用LINQ检索信息时遇到了困难:

下面是我试图编写的代码,我想使用属性pos >5的属性名添加一个列名,所以第一列是YPTME,第二列是YPDTE,等等,直到第12和最后一列将是YPBAL.

代码语言:javascript
运行
复制
var mapInfo = from nm in XElement.Elements("PQTemplate").Elements("field")
                where (string)nm.Attribute("documentID") == sRequests[0] 
                select nm;

if (mapInfo != null)
{
    for (int iCol = 5; iCol < mapInfo.Count(); iCol++) 
    { 
        // there should be twelve
        string colName = mapInfo.Attributes(iCol);
        dt[0].Columns.Add(new DataColumn(colName, typeof(System.String)));
    }
}

我似乎无法正确地做到这一点,我如何才能做到这一点,我的行string colName = mapInfo.Attributes(iCol);哪里出错了?

编辑:

我也尝试过这样做,但似乎无法在for..next循环中应用索引:

代码语言:javascript
运行
复制
var mapInfo = from nm in XElement.Elements("PQTemplate").Elements("field")
            where (string)nm.Attribute("documentID") == sRequests[0] && Convert.ToInt32(nm.Attribute("pos").ToString())>=5 
            orderby Convert.ToInt32(nm.Attribute("pos").ToString())
            select nm;

if (mapInfo != null)
{
    for (int iDx=5; iDx<mapInfo.Count(); iDx++)
    {
    string colName=mapInfo[iDx];
    dt[0].Columns.Add(new DataColumn(colName, typeof(System.String)));
            }
    }

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-28 09:53:30

尝尝这个

代码语言:javascript
运行
复制
        XElement rootElement = XElement.Parse(stringXml);
        DataTable dt = new DataTable();
        if (rootElement.Attribute("documentID").Value == "CSTrlsEN")
        {
             var colNames = from field in rootElement.Elements("field")
                          where Convert.ToInt32(field.Attribute("pos").Value) >= 5
                          select field.Attribute("name").Value;

             foreach (var name in colNames)
             {
                 dt.Columns.Add(name, typeof(string));
             }
        }
票数 1
EN

Stack Overflow用户

发布于 2015-08-28 10:11:25

这应该适用于你:-

代码语言:javascript
运行
复制
 IEnumerable<string> columnNames = from nm in xdoc.Descendants("field")
                          where (string)nm.Parent.Attribute("documentID") == "CSTrlsEN"
                                              && (int)nm.Attribute("pos") >= 5
                          select (string)nm.Attribute("name");

或者,如果您想要对documentID进行验证,可以按照@codeninja的建议,在前面使用if块。

这将为您提供具有指定条件的所有列名,然后您可以简单地遍历它:-

代码语言:javascript
运行
复制
 foreach (var colName in columnNames)
 {
     dt[0].Columns.Add(new DataColumn(colName, typeof(System.String)));
 }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32267624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档