且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-14 09:48:04

您可以选择每个值

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