Reputation: 436
I'm new to C# so this could be a simple answer. I have a List of XElements coming in from an API. I need to convert it to a datatable. How can I do that?
List of XElements:
<person><personid>1</personid><name>person1</name></person>
<person><personid>2</personid><name>person2</name></person>
I need it in the following data table format
PersonID | Name
--------------
1 person1
2 person2
Another thing is I dont know the nodes at design time. So the XElement could be of the format
<anyrootnode><anynumberofchildnodes/></anyrootnode>
Upvotes: 2
Views: 4724
Reputation: 1544
DataTable has a function named ReadXml which is used to read Xml.
string xml = XElements.ToString();
DataTable dt = new DataTable();
dt.ReadXml(new System.IO.StringReader(xml));
Upvotes: 4