I have an issue that I really need to solve. Any help or examples are greatly appreciated.
Problem
I need to take an xml string variable that comes from a database (it is not stored in an external xml file) and get it into a list of an object that I have created. The xml elements will match the properties in the class. My question, is do I need to serialize
that to an object before I can deserialize it and stuff it into my object or what is the approach? I have been searching for some examples online for the past few hours and haven't gotten very far. I don't have much experience with xml so any advice is welcome.
This is what my xml looks like coming out of the db record and into code. I have trimmed out the rest of the fields but you get the idea.
chanian
0 Points
1 Post
Need help getting XML string into a List
Dec 13, 2012 03:37 PM|LINK
Hello all--
I have an issue that I really need to solve. Any help or examples are greatly appreciated.
Problem
I need to take an xml string variable that comes from a database (it is not stored in an external xml file) and get it into a list of an object that I have created. The xml elements will match the properties in the class. My question, is do I need to serialize that to an object before I can deserialize it and stuff it into my object or what is the approach? I have been searching for some examples online for the past few hours and haven't gotten very far. I don't have much experience with xml so any advice is welcome.
This is what my xml looks like coming out of the db record and into code. I have trimmed out the rest of the fields but you get the idea.
<?xml version="1.0" encoding="utf-8"?><root><txtFirstName isRequired="False" friendlyName="First Name" type="text">Johan</txtFirstName><txtLastName isRequired="False" friendlyName="Last Name" type="text">Wilson</txtLastName></root>
Thank you.
DarrellNorto...
All-Star
87443 Points
9718 Posts
Moderator
MVP
Re: Need help getting XML string into a List
Dec 13, 2012 05:19 PM|LINK
Two easy ways I can think of:
You can use LINQ-to-XML: http://www.codeproject.com/Articles/24376/LINQ-to-XML
Or you can populate a DataTable with the XML string and then access the DataTable to build your object. http://msdn.microsoft.com/en-us/library/fx29c3yd(v=vs.110).aspx
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Need help getting XML string into a List
Dec 14, 2012 04:01 AM|LINK
Hi,
I think you can create an object class the have the public properties such as FirstName or "Lastname" and the fetch out the value in the xml file.
Now you can refer (if your xml isn't a file but a string, please use Parse).
XDocument doc = XDocument.Load("xxx.xml"); doc.Root.Element("txtFirstName").Value; ………………