seetoronto
asked on
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
Thank you so much
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";
}
}
Thank you so much
ASKER
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.
I am absolutely new to xml, xpath, and .Net
I am learning......
anticipating a early reply with code.
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;
ASKER
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
Could you try this on your pc if it works ??
I will appreciate a working code.
thx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
ASKER
Very good. Just as i needed.
I can say EE is the best community as they have great talented Gurus..
thx again
I can say EE is the best community as they have great talented Gurus..
thx again
/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