简体   繁体   English

从数据库数据集和生成的XSD类C#创建XML

[英]Create XML from Database Dataset and Generated XSD Class C#

Good day, I need some help, I have a dataset which I populate from my db, I also have a xsd Schema file I need to create an xml file using the data from the db and the xsd file. 美好的一天,我需要一些帮助,我有一个从数据库填充的数据集,我还有一个xsd Schema文件,我需要使用数据库和xsd文件中的数据来创建xml文件。

Can anyone please help me. 谁能帮帮我吗。

All the best 祝一切顺利

You can cross reference the database names with the datatable names in you Select Query by using "as" : "Select abc as xyz from table1". 您可以在选择查询中使用“ as”来交叉引用数据库名称和数据表名称:“从table1中选择abc作为xyz”。 "abc" is the database name and xyz is the datatable name which will be the xml tag name. “ abc”是数据库名称,xyz是数据表名称,它将是xml标记名称。

Here is an example of writing a DataSet 这是编写数据集的示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Data;

namespace ConsoleApplication34
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            DataTable dt = new DataTable("MyTable");

            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Id", typeof(int));

            dt.Rows.Add(new object[] { "John", 1});
            dt.Rows.Add(new object[] { "Mary", 2});
            dt.Rows.Add(new object[] { "Dick", 3});
            dt.Rows.Add(new object[] { "Harry", 4});
            dt.Rows.Add(new object[] { "Jane", 5});

            DataSet ds = new DataSet("MySet");
            ds.Tables.Add(dt);

            ds.WriteXml(FILENAME, XmlWriteMode.WriteSchema);
            // or
            //ds.WriteXml(FILENAME);

        }

    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM