子节点正在重复,无法让它工作

时间:2017-08-14 13:44:34

标签: c# xml

enter image description here我有一个xml代码,可以创建一个xml文件,但这很有用但是我有点困惑为什么其中一个父节点仍然打开而且它没有按员工分组(员工小时和工作日应该只在员工ID相同时开放一次,并且在创建所有日期时在最后关闭)参见附加代码`

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

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

 string catid = ""; 
string nurseCode = GridView1.Rows[0].Cells[0].Text;    
foreach ( GridViewRow row in GridView1.Rows)
{
    //first part of EMPLOYEES ELEMENTS AND CHILD ELEMENTS
    string fromFormat = "MM/dd/yyyy";
    string toFormat = "yyyy-MM-dd";
    if (catid != row.Cells[0].Text)
    {
     XmlElement employee = xmlDoc.CreateElement("employee");
    xmlDoc.DocumentElement.AppendChild(employee);
    Employees.AppendChild(employee);
    //create the element
    XmlElement NurseId1 = xmlDoc.CreateElement("employeeId");
    employee.AppendChild(NurseId1);
    NurseId1.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);

    }

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

    if (catid != row.Cells[0].Text)
    {
           XmlElement NurseId = xmlDoc.CreateElement("employeeId");
           staffHours.AppendChild(NurseId);
           NurseId.InnerText = row.Cells[0].Text;

   }  


           XmlElement WorkDays = xmlDoc.CreateElement("workDays"); 
           XmlElement WorkDay = xmlDoc.CreateElement("workDay");
            //Third node and data source
            XmlElement Date = xmlDoc.CreateElement("date");
            WorkDay.AppendChild(Date);
            DateTime converteddate = DateTime.ParseExact(row.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 = row.Cells[2].Text;

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

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

           staffHours.AppendChild(WorkDays); 
           WorkDays.AppendChild(WorkDay);
           parentNode.AppendChild(EmployeeHours);

           xmlDoc.DocumentElement.InsertAfter(parentNode, 
           xmlDoc.DocumentElement.LastChild);

           catid = row.Cells[0].Text;}

现在一旦这个代码运行它就会创建附加图像,但我不希望突出显示的行在同一个父节点内重复... enter image description here

[1]: https://i.stack.imgur.com/eirsv.png

1 个答案:

答案 0 :(得分:0)

我的问题是元素在我的循环中我需要一组新的眼睛来研究这个。好赶上Tamas Szabo。问题已经解决。