How can I Convert this xml Records object into List<dp_donorsearch>. My current code is below but I want to find best way to do this..... any idea?
<result>
<record>
<field name="donor_id" id="donor_id" value="9879" />
<field name="first_name" id="first_name" value="Trix5647" />
<field name="last_name" id="last_name" value="Rabbit657" />
<field name="email" id="email" value="[email protected]" />
<field name="business_phone" id="business_phone" value="" />
<field name="mobile_phone" id="mobile_phone" value="" />
<field name="home_phone" id="home_phone" value="" />
<field name="address" id="address" value="Street S.W. " />
<field name="address2" id="address2" value="" />
<field name="city" id="city" value="Quaker" />
<field name="state" id="state" value="PA" />
<field name="zip" id="zip" value="1234" />
<field name="country" id="country" value="USA" />
</record>
</result>
C# Code
public class dp_donorsearch
{
public string donor_id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string email { get; set; }
public string business_phone { get; set; }
public string mobile_phone { get; set; }
public string home_phone { get; set; }
public string address { get; set; }
public string address2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string zip { get; set; }
public string country { get; set; }
public static List<dp_donorsearch> Load(string url)
{
var list = new List<dp_donorsearch>();
var xmlDoc = new XmlDocument();
xmlDoc.Load(url);
list.AddRange(from XmlNode record in xmlDoc.SelectNodes("result/record")
select new dp_donorsearch
{
donor_id = ReadField(record, "donor_id"),
first_name = ReadField(record, "first_name"),
last_name = ReadField(record, "last_name"),
email = ReadField(record, "email"),
business_phone = ReadField(record, "business_phone"),
mobile_phone = ReadField(record, "mobile_phone"),
home_phone = ReadField(record, "home_phone"),
address = ReadField(record, "address"),
address2 = ReadField(record, "address2"),
city = ReadField(record, "city"),
state = ReadField(record, "state"),
zip = ReadField(record, "zip"),
country = ReadField(record, "country")
});
return list;
}
private static string ReadField(XmlNode node, string nodeName)
{
var selectSingleNode = node.SelectSingleNode("field[@name = '" + nodeName + "']");
if (selectSingleNode != null && selectSingleNode.Attributes != null)
return selectSingleNode.Attributes["value"].Value;
return string.Empty;
}
}