• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1410
  • Last Modified:

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
0
seetoronto
Asked:
seetoronto
  • 4
  • 2
1 Solution
 
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now