2017-04-20 87 views
0

我需要用我的元素幫助,因爲我想確保我的文件相匹配的要素及以下的Xml文件創建

<employees><employee><employeeId>employeeid</employeeId> 
<hireDate>Hiredate</hireDate> 
</employee> 
<employee> 
<employeeId>employeeid</employeeId> 
<hireDate>Hiredate</hireDate> 
</employee> 
<employees> 

<staffingHours processType="merge"> 

<staffHours> 
<employeeId>employeeid</employeeId> 
<workDays> 
<workDay> 
<date>date</date> 
<hourEntries> 
<hourEntry> 
<hours>hours</hours> 
<jobTitleCode>titlecode</jobTitleCode> 
<payTypeCode>paycode</payTypeCode> 
</hourEntry></hourEntries> 
</workDay> 
</workDays> 
</staffHours> 
</staffingHours> 

屬性請記住,我從一個Excel工作表網頁上得到這個數據手冊頁和下面是生成的XML文件中的代碼和我使用的是爲每個環的任何幫助表示讚賞GridView的記錄

XmlElement Empoyees = xmldoc.CreateElement("Employees"); 

    XmlElement newDept = xmldoc.CreateElement("Employee"); 
    XmlElement NurseId = xmldoc.CreateElement("employeeid"); 
    newDept.AppendChild(NurseId); 
    NurseId.InnerText = row.Cells[0].Text; 

    XmlElement Firstname = xmldoc.CreateElement("Firstname"); 
    newDept.AppendChild(Firstname); 
    Firstname.InnerText = row.Cells[1].Text; 
    //Second node and data source 
    XmlElement Lastname = xmldoc.CreateElement("Lastname"); 
    newDept.AppendChild(Lastname); 
    Lastname.InnerText = row.Cells[2].Text; 

    //Third node and data source 
    XmlElement Starthour = xmldoc.CreateElement("StartDate"); 
    newDept.AppendChild(Starthour); 
    Starthour.InnerText = row.Cells[5].Text; 
    //Fourth node and data source 
    XmlElement EndDate = xmldoc.CreateElement("EndDate"); 
    newDept.AppendChild(EndDate); 
    EndDate.InnerText = row.Cells[6].Text; 

    XmlElement TotalHours = xmldoc.CreateElement("TotalHours"); 
    newDept.AppendChild(TotalHours); 
    TotalHours.InnerText = row.Cells[10].Text; 

    XmlElement Starttime = xmldoc.CreateElement("Starttime"); 
    newDept.AppendChild(Starttime); 
    Starttime.InnerText = row.Cells[11].Text; 

    XmlElement EndTime = xmldoc.CreateElement("EndTime"); 
    newDept.AppendChild(EndTime); 
    EndTime.InnerText = row.Cells[12].Text; 

    //Fourth node and data source 
    XmlElement HireDate = xmldoc.CreateElement("HireDate"); 
    newDept.AppendChild(HireDate); 
    HireDate.InnerText = row.Cells[8].Text; 
    //write to the xml document 
    xmldoc.DocumentElement.InsertAfter(newDept, xmldoc.DocumentElement.LastChild); 

+0

什麼編程語言是您使用? Java的? C#? – jwodder

+0

我正在使用C#和VS2017 –

+0

爲什麼你需要幫助?你粘貼的代碼有什麼問題? –

回答

0

我得到這個想通了,萬一有人需要幫助這個見下文

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

    } 

    // Create a new <Employees> element and add it to the root node 
    XmlElement staffHours = xmlDoc.CreateElement("staffHours"); 
    xmlDoc.DocumentElement.PrependChild(staffHours); 
    parentNode.AppendChild(staffHours); 
    // Save to the XML file 
    // Create a new <Employees> element and add it to the root node 
    XmlElement WorkDays = xmlDoc.CreateElement("childElement"); 
    xmlDoc.DocumentElement.PrependChild(WorkDays); 
    staffHours.AppendChild(WorkDays); 

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

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

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

//First node and data source 
XmlElement NurseId = xmlDoc.CreateElement("mainelement"); 
staffHours.AppendChild(NurseId); 
NurseId.InnerText = row.Cells[0].Text; 

//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 newDept = xmlDoc.CreateElement("childmain"); 
//Fourth node and data source 
XmlElement Hours = xmlDoc.CreateElement("hours"); 
HourEntry.AppendChild(Hours); 
Hours.InnerText = row.Cells[2].Text; 

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

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

XmlElement endhour = xmlDoc.CreateElement("endhour"); 
HourEntry.AppendChild(endhour); 
endhour.InnerText = row.Cells[10].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; 

xmlDoc.DocumentElement.InsertAfter(parentNode, xmlDoc.DocumentElement.LastChild); 
catid = row.Cells[0].Text; 
}