简体   繁体   English

c#LINQ从XElement获取属性值

[英]c# LINQ get Attribute value from XElement

I have the below XML in a data definitions file: 我在数据定义文件中包含以下XML:

<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"  />

I get that specific XElement using LINQ, extracting it from an XML file that contains several PQTemplate elements by using the below LINQ Expression: 我使用LINQ获得了特定的XElement,并使用下面的LINQ表达式从包含几个PQTemplate元素的XML文件中提取了它:

var mapInfo = from nm in XElement.Elements("PQTemplate")
                where (string)nm.Attribute("documentID") == sRequests[0].Split('\t')[0] 
                select nm;    

Now I need to get the value of the attribute documentType so I tried the below LINQ Expression: 现在,我需要获取属性documentType的值,因此尝试了以下LINQ表达式:

var repName = from d in mapInfo.Attributes("documentType")
                     select d.Value;

reportName = repName.ToString();

Unfortunately although I can see the value TransList is part of the reportName element, I have had no luck trying to retrieve it. 不幸的是,尽管我看到TransList值是reportName元素的一部分,但是我没有运气尝试检索它。

Here is an image showing it in VS 2013: 这是在VS 2013中显示的图片:

在此处输入图片说明

so how can I get the documentType attribute in the element? 那么如何获取元素中的documentType属性呢?

That's because repName will return an IEnumerable<string> for all the mapInfo . 这是因为repName将返回IEnumerable<string>所有mapInfo

IEnumerable<string> repName = from d in mapInfo.Attributes("documentType")
                     select d.Value;

So either use a foreach loop if you suspect you may get more values or use First to get first attribute like this:- 因此,如果怀疑可能会得到更多的值,请使用foreach循环,或者使用First来获取第一个属性,例如:

string reportName = mapInfo.First().Attribute("documentType").Value;

Linq queries return collections. Linq查询返回集合。 Do for each over repName or for each repName

repName.First().ToString()

if that is all you need. 如果这就是您所需要的。

Your solution depends on how many elements DocumentType exist in your XML. 您的解决方案取决于XML中存在DocumentType元素数量。 If it´s only one (what I suppose) you may use repName.First().ToString() . 如果只有一个(我想),则可以使用repName.First().ToString()

If the attribute may occure more than once you should use a loop instead: 如果该属性可能出现多次,则应改为使用循环:

var result = new List<string>();
foreach(var a in (from d in mapInfo.Attributes("documentType") select d.Value) 
    result.Add(a.ToString());

Or even shorter: 甚至更短:

result = mapInfo.Attributes("documentType").Select(x => x.Value.ToString());

Which will return an enumeration. 这将返回一个枚举。

Change 更改

var mapInfo = from nm in XElement.Elements("PQTemplate")
                where (string)nm.Attribute("documentID") == sRequests[0].Split('\t')[0] 
                select nm;   

to

var mapInfo = from nm in XElement.Elements("PQTemplate")
                where (string)nm.Attribute("documentID") == sRequests[0].Split('\t')[0] 
                select nm.Attribute("documentType").Value;   

then mapInfo.First() will give you the value you want. 然后mapInfo.First()将为您提供所需的值。

To get a single value out of a LINQ query you have to call for example First or FirstOrDefault . 要从LINQ查询中获取单个值,您必须调用例如FirstFirstOrDefault If you call FirstOrDefault it won't throw an exception if the query returns no matches. 如果调用FirstOrDefault ,则在查询未返回任何匹配FirstOrDefault它不会引发异常。

string repName =  doc.Elements("PQTemplate")
                     .Where(e => (string)a.Attribute("documentID") == sRequests[0].Split('\t')[0])
                     .Select(e => (string)e.Attribute("documentType"))
                     .FirstOrDefault();

Also, you don't need to call ToString() on XAttribute.Value as it's already a string . 另外,您不需要在XAttribute.Value上调用ToString() ,因为它已经是string

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

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