Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Dec 14, 2012 04:01 AM by Decker Dong - MSFT
Dec 13, 2012 03:37 PM|LINK
I have an issue that I really need to solve. Any help or examples are greatly appreciated.
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>
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
Dec 14, 2012 04:01 AM|LINK
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");