简体   繁体   English

XML文件中的自动增量ID

[英]Auto Increment ID in XML File

Could someone help me do this? 有人可以帮我吗? I wish he auto-increment, but he does not. 我希望他会自动递增,但他不会。 Could you help me do this? 你能帮我做这个吗?

My code is: 我的代码是:

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

Anyone can help-me? 有人可以帮我吗? Anyone know how this? 有人知道吗?

__ _ __ _ __ _ _ EDIT 09/05/2012 _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _编辑2012年5月5日_ __ _ __ _ __ _ __ _ _

Solution: 解:

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

Use Attributes... Because we have to follow the standard XML. 使用属性...,因为我们必须遵循标准XML。 Select ID to set value with GetElementsByTagName! 选择ID以使用GetElementsByTagName设置值! Use foreach to verify Attribute "value" and insert +1 using SetAttribute 使用foreach验证属性“值”并使用SetAttribute插入+1

Is this what you need? 这是您需要的吗?

int iID = 0;

XmlElement newElement = xmlDoc.CreateElement("ID_" + iID++.ToString())

And the rest is the same... 其余的都一样...

I've added the "ID_" because I'm not sure if a XML tag name can start with a number 我添加了“ ID_”,因为我不确定XML标签名称是否可以以数字开头

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM