如何基于特定的XML元素创建XML文件 [英] How do I create an XML file based on specific XML elements

查看:110
本文介绍了如何基于特定的XML元素创建XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下生成xml文件的代码;节点不在下面是代码和预期的结果也添加让我知道我做错了什么基于预期的结果和结果我得到我从gridview结果下面的数据

I have the following code that generate an xml file however; the nodes are not right below is the code and the expected results is also added let me know what i am doing wrong based on the expected results and the result i got i am getting the data from the gridview result below

EmployeeID date    Hours   JobTitleCode    PayTypeCode
     101631    6/12/2017   7.5            10               3
     101631    6/13/2017   7.5            10               3
     103673    6/18/2017   12.75               9               3





我的尝试:





What I have tried:

try {

            DataSet ds = new DataSet();
            DataTable dtxml = (DataTable)ViewState["Data"];
            ds.Tables.Add(dtxml);

            //Create a new XML doc
            XmlDocument xmlDoc = new XmlDocument();
            string dt = Txtendhour.Text.Replace("/", "");


            // Write down the XML declaration
            //Server.MapPath("directory.xml")
            if (File.Exists(Server.MapPath("PPE" + dt + ".xml")))
            {
                File.Delete(Server.MapPath("PPE" + dt + ".xml"));
            }

            XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "ASCII", null);
            // Create the root element
            XmlElement rootNode = xmlDoc.CreateElement("companyData");
            rootNode.SetAttribute("xmlns", "");
            xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
            xmlDoc.AppendChild(rootNode);

            XmlElement headernode = xmlDoc.CreateElement("header");
            headernode.SetAttribute("fileSpecVersion", "2.00.0");
            xmlDoc.DocumentElement.PrependChild(headernode);
            // Create the required nodes
            XmlElement mainNode = xmlDoc.CreateElement("companyId");
            XmlElement descNode = xmlDoc.CreateElement("stateCode");
            XmlElement activeNode = xmlDoc.CreateElement("reportQuarter");
            XmlElement fiscalYear = xmlDoc.CreateElement("federalFiscalYear");
            XmlElement Vendor = xmlDoc.CreateElement("softwareVendorName");
            XmlElement version = xmlDoc.CreateElement("softwareProductName");
            XmlElement vendorversion = xmlDoc.CreateElement("softwareProductVersion");

            // retrieve the text
            string faccode = txtcode.Text;
            string quarter = GridView1.Rows[0].Cells[7].Text;
            XmlText categoryText = xmlDoc.CreateTextNode(faccode);
            XmlText descText = xmlDoc.CreateTextNode("State");
            XmlText activeText = xmlDoc.CreateTextNode(quarter);
            XmlText fiscalYearactiveText = xmlDoc.CreateTextNode("2017");
            XmlText activetext2 = xmlDoc.CreateTextNode("softwarename");
            XmlText activetext3 = xmlDoc.CreateTextNode("Softwaretype");
            XmlText activetext4 = xmlDoc.CreateTextNode("VersionNumber");

            // append the nodes to the parentNode without the value
            headernode.AppendChild(mainNode);
            headernode.AppendChild(descNode);
            headernode.AppendChild(activeNode);
            headernode.AppendChild(fiscalYear);
            headernode.AppendChild(Vendor);
            headernode.AppendChild(version);
            headernode.AppendChild(vendorversion);
            // save the value of the fields into the nodes
            mainNode.AppendChild(categoryText);
            descNode.AppendChild(descText);
            activeNode.AppendChild(activeText);
            fiscalYear.AppendChild(fiscalYearactiveText);
            Vendor.AppendChild(activetext2);
            version.AppendChild(activetext3);
            vendorversion.AppendChild(activetext4);

            // Create a new <Employees> element and add it to the root node
            XmlElement Employees = xmlDoc.CreateElement("employees");
            xmlDoc.DocumentElement.AppendChild(Employees);

            XmlElement parentNode = xmlDoc.CreateElement("EmployeeHours");

            // Set attribute name and value!
            parentNode.SetAttribute("processType", "merge");
            // xmlDoc.DocumentElement.PrependChild(parentNode);

            string id = "";
            string employeeCode = GridView1.Rows[0].Cells[0].Text;



            string fromFormat = "MM/dd/yyyy";
            string toFormat = "yyyy-MM-dd";
            foreach (GridViewRow row in GridView1.Rows)
            {
                //first part of EMPLOYEES ELEMENTS AND CHILD ELEMENTS

                if (id != row.Cells[0].Text)
                {
                    XmlElement employee = xmlDoc.CreateElement("employee");
                    xmlDoc.DocumentElement.AppendChild(employee);
                    Employees.AppendChild(employee);
                    //create the element
                    XmlElement EmployeeID1= xmlDoc.CreateElement("employeeId");
                    employee.AppendChild(EmployeeID1);
                    EmployeeID1.InnerText = row.Cells[0].Text;

                    XmlElement HireDate1 = xmlDoc.CreateElement("hireDate");
                    employee.AppendChild(HireDate1);
                    DateTime newdate = DateTime.ParseExact(row.Cells[6].Text, fromFormat, null);

                    HireDate1.InnerText = newdate.ToString(toFormat);//row.Cells[6].Text;
                    xmlDoc.DocumentElement.InsertAfter(Employees, xmlDoc.DocumentElement.LastChild);
                    id = row.Cells[0].Text;
                }
            }


            string employeenumber = "";

          XmlElement staffHours = xmlDoc.CreateElement("empoyeeHours");
            foreach (GridViewRow row2 in GridView1.Rows)

            {
                XmlElement EmployeeCode= xmlDoc.CreateElement("employeeId");
            XmlElement WorkDays = xmlDoc.CreateElement("workDays");

              if (nursenumber != row2.Cells[0].Text)
                {

                    staffHours.AppendChild(NurseIdCode);
                    employeeCode.InnerText = row2.Cells[0].Text;
                    employeenumber = row2.Cells[0].Text;

                }

                XmlElement WorkDay = xmlDoc.CreateElement("workDay");
                xmlDoc.DocumentElement.PrependChild(WorkDay);
                WorkDays.AppendChild(WorkDay);
                staffHours.AppendChild(WorkDays);
                parentNode.AppendChild(staffHours);
                //Third node and data source
                XmlElement Date = xmlDoc.CreateElement("date");
                WorkDay.AppendChild(Date);
                DateTime converteddate = DateTime.ParseExact(row2.Cells[1].Text, fromFormat, null);
                Date.InnerText = converteddate.ToString(toFormat);


                XmlElement hourEntries = xmlDoc.CreateElement("hourEntries");
                xmlDoc.DocumentElement.PrependChild(hourEntries);
                WorkDay.AppendChild(hourEntries);

                XmlElement HourEntry = xmlDoc.CreateElement("hourEntry");
                xmlDoc.DocumentElement.PrependChild(HourEntry);
                hourEntries.AppendChild(HourEntry);

                //Fourth node and data source
                XmlElement Hours = xmlDoc.CreateElement("hours");
                HourEntry.AppendChild(Hours);
                Hours.InnerText = row2.Cells[2].Text;

                XmlElement JobTitleCode = xmlDoc.CreateElement("jobTitleCode");
                HourEntry.AppendChild(JobTitleCode);
                JobTitleCode.InnerText = row2.Cells[3].Text;

                XmlElement payTypeCode = xmlDoc.CreateElement("payTypeCode");
                HourEntry.AppendChild(payTypeCode);
                payTypeCode.InnerText = row2.Cells[4].Text;

                xmlDoc.DocumentElement.InsertAfter(parentNode, xmlDoc.DocumentElement.LastChild);
                employeenumber = row2.Cells[0].Text;

            }


        }
        catch (Exception ex)
        {
            lblMessage.Visible = true;
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Text = ex.Message;
        }



根据上面的代码我的xml文件如下(我没有包含xml的顶部部分,因为那部分很好,提醒你我从gridview获取数据)


My xml file is below based on the code above (i did not include the top portion of the xml as that part is just fine, mind you i am getting the data from a gridview)

<pre><EmployeeHours>
<employeeId>101631</employeeId>
<workDays>
<workDay>
<date>2017-06-12</date>
<hourEntries>
<hourEntry>
<hours>7.5</hours>
<jobTitleCode>10</jobTitleCode>
<payTypeCode>3</payTypeCode>
</hourEntry>
</hourEntries>
</workDay>
</workDays>
<workDays>
<workDay>
<date>2017-06-13</date>
<hourEntries>
<hourEntry>
<hours>7.5</hours>
<jobTitleCode>10</jobTitleCode>
<payTypeCode>3</payTypeCode>
</hourEntry>
</hourEntries>
</workDay>
</workDays>
<employeeId>103673</employeeId>
<workDays>
<workDay>
<date>2017-06-18</date>
<hourEntries>
<hourEntry>
<hours>12.75</hours>
<jobTitleCode>9</jobTitleCode>
<payTypeCode>3</payTypeCode>
</hourEntry>
</hourEntries>
</workDay>
</workDays>
</EmployeeHours>





i希望employeehours标记在每个员工的最后一天标记后关闭在关闭employeehours标记之前的工作日之后关闭,workdays标记只打开一次并关闭一次。



i want the employeehours tag to close after the last day of days tag per employee and days tag to close right after the works days before the closing of employeehours tag and the workdays tag only opens once and close once.

推荐答案

读一读:在DataSet中使用XML Microsoft Docs [ ^ ] - 您可以直接写入XML文件:将DataSet内容编写为XML数据Microsoft Docs [ ^ ]
Have a read of this: Using XML in a DataSet | Microsoft Docs[^] - you can write directly to an XML file: Writing DataSet Contents as XML Data | Microsoft Docs[^]


这篇关于如何基于特定的XML元素创建XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆