1

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

1 Answer 1

1

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);
 }
Sign up to request clarification or add additional context in comments.

5 Comments

hmmmm , U miss understood the question.I don't want to read xml file , i want to generate xml file with specific schema from datatables?
i have three datatabels:Teachers,Subjects,ClassRooms. From these datarables ,i wanna to generate an xml file with the previous schema.
I can customize the datatable but at least two columns (id,name)
Great , but the new file becomes <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> where is the rest of my schema I mean the root element and (subject,classroom ) even empty
I want the result like this: <timetable importtype="database" options="idprefix:id"> <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> <subjects options="" columns="id,name,short"> </subjects> <classrooms options="" columns="id,name,short"> </classrooms> </timetable>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.