如何向xmlfile现有ID号添加新行?
我希望能够在我的xml文件中添加一个新行,但是使用该文件中的相同ID。我的xml文件有三行,我想添加行,直到我累了。这就是意思;
一切正在工作,阅读,编写和编辑三行中的当前单元格并保存。我希望能够添加这三个ID的新行:
name="model"
name="year"
name="color"
如果我添加另一行,则会抛出错误
XML文件:
<root>
<dealer name="model">
<number>Hashback</number>
</dealer>
<dealer name="year">
<year>2008</year>
</dealer>
<dealer name="color">
<color>black</color>
</dealer>
</root>
我的代码:
private void btnSave_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(FilePath);
XmlNode root = doc.DocumentElement;
XmlNodeList CnodesList = root.SelectNodes("descendant::dealer");
string Tag = string.Empty;
int iTotalRowCount = Gridview_Output.Rows.Count - 1;
for (int i = 0; i < iTotalRowCount; i++)
{
Tag = "";
string name = Gridview_Output.Rows[i].Cells[0].Value.ToString();
if (Gridview_Output.Rows[i].Cells[1].Value.ToString().Length > 0)
{
Tag = Gridview_Output.Rows[i].Cells[1].Value.ToString();
}
if (Gridview_Output.Rows[i].Cells[2].Value.ToString().Length > 0)
{
Tag = Gridview_Output.Rows[i].Cells[2].Value.ToString();
}
if (Gridview_Output.Rows[i].Cells[3].Value.ToString().Length > 0)
{
Tag = Gridview_Output.Rows[i].Cells[3].Value.ToString();
}
XmlAttributeCollection xxxx = CnodesList[i].Attributes;
for (int x = 0; x < xxxx.Count; x++)
{
if (xxxx[x].Value.ToString().Contains(name))
{
XmlNodeList Elements = CnodesList[i].ChildNodes;
for (int z = 0; z < Elements.Count; z++)
{
if (Elements[z].Name == "number")
{
Elements[z].InnerXml = Tag;
}
if (Elements[z].Name == "year")
{
Elements[z].InnerXml = Tag;
}
if (Elements[z].Name == "color")
{
Elements[z].InnerXml = Tag;
}
}
}
}
}
doc.Save(FilePath);
}