有人可以帮我这样做吗?我希望他自动增加,但他没有。你能帮我做这件事吗?
我的代码是:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath(@"Participantes.xml"));
XmlElement newElement = xmlDoc.CreateElement("ID" **/* this is need auto-increment */**);
XmlElement xmlNome = xmlDoc.CreateElement("Nome");
XmlElement xmlClass = xmlDoc.CreateElement("Classe");
XmlElement xmlFaccao = xmlDoc.CreateElement("Facção");
XmlElement xmlLevel = xmlDoc.CreateElement("Level");
xmlNome.InnerText = this.TextBox_I1.Text.Trim();
xmlClass.InnerText = this.Class_I1.SelectedItem.Text.Trim();
xmlFaccao.InnerText = this.Faccao.SelectedItem.Text.Trim();
xmlLevel.InnerText = this.TextBox_lvl.Text.Trim();
newElement.AppendChild(xmlNome);
newElement.AppendChild(xmlClass);
newElement.AppendChild(xmlFaccao);
newElement.AppendChild(xmlLevel);
xmlDoc.DocumentElement.AppendChild(newElement);
xmlDoc.Save(Server.MapPath(@"Participantes.xml"));
任何人都可以帮助我吗?有谁知道这是怎么回事?
__ _ __ _ __ _ _编辑 09/05/2012 _ __ _ __ _ __ _ __ _
解决方案:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath(@"Participantes.xml"));
var x = xmlDoc.GetElementsByTagName("ID");
int Max = 0;
foreach (XmlElement item in x)
{
int ultimoID = Convert.ToInt32(item.GetAttribute("value"));
if (ultimoID > Max)
{
Max = ultimoID;
}
}
Max++;
XmlElement newElement = xmlDoc.CreateElement("ID");
newElement.SetAttribute("value", Max.ToString());
XmlElement xmlNome = xmlDoc.CreateElement("Nome");
XmlElement xmlClass = xmlDoc.CreateElement("Classe");
XmlElement xmlFaccao = xmlDoc.CreateElement("Facção");
XmlElement xmlLevel = xmlDoc.CreateElement("Level");
xmlNome.InnerText = this.TextBox_I1.Text.Trim();
xmlClass.InnerText = this.Class_I1.SelectedItem.Text.Trim();
xmlFaccao.InnerText = this.Faccao.SelectedItem.Text.Trim();
xmlLevel.InnerText = this.TextBox_lvl.Text.Trim();
newElement.AppendChild(xmlNome);
newElement.AppendChild(xmlClass);
newElement.AppendChild(xmlFaccao);
newElement.AppendChild(xmlLevel);
xmlDoc.DocumentElement.AppendChild(newElement);
xmlDoc.Save(Server.MapPath(@"Participantes.xml"));
使用属性...因为我们必须遵循标准的 XML。选择 ID 以使用 GetElementsByTagName 设置值!使用 foreach 验证属性“值”并使用 SetAttribute 插入 +1