Reputation: 7830
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;
}
}
Upvotes: 0
Views: 59
Reputation: 1038
Here's how you deserialize it into your class structure
Reference xml namespace
using System.Xml;
using System.Xml.Serialization;
deserialize
string input =
"<result>\n" +
"<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>";
XmlSerializer serializer = new XmlSerializer(typeof(Result));
Result result = serializer.Deserialize(new System.IO.StringReader(input)) as Result;
Class definition
[XmlRoot("result")]
public class Result
{
[XmlElement("record")]
public Record[] Records { get; set; }
}
public class Record
{
[XmlElement("field")]
public Field[] Fields { get; set; }
}
public class Field
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("id")]
public string Id { get; set; }
[XmlAttribute("value")]
public string Value { get; set; }
}
Upvotes: 2
Reputation: 34421
A solution more like your original
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string input =
"<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>" +
"<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>";
List<dp_donorsearch> list = dp_donorsearch.Load(input);
}
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 input)
{
List<dp_donorsearch> list = new List<dp_donorsearch>();
XDocument doc = XDocument.Parse(input);
var listDict = doc.Descendants("record").AsEnumerable()
.Select(x => new
{
dict = x.Descendants("field")
.GroupBy(y => y.Attribute("name").Value, z => z.Attribute("value").Value)
.ToDictionary(y => y.Key, z => z.FirstOrDefault())
});
foreach(var dict in listDict)
{
dp_donorsearch newDp = new dp_donorsearch();
list.Add(newDp);
newDp.donor_id = dict.dict["donor_id"];
newDp.first_name = dict.dict["first_name"];
newDp.last_name = dict.dict["last_name"];
newDp.email = dict.dict["email"];
newDp.business_phone = dict.dict["business_phone"];
newDp.mobile_phone = dict.dict["mobile_phone"];
newDp.home_phone = dict.dict["home_phone"];
newDp.address = dict.dict["address"];
newDp.address2 = dict.dict["address2"];
newDp.city = dict.dict["city"];
newDp.state = dict.dict["state"];
newDp.zip = dict.dict["zip"];
newDp.country = dict.dict["country"];
}
return list;
}
}
}
}
Upvotes: 0
Reputation: 34421
Here is an interesting solution
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string input =
"<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>" +
"<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>";
XDocument doc = XDocument.Parse(input);
var listDict = doc.Descendants("record").AsEnumerable()
.Select(x => x.Descendants("field")
.GroupBy(y => y.Attribute("name").Value, z => z.Attribute("value").Value)
.ToDictionary(y => y.Key, z => z.FirstOrDefault())
.ToList());
}
}
}
Upvotes: 0