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

本文关键字:结构 文件 xml 数据表 | 更新日期: 2023-09-27 18:16:49

Q:

根据用户的选择,我将从数据库中得到一组数据表。

我有三个数据表:Teachers, Subjects, ClassRooms

和我想要一个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>

现在如何生成具有先前结构的xml文件并从数据表填充xml文件数据?

,

<teachers options="" columns="id,name,short">
<teacher id="a" name="jhon" short="jo"/>
<teacher id="b" name="philips" short="Ph"/>
<teacher id="c" name="sara" short="Sa"/>
</teachers>

From teacher datatable .

请尽可能用示例详细说明答案。

<标题>编辑:
 private static void ConvertDataTableToXML(string schemaFile, DataTable dtTeacher, DataTable dtSubject, DataTable dtClassRoom)
        {
            XDocument doc = new XDocument();
            //Read the teachers element from xml schema file
            XElement teachers = XDocument.Load(schemaFile).Descendants("teachers").SingleOrDefault();
            XElement subjects = XDocument.Load(schemaFile).Descendants("subjects").SingleOrDefault();
            XElement classes = XDocument.Load(schemaFile).Descendants("classrooms").SingleOrDefault();
            if (teachers != null)
            {
                foreach (DataRow row in dtTeacher.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);
                }
            }
            if (subjects != null)
            {
                foreach (DataRow row in dtSubject.Rows)
                {
                    XElement subject = new XElement("subject");
                    subject.SetAttributeValue("id", row["num"]);
                    subject.SetAttributeValue("name", row["name"]);
                    subject.SetAttributeValue("short", row["name"].ToString().Substring(0, 2));
                    subjects.Add(subject);
                }
            }
            if (classes != null)
            {
                foreach (DataRow row in dtClassRoom.Rows)
                {
                    XElement cls = new XElement("classroom");
                    cls.SetAttributeValue("id", row["id"]);
                    cls.SetAttributeValue("name", row["name"]);
                    cls.SetAttributeValue("short", row["name"].ToString().Substring(0, 2));
                    classes.Add(cls);
                }
            }
            XElement xml = new XElement("timetable",
                                 new XAttribute("importtype", "database"),
                                 new XAttribute("options", "idprefix:id"),teachers,subjects,classes
                         );
            doc.Add(xml);
            string dirPath = System.Web.HttpContext.Current.Server.MapPath("~/import");
            string targetFileName = Path.Combine(dirPath, "import.xml");
            int counter = 1;
            while (System.IO.File.Exists(targetFileName))
            {
                counter++;
                targetFileName = Path.Combine(dirPath,
                    "import" + counter.ToString() + ".xml");
            }
            doc.Save(targetFileName);
        }

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

您可以尝试使用Linq-XML来读取和更新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);
 }