c# - Read attribute/value pairs from XML file using Linq -
c# - Read attribute/value pairs from XML file using Linq -
i have xml file looks 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 trying read "key/value" pairs in row element, can't seem @ grasp on how using linq.
i need "de-serialize" info 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, elements have same name need attribut value (val) paired element value.
so question is, there smart way using linq.
otherwise workaround write xls , transform xml more de-serializeable xml format.
you can select each value
let company = (string)r.elements() .single(x => (string)x.attribute("val") == "company")
but believe projecting elements dictionary work faster (and 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 maintain in mind need utilize long
first 2 properties (according values in sample xml).
c# xml linq-to-xml deserialization
Comments
Post a Comment