简体   繁体   中英

Read attribute/value pairs from XML file using Linq

I have an XML file that looks like this:

<?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.

I need to "de-serialize" the data into a simple 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.

So my question is, is there a smart way to do this using Linq.

Otherwise my workaround would be to write an XLS and transform the XML into a more de-serializeable XML format.

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).

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..
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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