我需要从附加到XML的网格视图图像中导出数据

时间:2017-08-22 13:56:10

标签: c# xml

有人可以告诉我为什么上面的代码不会产生下面的xml元素

    <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>
<workDay>
<date>2017-06-13</date>
<hourEntries>
<hourEntry>
<hours>7.5</hours>
<jobTitleCode>10</jobTitleCode>
<payTypeCode>3</payTypeCode>
</hourEntry>
</hourEntries>
</workDay>
</workDays>
</EmployeeHours>
<EmployeeHours>
<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>

而不是以下代码

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

为了清楚起见,我附上了gridview数据的图像 enter image description here

及以下是我用来生成xml的完整代码。

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

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

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

                        EmployeeHours.AppendChild(EmployeeCode);
                        EmployeeCode.InnerText = row2.Cells[0].Text;
                        EmployeeID = row2.Cells[0].Text;

                    }

                    XmlElement WorkDay = xmlDoc.CreateElement("workDay");
                    xmlDoc.DocumentElement.PrependChild(WorkDay);
                    WorkDays.AppendChild(WorkDay);
                    EmployeeHours.AppendChild(WorkDays);
                    parentNode.AppendChild(EmployeeHours);
                    //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);
                    EmployeeID = row2.Cells[0].Text;

                }

我真的很感谢你对我的代码遗漏的方向,它必须是一个非常简单的东西,我忽略了。 谢谢

对于那些要求我的完整代码的人,请看下面这个工作正常但是输出不是预期的输出(代码的第二部分是问题所在......)  尝试             {

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

1 个答案:

答案 0 :(得分:0)

第一个XML示例无效,您可以通过在Visual Studio中加载它来检查它。它会显示多个根元素。为了避免这种问题,创建XML的最佳方法是使用DataTable.WriteXML() Method

但是,由于您具有需要XML输出的特定格式,因此实现此目的的正确方法是将XSLT转换应用于DataSet。

请参阅此代码示例 -

DataSet ds = new DataSet();
StringBuilder sbXslOutput = new StringBuilder();

using (XmlWriter xslWriter = XmlWriter.Create(sbXslOutput))
{
    XslCompiledTransform transformer = new XslCompiledTransform();
    transformer.Load("transformer.xsl");
    XsltArgumentList args = new XsltArgumentList();

    transformer.Transform(new XmlDataDocument(ds), args, xslWriter);
}

string dataSetHtml = sbXslOutput.ToString();

以下是详细说明您需要做什么的示例 -

Applying an XSLT Transform to a DataSet