使用Linq从XML文件读取属性/值对 [英] Read attribute/value pairs from XML file using Linq

查看:85
本文介绍了使用Linq从XML文件读取属性/值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的XML文件:

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>

我正在尝试读取row元素中的键/值"对,但是我似乎无法掌握如何使用Linq进行操作.

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.

我需要将数据反序列化"为简单的POCO

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

我的问题是,所有元素都具有相同的名称,因此我需要将属性值(val)与元素值配对.

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.

所以我的问题是,有没有使用Linq做到这一点的聪明方法.

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

否则,我的解决方法是编写XLS并将XML转换为更可反序列化的XML格式.

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

推荐答案

您可以选择每个值

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

还请记住,您需要对前两个属性使用long(根据示例xml中的值).

Also keep in mind you need to use long for first two properties (according to values in sample xml).

这篇关于使用Linq从XML文件读取属性/值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆