简体   繁体   English

使用Linq从XML文件中读取属性/值对

[英]Read attribute/value pairs from XML file using Linq

I have an XML file that looks like this: 我有一个XML文件,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<response uri="/crm/private/xml/Leads/getRecords">
   <Leads>
      <row no="1">
         <FL val="LEADID">2000000022020</FL>
         <FL val="SMOWNERID">2000000018005</FL>
         <FL val="Lead Owner">John</FL>
         <FL val="Company">Zillium</FL>
         <FL val="First Name">Scott</FL>
         <FL val="Last Name">James</FL>
         <FL val="Designation">null</FL>
         <FL val="Email">null</FL>
         <FL val="Phone">null</FL>
         <FL val="Fax">null</FL>
         <FL val="Mobile">null</FL>
         <FL val="Website">null</FL>
         <FL val="Lead Source">null</FL>
         <FL val="Lead Status">null</FL>
         <FL val="No of Employees">0</FL>
         <FL val="Annual Revenue">0.0</FL>
      </row>
      <row no="2">
         <FL val="LEADID">2000000022020</FL>
         <FL val="SMOWNERID">2000000018005</FL>
         <FL val="Lead Owner">John</FL>
         <FL val="Company">Zillium</FL>
         <FL val="First Name">Scott</FL>
         <FL val="Last Name">James</FL>
         <FL val="Designation">null</FL>
         <FL val="Email">null</FL>
         <FL val="Phone">null</FL>
         <FL val="Fax">null</FL>
         <FL val="Mobile">null</FL>
         <FL val="Website">null</FL>
         <FL val="Lead Source">null</FL>
         <FL val="Lead Status">null</FL>
         <FL val="No of Employees">0</FL>
         <FL val="Annual Revenue">0.0</FL>
      </row>
   </Leads>
</response>

I am trying to read the "key/value" pairs in the row element, but I can't seem to get at grasp on how to do it using Linq. 我试图读取行元素中的“键/值”对,但我似乎无法掌握如何使用Linq来做到这一点。

I need to "de-serialize" the data into a simple POCO 我需要将数据“反序列化”为一个简单的POCO

public class Leads
{
    public long LEADID { get; set; }
    public long SMOWNERID { get; set; }
    public string LeadOwner { get; set; }
    public string Company { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Designation { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string Fax { get; set; }
    public string Mobile { get; set; }
    public string Website { get; set; }
    public string LeadSource { get; set; }
    public string LeadStatus { get; set; }
    public int NoOfEmployees { get; set; }
    public decimal AnnualRevenue { get; set; }
}

My problem is, that all the elements have the same name so I need to get the attribut value (val) paired with the element value. 我的问题是,所有元素都具有相同的名称,因此我需要获得与元素值配对的attribut值(val)。

So my question is, is there a smart way to do this using Linq. 所以我的问题是,有没有一种聪明的方法来使用Linq做到这一点。

Otherwise my workaround would be to write an XLS and transform the XML into a more de-serializeable XML format. 否则我的解决方法是编写XLS并将XML转换为更可反序列化的XML格式。

You can select each value like 您可以选择每个值

let company = (string)r.Elements()
                       .Single(x => (string)x.Attribute("val") == "Company")

But I believe that projecting all elements into dictionary will work faster (and is much more readable) 但我相信将所有元素投射到字典中会更快(并且更易读)

var query = 
     xdoc.Descendants("row")
         .Select(r => r.Elements().ToDictionary(f => (string)f.Attribute("val")))
         .Select(d => new Leads {
             LEADID = (long)d["LEADID"],
             SMOWNERID = (long)d["SMOWNERID"],
             Company = (string)d["Company"]
             // etc
         });

Also keep in mind you need to use long for first two properties (according to values in sample xml). 另外请记住,您需要使用long来获得前两个属性(根据示例xml中的值)。

var xdoc = XDocument.Parse(xmlstring);
xdoc.Elements("response").Elements("Leads").Elements("row").Select(l => new Leads() 
{
    LEADID = (long)l.Elements("FL").Where(fl => (string)fl.Attribute("val") == "LEADID").FirstOrDefault(),
    SMOWNERID = (long)l.Elements("FL").Where(fl => (string)fl.Attribute("val") == "SMOWNERID").FirstOrDefault(),
    etc..
});

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

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