简体   繁体   中英

How to generate xml file with specific structure from datatables?

Q :

According to some choices of the user, i will get set of Datatables from the database.

i have three datatabels: Teachers , Subjects , ClassRooms

and I want an xml file with the following structure:

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

Now How to generate xml file with the previous structure and fill the xml file data from the data tables?

ex:

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

please details answer with sample example as possible.

EDIT :

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

You may try to read and update XML document using Linq-XML.

Have a look at sample:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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