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

insert node into xml from code behind asp.net

Let's say I have a xml file (doc.xml) like this:
<Root>
  <Menu>1</Menu>
  <Menu>2</Menu>
  <Other>1</Other>
  <Other>2</Other>
  <Page>1</Page>
  <Page>2</Page>
</Root>

I need to insert a line before the first <Other> node, to make the doc.xml look like this:

<Root>
  <Menu>1</Menu>
  <Menu>2</Menu>
  <Other>InnerText Inserted</Other>
  <Other>1</Other>
  <Other>2</Other>
  <Page>1</Page>
  <Page>2</Page>
</Root>

How can I do this at asp.net code behind? tks
0
joeylu
Asked:
joeylu
1 Solution
 
Amandeep Singh BhullarCommented:
Check the attached code.
private void AddRecord()
{
  DataSet ds = new DataSet();
  try
  {
	ds.ReadXml(@"c:\inetpub\wwwroot\WebApplication10\XMLFile1.xml");
	DataRow row = ds.Tables[0].NewRow();
	row["name"]=txtName.Text;
	row["fathersname"]=txtFName.Text;
	row["address"]=txtAddress.Text;
	ds.Tables[0].Rows.Add(row);
	ds.WriteXml(@"c:\inetpub\wwwroot\WebApplication10\XMLFile1.xml");
	txtAddress.Text="";
	txtFName.Text="";
	txtName.Text ="";
  }
  catch(Exception ex)
  {
    throw ex;
  }
}

Open in new window

0
 
lazyberezovskyCommented:

XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("doc.xml")); // or just "doc.xml"
XmlNode root = doc.DocumentElement;
XmlElement element = doc.CreateElement("Other");
element.InnerText = "InnerText Inserted";
root.InsertBefore(element, doc.SelectSingleNode("//Other"));

Open in new window

0
 
joeyluAuthor Commented:
clear and easy, tks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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