Create a Xml File using XMLDom need auto increment attribute.

Hi ,
I have a Form in c# with fields which take following input
employeename
employeedescription
employeelocation
employeestartdate
employeeenddate
employeeemail

When i fill in the form and submit it write a xml file and appends new data...
But i want to add a attribute to employee as id which will auto increment by 1 when the form is submitted each time.

i have the following code....can you make changes to get it working with autoincrement id?
Pls help me with autoincrementing an attribute named "id" for employee
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.IO;

public partial class inserteventbyxml : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void button1_Click(object sender, EventArgs e)
    {
        string filename = Request.PhysicalApplicationPath + @"employees.xml";
        XmlDocument doc = new XmlDocument();
        if (File.Exists(filename))
        {
            doc.Load(filename);
        }
        else
        {
          XmlElement mainelement= doc.CreateElement("employees");   
  doc.AppendChild(mainelement);


        }
        XmlElement root = doc.DocumentElement;

        XmlElement nextroot= doc.CreateElement("employee");
        XmlElement employeenameelement = doc.CreateElement("employeename");
        XmlElement employeedescriptionelement = doc.CreateElement("employeedescription");
        XmlElement employeelocationelement = doc.CreateElement("employeelocation");
        XmlElement employeestartelement = doc.CreateElement("employeestart");
        XmlElement employeeendelement = doc.CreateElement("employeeend");
        XmlElement employeeemailelement = doc.CreateElement("employeeemail");



        nextroot.AppendChild(employeenameelement );
        employeenameelement .InnerText = textmployeename.Text;
        nextroot.AppendChild(employeedescriptionelement);
        employeedescriptionelement.InnerText = textemployeedescription.Text;
        nextroot.AppendChild(employeelocationelement);
        employeelocationelement.InnerText = textemployeelocation.Text;
        nextroot.AppendChild(employeestartelement);
        employeestartelement.InnerText = textemployeestart.Text;
        nextroot.AppendChild(employeeendelement);
        employeeendelement.InnerText = textemployeeend.Text;
        nextroot.AppendChild(employeeemailelement);
        employeeemailelement.InnerText = textemployeeemail.Text;

        root.AppendChild(nextroot);

        doc.Save(filename);
        lblresult.Text = "employee added";
        
    }
}

Open in new window


Thank you so much
seetorontoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jjardineCommented:
If the document already exists, try using xpath to grab the last Employee element with something like this:  

/employees/employee[last()]

Once you use that expression to get the last employee you can grab the attribute value then increment it by one.

Here is a MSDN reference for using Xpath with .net.   http://support.microsoft.com/kb/308333
0
seetorontoAuthor Commented:
would you mind to code it for me to show a perfect working thing.
I am absolutely new to xml, xpath, and .Net
I am learning......

anticipating a early reply with code.
0
MlandaTCommented:
try this:

long newId = 0;

if (File.Exists(filename))
{
	doc.Load(filename);
	XmlNode lstNode = xmlDoc.SelectSingleNode("//employee/person[last()]");
	newId = lstNode.Attributes("id").Value as long;
}
else
{
	XmlElement mainelement= doc.CreateElement("employees");  
	doc.AppendChild(mainelement);
}

XmlElement root = doc.DocumentElement;

XmlElement nextroot= doc.CreateElement("employee");
nextroot.Attributes("id") = newId + 1;

Open in new window

0
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference ithrough the Colors of STEM program.

seetorontoAuthor Commented:
I am afarid but this does not work...Probably the Syntax is the problem...
Could you try this on your pc if it works ??
I will appreciate a working code.

thx
0
MlandaTCommented:
I had thougt it trivial to get the code working, as you correctly picked out, I had not tried this in my compiler - and indeed there were a few mistakes in my C# syntax. Sorry about that. However, the way to go about it remains the same...

long newId = 0;
string filename = "C:\\Temp\\Data.xml";
XmlDocument doc = new XmlDocument();

if (File.Exists(filename))
{
	doc.Load(filename);
	XmlNode lstNode = doc.SelectSingleNode("//employees/employee[last()]");
	if(lstNode!=null) newId = long.Parse(lstNode.Attributes["id"].InnerText);

}
else
{

	XmlElement mainelement= doc.CreateElement("employees");  
	doc.AppendChild(mainelement);

}

XmlElement root = doc.DocumentElement;

XmlElement nextroot= doc.CreateElement("employee");
nextroot.SetAttribute("id", (newId + 1).ToString());

Console.WriteLine("Begin");
XmlElement employeenameelement = doc.CreateElement("employeename");
//declare the rest of your nodes here...

nextroot.AppendChild(employeenameelement );
employeenameelement.InnerText = "MyName " + (newId + 1).ToString();
//assign the rest of them here...

root.AppendChild(nextroot);

doc.Save(filename);

Open in new window


EmployeesXMLDemo.cs
Data.xml
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
seetorontoAuthor Commented:
Thank you so much... Your code worked just PERFECT, and you were so quick...
I highly appreciate....
I have another question related to similar xml file..
Can you pls see my another posted question as below?
My posted question is Searching a XML file and displaying results in GridView.
Hope you can solve this too....

Thx a mil
0
seetorontoAuthor Commented:
Very good. Just as i needed.
I can say EE is the best community as they have great talented Gurus..

thx again
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.