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
LVL 3
joeyluAsked:
Who is Participating?
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.

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

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
joeyluAuthor Commented:
clear and easy, tks
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
Web Languages and Standards

From novice to tech pro — start learning today.