2017-08-14 5 views
0

enter image description here xmlファイルを作成するxmlコードがありますが、これはうまく動作しますが、なぜ親ノードの1つがまだ開いていてグループ化しないのか混乱しています従業員が最初にこのコードを今一度取り付けたコード子ノードが繰り返されているため、動作させることができません。

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

`

を参照してください(従業員の時間と平日は一度だけ限り、従業員IDが同じで、すべての日が作成されている最後に近いとして開く必要があります)それを実行して添付画像を作成しますが、強調表示された行が同じ親ノード内で繰り返されることは望ましくありません。 enter image description here

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

XmlElement staffHours = xmlDoc.CreateElement( "EmployeeHours"); 'あなたのforeachループの前にあるべきですか? –

+0

XmlDocumentをファイルの書き込み以外の目的で使用していますか? – JuanR

+0

ファイルを作成して電子メールとして送信するためにのみ使用しています –

答えて

0

私の問題は、私のループの内側にありました。私はこれを調べるために新しい目を必要としました。良いキャッチTamas Szabo。問題は解決されました。

関連する問題