Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

How to convert a XMl File to DataTable using C#?

I have Xml file
----------------

XML
<Destination Type="Country" ID="CN">
<Item Level="1" Title="Executive Summary" ID="DG-IG-EXECSUM">sample Text</Item>
</Destination>


In C# Code:
------------
DataSet xmlDS = new DataSet();
stream = new StringReader(xmlData);
// Load the XmlTextReader from the stream
reader = new XmlTextReader(stream);
xmlDS.ReadXml (xmlData,XmlReadMode.ReadSchema);

in dataset i got 'Item' table like this
Level Title ID
1 Executive Summary DG-IG-EXECSUM

I need output like this

Level Title ID Value
1 Executive Summary DG-IG-EXECSUM sample Text


please guide me how to implement it.
Posted
Updated 21-Sep-15 2:22am
v2

1 solution

Hi,

You can use below code for adding the value to the node.

C#
XmlNode node = doc.SelectSingleNode("/Destination/Item");

            XmlAttribute xKey = doc.CreateAttribute("Value");
            xKey.Value = node.InnerText;

            node.Attributes.Append(xKey);


Try this and let me know if you are facing any issues.

Thanks,
Sisir Patro
 
Share this answer
 
Comments
vulisiramu 23-Sep-15 2:50am    
Thanks, it is working
[no name] 23-Sep-15 4:38am    
Cool... :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900