Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
I have to read nested xml and save into db but the problem is the xml is nested and have to save in single table when i read it with dataset.readxml() method it returns multiple tables but i single table is there any way to do this? i want result like

author | contact | book | Price
test |155335852| abc | 152
test |155335852| xyz | 1523

sample xml

XML
<bookinfo>
  <author>
   <name>test</name>
   <contact>155335852</contact>
  </author>
 <books>
  <book>
   <title>abc</book>
   <price>152</price>
 </book>
 <book>
  <title>xyz</book>
  <price>1523</price>
  </book>
 </books>
</bookinfo>



Note: the xml is dynamic and required tags will come from pre defined tags in database.
and want to save this kind of nested xml in single table so i just want the solution for read xml in single datatable or list of objects.

What I have tried:

i have tried DataSet.ReadXml(); but it returns multiple table and due to dynamic xml i cannot join them on runtime.
Posted
Updated 14-May-18 20:49pm

1 solution

You can do this in a variety of ways in C# but you probably want to implement IXmlSerializable on each object:

public interface IXmlSerializable
{
  XmlSchema GetSchema ();
  void ReadXml ( XmlReader reader );
  void WriteXml ( XmlWriter writer );
}


...and cascade the process from root to bottom. Implementing the interface gives you a very fine grained way of controlling/debugging the process. You can either use:

- XMLSerializer or (this is the prefered and better tool)
- DataContractSerializer

The code for this is too involved to post it here. But you should look for solutions that serialize tree or multi tree (graph) structures and you should find a few samples that give you a more detailed idea on this one.
 
Share this answer
 
Comments
Dirk Bahle 15-May-18 3:22am    
DataContractSerializer is the better solution since its much newer etc... (just to be clear)
abid zahid 15-May-18 4:01am    
thanks for the help if you can give some code example it would be more appreciable.

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