如何在单个展平对象中读取嵌套XML [英] How do I read nested XML in single flatten object

查看:87
本文介绍了如何在单个展平对象中读取嵌套XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我必须读取嵌套的xml并保存到db但是问题是xml是嵌套的,当我用dataset.readxml()方法读取它时必须保存在单个表中它返回多个表,但我单表是否有任何方法可以做到这一点?我想要结果像



作者|联系|书|价格

测试| 155335852 | abc | 152

测试| 155335852 | xyz | 1523



样本xml



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

<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>







注意:xml是动态的,必需的标签会来从数据库中预定义的标签。

并希望将这种嵌套的xml保存在单个表中,所以我只想在单个数据表或对象列表中读取xml的解决方案。



我尝试过:



i尝试过DataSet.ReadXml();但它返回多个表,由于动态xml我不能在运行时加入它们。




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.

推荐答案

你可以在C#中以各种方式做到这一点但是你可能想要在每个对象上实现 IXmlSerializable



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 );
}





...并将进程从根到底级联。实现界面为您提供了一种非常精细的控制/调试过程的方式。你可以使用:



- XMLSerializer 或(这是首选的更好的工具)

- DataContractSerializer



此代码过于简单,无法在此处发布。但是你应该寻找序列化树或多树(图)结构的解决方案,你应该找到一些样本,让你对这个结构有更详细的了解。



...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.


这篇关于如何在单个展平对象中读取嵌套XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆