sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
Anita C's Avatar

Writing to an xml file


Question posted by: Anita C (Guest) on November 11th, 2005 10:49 PM
A code snippet from an xml file that I want to edit:
<?xml version="1.0" encoding="utf-8" ?>
<ROOT>
<WEB>
<THEME>Default</THEME>
I am trying to replace the 'InnerText' - Default with the text present in a
textbox 'txt1'. How do I accomplish the above?
Thanks in advance.


3 Answers Posted
Dan Wahlin's Avatar
Guest - n/a Posts
#2: Re: Writing to an xml file

Something like the following should get you going:

XmlDocument doc = new XmlDocument();
doc.Load(yourfile);
XmlNode root = doc.DocumentElement;
XmlNode theme = root.SelectSingleNode("/WEB/THEME");
if (theme != null) {
theme.InnerText = txt1.Text;
}
doc.Save(yourFile);

HTH,
Dan Wahlin

Wahlin Consulting
Microsoft MVP - ASP.NET and XML Web Services
http://www.xmlforasp.net


"Anita C" <anitac@ptm-software.com> wrote in message
news:us1PPcxSDHA.1072@TK2MSFTNGP10.phx.gbl...[color=blue]
> A code snippet from an xml file that I want to edit:
> <?xml version="1.0" encoding="utf-8" ?>
> <ROOT>
> <WEB>
> <THEME>Default</THEME>
> I am trying to replace the 'InnerText' - Default with the text present in[/color]
a[color=blue]
> textbox 'txt1'. How do I accomplish the above?
> Thanks in advance.
>
>[/color]


Santastp's Avatar
Guest - n/a Posts
#3: Re: Writing to an xml file

Hi!

The simplest way!

XmlDocument xmlDocument = new XmlDocument();

xmlDocument.Load(Environment.CurrentDirectory + "\\" + "test.xml");

XmlNodeList nodelist = xmlDocument.SelectNodes("/ROOT/WEB/THEME");

nodelist.Item(0).InnerText = "New Theme";

xmlDocument.Save(Environment.CurrentDirectory + "\\" + "test.xml");



Stephane


"Sergey Dubinets" <sdub@online.microsoft.com> wrote in message
news:us5Kj7zSDHA.2084@TK2MSFTNGP11.phx.gbl...[color=blue]
> You can load xsl file to DOM -- XmlDocument, change required text node and
> save DOM back or to another file.
> You can use XSLT to do all three steps for you.
> Sergey
> --
> This posting is provided "AS IS" with no warranties, and confers no[/color]
rights.[color=blue]
>
> "Anita C" <anitac@ptm-software.com> wrote in message
> news:us1PPcxSDHA.1072@TK2MSFTNGP10.phx.gbl...[color=green]
> > A code snippet from an xml file that I want to edit:
> > <?xml version="1.0" encoding="utf-8" ?>
> > <ROOT>
> > <WEB>
> > <THEME>Default</THEME>
> > I am trying to replace the 'InnerText' - Default with the text present[/color][/color]
in[color=blue]
> a[color=green]
> > textbox 'txt1'. How do I accomplish the above?
> > Thanks in advance.
> >
> >[/color]
>
>[/color]


Anita C's Avatar
Guest - n/a Posts
#4: Re: Writing to an xml file

Thanks for all the help.

"Anita C" <anitac@ptm-software.com> wrote in message
news:us1PPcxSDHA.1072@TK2MSFTNGP10.phx.gbl...[color=blue]
> A code snippet from an xml file that I want to edit:
> <?xml version="1.0" encoding="utf-8" ?>
> <ROOT>
> <WEB>
> <THEME>Default</THEME>
> I am trying to replace the 'InnerText' - Default with the text present in[/color]
a[color=blue]
> textbox 'txt1'. How do I accomplish the above?
> Thanks in advance.
>
>[/color]


 
Not the answer you were looking for? Post your question . . .
196,805 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,805 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors