Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从数据表生成具有特定结构的xml文件?_C#_Asp.net_Xml_Linq_Linq To Xml - Fatal编程技术网

C# 如何从数据表生成具有特定结构的xml文件?

C# 如何从数据表生成具有特定结构的xml文件?,c#,asp.net,xml,linq,linq-to-xml,C#,Asp.net,Xml,Linq,Linq To Xml,问: 根据用户的一些选择,我将从数据库中获取一组数据表 我有三个数据表:教师,科目,教室 我想要一个具有以下结构的xml文件: <timetable importtype="database" options="idprefix:id"> <teachers options="" columns="id,name,short"> </teachers> <subject

问:

根据用户的一些选择,我将从数据库中获取一组数据表


我有三个数据表:
教师
科目
教室

我想要一个具有以下结构的xml文件:

<timetable importtype="database" options="idprefix:id">

<teachers options="" columns="id,name,short">

</teachers>

<subjects options="" columns="id,name,short">

</subjects>

<classrooms options="" columns="id,name,short">

</classrooms>

</timetable>

您可以尝试使用LINQXML读取和更新XML文档

请看一看示例:

//Read the teachers element from xml schema file
XElement teachers = XDocument.Load(schemaFile).Descendants("teachers").SingleOrDefault();

if (teachers != null)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("id");
    dt.Columns.Add("name");


    dt.Rows.Add("1", "john");
    dt.Rows.Add("2", "philips");
    dt.Rows.Add("3", "sara");

    XDocument doc = new XDocument();
    foreach (DataRow row in dt.Rows)
    {
        XElement teacher = new XElement("teacher");
        teacher.SetAttributeValue("id", row["id"]);
        teacher.SetAttributeValue("name", row["name"]);
        teacher.SetAttributeValue("short", row["name"].ToString().Substring(0,2));

        teachers.Add(teacher);
    }
    doc.Add(teachers);
    doc.Save(newFilename);
 }

您可以尝试使用LINQXML读取和更新XML文档

请看一看示例:

//Read the teachers element from xml schema file
XElement teachers = XDocument.Load(schemaFile).Descendants("teachers").SingleOrDefault();

if (teachers != null)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("id");
    dt.Columns.Add("name");


    dt.Rows.Add("1", "john");
    dt.Rows.Add("2", "philips");
    dt.Rows.Add("3", "sara");

    XDocument doc = new XDocument();
    foreach (DataRow row in dt.Rows)
    {
        XElement teacher = new XElement("teacher");
        teacher.SetAttributeValue("id", row["id"]);
        teacher.SetAttributeValue("name", row["name"]);
        teacher.SetAttributeValue("short", row["name"].ToString().Substring(0,2));

        teachers.Add(teacher);
    }
    doc.Add(teachers);
    doc.Save(newFilename);
 }

嗯,你不理解这个问题。我不想读xml文件,我想从
数据表
生成具有特定模式的xml文件。我有三个数据表:
教师
科目
教室
。从这些datarables中,我想用以前的模式生成一个xml文件。我可以自定义datatable,但至少有两列
(id,name)
很好,但是新文件变成
我的模式的其余部分在哪里?我指的是
根元素
(主题,教室)
即使是空的,我也希望得到这样的结果:
嗯,你没听懂这个问题。我不想读xml文件,我想从
数据表
生成具有特定模式的xml文件。我有三个数据表:
教师
科目
教室
。从这些datarables中,我想用以前的模式生成一个xml文件。我可以自定义datatable,但至少有两列
(id,name)
很好,但是新文件变成
我的模式的其余部分在哪里?我指的是
根元素
(主题,教室)
即使是空的,我也希望得到这样的结果:
//Read the teachers element from xml schema file
XElement teachers = XDocument.Load(schemaFile).Descendants("teachers").SingleOrDefault();

if (teachers != null)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("id");
    dt.Columns.Add("name");


    dt.Rows.Add("1", "john");
    dt.Rows.Add("2", "philips");
    dt.Rows.Add("3", "sara");

    XDocument doc = new XDocument();
    foreach (DataRow row in dt.Rows)
    {
        XElement teacher = new XElement("teacher");
        teacher.SetAttributeValue("id", row["id"]);
        teacher.SetAttributeValue("name", row["name"]);
        teacher.SetAttributeValue("short", row["name"].ToString().Substring(0,2));

        teachers.Add(teacher);
    }
    doc.Add(teachers);
    doc.Save(newFilename);
 }