C# 我需要从附加到XML的网格视图图像中导出数据
有人能告诉我为什么上面的代码没有生成下面的xml元素吗C# 我需要从附加到XML的网格视图图像中导出数据,c#,xml,C#,Xml,有人能告诉我为什么上面的代码没有生成下面的xml元素吗 <EmployeeHours> <employeeId>101631</employeeId> <workDays> <workDay> <date>2017-06-12</date> <hourEntries> <hourEntry> <hours>7.5</hours> <jobTitleCo
<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>
我真的很感激您对我的代码中缺少的东西的指导,它必须是我忽略的愚蠢的简单的东西。
多谢各位
对于那些要求我提供完整代码的人,请参见下文,这很好,但输出不是预期的输出(代码的第二部分是问题所在….)
尝试
{
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;
}
DataSet ds=新数据集();
数据表dtxml=(数据表)视图状态[“数据”];
ds.Tables.Add(dtxml);
//创建新的XML文档
XmlDocument xmlDoc=新的XmlDocument();
字符串dt=Txtendhour.Text.Replace(“/”,“”);
//写下XML声明
//Server.MapPath(“directory.xml”)
if(File.Exists(Server.MapPath(“PPE”+dt+“.xml”))
{
Delete(Server.MapPath(“PPE”+dt+“.xml”);
}
XmlDeclaration=xmlDoc.CreateXmlDeclaration(“1.0”,“ASCII”,null);
//创建根元素
xmlementrootnode=xmlDoc.CreateElement(“companyData”);
SetAttribute(“xmlns”和“”);
xmlDoc.InsertBefore(xmldocation,xmlDoc.DocumentElement);
AppendChild(rootNode);
xmlement headernode=xmlDoc.CreateElement(“header”);
SetAttribute(“fileSpecVersion”,“2.00.0”);
xmlDoc.DocumentElement.PrependChild(headernode);
//创建所需的节点
xmlementmainnode=xmlDoc.CreateElement(“companyId”);
XmlElement descNode=xmlDoc.CreateElement(“状态代码”);
XmlElement activeNode=xmlDoc.CreateElement(“reportQuarter”);
XmlElement fiscalYear=xmlDoc.CreateElement(“联邦财政年度”);
XmlElement供应商=xmlDoc.CreateElement(“软件供应商名称”);
XmlElement版本=xmlDoc.CreateElement(“softwareProductName”);
XmlElement vendorversion=xmlDoc.CreateElement(“softwareProductVersion”);
//检索文本
字符串faccode=txtcode.Text;
字符串quarter=GridView1。行[0]。单元格[7]。文本;
XmlText categoryText=xmlDoc.CreateTextNode(faccode);
XmlText descText=xmlDoc.CreateTextNode(“状态”);
XmlText activeText=xmlDoc.CreateTextNode(季度);
XmlText fiscalYearactiveText=xmlDoc.CreateTextNode(“2017”);
XmlText activetext2=xmlDoc.CreateTextNode(“软件名”);
XmlText activetext3=xmlDoc.CreateTextNode(“软件类型”);
XmlText activetext4=xmlDoc.CreateTextNode(“版本号”);
//将节点附加到不带值的parentNode
headernode.AppendChild(主节点);
headernode.AppendChild(descNode);
headernode.AppendChild(activeNode);
头节附属物子体(财政耳);
headernode.AppendChild(供应商);
headernode.AppendChild(版本);
headernode.AppendChild(供应商版本);
//将字段的值保存到节点中
mainNode.AppendChild(categoryText);
descNode.AppendChild(descText);
AppendChild(activeText);
fiscalYear.AppendChild(fiscalYearactiveText);
Vendor.AppendChild(activetext2);
AppendChild版本(activetext3);
AppendChild(activetext4);
//创建新元素并将其添加到根节点
xmlement Employees=xmlDoc.CreateElement(“员工”);
xmlDoc.DocumentElement.AppendChild(员工);
XmlElement parentNode=xmlDoc.CreateElement(“EmployeeHours”);
//设置属性名称和值!
SetAttribute(“processType”、“merge”);
//xmlDoc.DocumentElement.PrependChild(parentNode);
字符串id=“”;
字符串employeeCode=GridView1。行[0]。单元格[0]。文本;
字符串fromFormat=“MM/dd/yyyy”;
字符串toFormat=“yyy-MM-dd”;
foreach(GridView1.Rows中的GridViewRow行)
{
//员工元素和子元素的第一部分
如果(id!=行。单元格[0]。文本)
{
xmlement employee=xmlDoc.CreateElement(“员工”);
xmlDoc.DocumentElement.AppendChild(员工);
雇员。子女(雇员);
//创建元素
XmlElement employeeId=xmlDoc.CreateElement(“employeeId”);
employee.AppendChild(EmployeeID1);
EmployeeID1.InnerText=行。单元格[0]。文本;
XmlElement HireDate1=xmlDoc.CreateElement(“hireDate”);
雇员、子女(雇用1人);
DateTime newdate=DateTime.ParseExact(row.Cells[6].Text,fromFormat,null);
HireDate1.InnerText=newdate.ToString(toFormat);//行.Cells[6].Text;
xmlDoc.DocumentElement.InsertAfter(Employees,xmlDoc.DocumentElement.LastChild);
id=行。单元格[0]。文本;
}
}
字符串employeenumber=“”;
xmlement staffHours=xmlDoc.CreateElement(“员工小时”);
foreach(GridView1.Rows中的GridViewRow2)
{
XmlElement EmployeeCode=xmlDoc.CreateElement(“employeeId”);
xmlement WorkDays=xmlDoc.CreateElement(“工作日”);
如果(nursenumber!=行2.Cells[0].Text)
{
员工小时数。附加儿童(NurseIdCode);
employeeCode.InnerText=row2.Cells[0].Text;
employeenumber=row2。单元格[0]。文本;
}
xmlement WorkDay=xmlDoc.Crea
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;
}
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();