如何将datatable转换为xml? [英] How to convert datatable to xml?

查看:113
本文介绍了如何将datatable转换为xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使有很多例子,我找不到我需要的东西?我的问题只不过是标题如何将datatable转换为xml...



虽然可以在任何技术中提出解决方案,但是我有一个起点还有,请看看这段代码。

  Dim xmlDoc As New XDocument(
New XElement(Class ,
从row In dt.AsEnumerable()
选择新的XElement(PUPIL,
新的XAttribute(FIRSTNAME,row.Field(Of String)(First Name) ),
新建XAttribute(LASTNAME,row.Field(Of String)(Last Name)),
新建XAttribute(DOB,row.Field(Of String) ))
新建XAttribute(YEAR,row.Field(Of String)(Year)),
新建XAttribute(SEX,row.Field(Of String) )),
新的XAttribute(CLASSNAME,row.Field(Of String)(Reg))
)))))

在这个例子中,我需要知道有多少列和列的名称来进行转换。我讨厌将列名称编码到Linq中,是否可以在Linq中使用任意数量的列和列名?



如果有人可以建议我改进Linq来处理数据表中任何非常棒的列。



感谢alot。



LINQ解决方案...

  Dim xmlDoc As New XDocument(
From row In dt.Rows
选择新XElement(pupil,
从列In dt.Columns
选择
新的XAttribute(column.ToString,row.Item(column.ToString))))

对于Datatable.WriteXML解决方案,请参阅Habib的帖子

解决方案

而不是使用LINQ,您可以使用: DataTable.WriteXML();

  DataTable dt = new DataTable(); 
dt.WriteXml(myxmlfile.xml);

如果要根据模式读取和写入数据,请执行以下步骤:




  • 我相信你在DataTable中有数据,你可能还有一个xml的数据,在visual studio中打开这个XML,使用 - >在菜单中生成模式选择 - > XML - >创建模式

  • 这将生成一个模式文件,保存该模式文件,例如test1.xsd

  • 尝试以下代码

      DataSet ds = new DataSet(); 
    ds.ReadXmlSchema(test1.xsd); //这将根据tes1.xsd schema
    ds.ReadXml(test.xml)读取和准备数据集; //这将读取XML,它应该是符合架构的
    ds.WriteXml(test7.xml); //这将根据架构编写新的XML

    在您的情况下,您可以根据需要修改架构,然后可以使用XML模式从Dataset生成文档到XML。您必须先将datatable保存在数据集中。 (我不太确定这个方法,但由于我没有看到这个问题的新回复,我以为可能对你有所帮助)



even though there are many examples out there, i can't find what I need? My question is simply just as the title "How to convert datatable to xml" ...

Although, you can suggest solution in any technology, but i have a starting point as well, please have a look at this code.

    Dim xmlDoc As New XDocument(
                    New XElement("Class",                                   
                                From row In dt.AsEnumerable()
                                Select New XElement("PUPIL",
                                    New XAttribute("FIRSTNAME", row.Field(Of String)("First Name")),
                                    New XAttribute("LASTNAME", row.Field(Of String)("Last Name")),
                                    New XAttribute("DOB", row.Field(Of String)("DoB")),
                                    New XAttribute("YEAR", row.Field(Of String)("Year")),
                                    New XAttribute("SEX", row.Field(Of String)("Gender")),
                                    New XAttribute("CLASSNAME", row.Field(Of String)("Reg"))
                                )))))

In this example, I need to know how many columns and the name of the columns to do the convertion. I hate to hardcode the columns name into that Linq, is it possible to do in Linq with any number of columns and column names ??

If someone can just suggest me to improve that Linq to handle any columns from the datatable that would be great.

Thanks alot.

LINQ Solution ...

    Dim xmlDoc As New XDocument(                       
         From row In dt.Rows
         Select New XElement("Pupil",
         From column In dt.Columns
         Select
         New XAttribute(column.ToString, row.Item(column.ToString))))

For Datatable.WriteXML solution, please refer to Habib's post

解决方案

Instead of LINQ you can use: DataTable.WriteXML();

        DataTable dt = new DataTable();
        dt.WriteXml("myxmlfile.xml");

If you want to read and write data according to Schema do the following step:

  • I believe you have data in DataTable, You probably also have an xml for the data, open that XML in visual studio, Generate Schema using -> In the Menu Select -> XML -> Create Schema
  • This will generate a schema file, Save that schema file e.g test1.xsd
  • Try the following code

    DataSet ds = new DataSet();
    ds.ReadXmlSchema("test1.xsd"); //This will read and prepare dataset according to tes1.xsd schema
    ds.ReadXml("test.xml"); // This will read the XML and it should be schema compliant 
    ds.WriteXml("test7.xml");//This will write the new XML according to schema
    

    In your case you can modify the schema to your need and then you can use the XML Schema to generate documents from Dataset to XML. You have to save the datatable in the dataset first. (I am not too sure about this approach, but since I have seen no new replies to this question, I thought it may be helpful to you)

这篇关于如何将datatable转换为xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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