根据客户的需求,我做出来的是这样的
但实际客户想要的是这样的,怎么才能做出来这样的,标红的部分是区别
private void button4_Click(object sender, EventArgs e)
{
int n = dataGridView1.Rows.Count;
string para1="";
try
{
XmlDocument pXmlDocument = new XmlDocument();
XmlDeclaration pXmlDeclaration = pXmlDocument.CreateXmlDeclaration("1.0", "gb2312", null);
pXmlDocument.AppendChild(pXmlDeclaration);
//加入一个根元素
XmlElement pElement = pXmlDocument.CreateElement("", "root", "");
pXmlDocument.AppendChild(pElement);
XmlNode pXmlRootNode = pXmlDocument.SelectSingleNode("root");//拿到根元素
//添加要素
XmlElement pXmlRootElement = pXmlDocument.CreateElement("Title");
XmlElement pXmlElement = pXmlDocument.CreateElement("DataModelCode");
pXmlElement.InnerText = "DMXXXX";
XmlElement pXmlElement1 = pXmlDocument.CreateElement("Version");
pXmlElement1.InnerText = "1.00";
XmlElement pXmlRootElement1 = pXmlDocument.CreateElement("MeasureContent");
XmlElement pXmlElement2 = pXmlDocument.CreateElement("MeasuringItem");
for (int i = 0; i < n-1; i++)
{
para1 = para1 + "<item Code=\"" + dataGridView1.Rows[i].Cells[0].Value.ToString() + "\" " +
"Name=\"" + dataGridView1.Rows[i].Cells[1].Value.ToString() + "\" " +
"Value=\"" + dataGridView1.Rows[i].Cells[2].Value.ToString() + "\" />" + "\r\n";
}
para1 = "<Item Code=\"JOBNAME\" Name=\"生产指令号\" Value=\"\">" + "\r\n" +
"<Item Code=\"SYSB\" Name=\"试验设备\" Value=\"\">" + "\r\n" + para1;
pXmlElement2.InnerText = para1;
pXmlRootElement.AppendChild(pXmlElement);
pXmlRootElement.AppendChild(pXmlElement1);
pXmlRootElement1.AppendChild(pXmlElement2);
pXmlRootNode.AppendChild(pXmlRootElement);
pXmlRootNode.AppendChild(pXmlRootElement1);
pXmlDocument.Save("C:\\Users\\Administrator\\Desktop\\##测试程序\\datagridview操作\\test\\bin\\XML.xml");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示");
}
}