Which .NET class to use to modify xml on the fly?
IE. <node address="hello">
change to <node address="world">
Printable View
Which .NET class to use to modify xml on the fly?
IE. <node address="hello">
change to <node address="world">
"barknee@westrew.com" <none@none.com> wrote:
>
>Which .NET class to use to modify xml on the fly?
>
>IE. <node address="hello">
>
>change to <node address="world">
>
>
Hi
See XmlDocument and XmlNode
You have to load document
XmlDocument dom = new XmlDocument ();
dom.Load ("...");
// then select node
XmlNode node = dom.SelectSingleNode("...");
node.attributes["address"].Value = "world";
// and then you could save
dom.Save(...);
Miha
"barknee@westrew.com" <none@none.com> wrote in message
news:3d32d0e5$1@10.1.10.29...
>
> Which .NET class to use to modify xml on the fly?
>
> IE. <node address="hello">
>
> change to <node address="world">
>
>
"Miha Markic" <miham@pleasenospam.spin.si> wrote:
>Hi
>
>See XmlDocument and XmlNode
>
>You have to load document
>XmlDocument dom = new XmlDocument ();
>dom.Load ("...");
>// then select node
>XmlNode node = dom.SelectSingleNode("...");
>node.attributes["address"].Value = "world";
>// and then you could save
>dom.Save(...);
>
>Miha
>
>"barknee@westrew.com" <none@none.com> wrote in message
>news:3d32d0e5$1@10.1.10.29...
>>
>> Which .NET class to use to modify xml on the fly?
>>
>> IE. <node address="hello">
>>
>> change to <node address="world">
>>
>>
>
>
Thanks. I have that but now I cannot get the file to close. I need the
file to save, but apparently even thought the Document object goes out of
scope a HANDLE to the file is still open. ???
If you are using XmlTextWriter, make sure you call Close() at the end.
--
Miha
www.spin.si/www/eng
"barknee@westrew.com" <cplus.@127.0.0.1> wrote in message
news:3d341c93$1@10.1.10.29...
>
> "Miha Markic" <miham@pleasenospam.spin.si> wrote:
> >Hi
> >
> >See XmlDocument and XmlNode
> >
> >You have to load document
> >XmlDocument dom = new XmlDocument ();
> >dom.Load ("...");
> >// then select node
> >XmlNode node = dom.SelectSingleNode("...");
> >node.attributes["address"].Value = "world";
> >// and then you could save
> >dom.Save(...);
> >
> >Miha
> >
> >"barknee@westrew.com" <none@none.com> wrote in message
> >news:3d32d0e5$1@10.1.10.29...
> >>
> >> Which .NET class to use to modify xml on the fly?
> >>
> >> IE. <node address="hello">
> >>
> >> change to <node address="world">
> >>
> >>
> >
> >
>
> Thanks. I have that but now I cannot get the file to close. I need the
> file to save, but apparently even thought the Document object goes out of
> scope a HANDLE to the file is still open. ???
Yeah, I found it. I have an MMC snap-in that modifies web.config on the fly
and when I got done showing it, well I forgot to close it. Ooops
"Miha Markic" <miham.nospam@spamno.spin.si> wrote:
>If you are using XmlTextWriter, make sure you call Close() at the end.
>
>--
>Miha
>www.spin.si/www/eng
>
>"barknee@westrew.com" <cplus.@127.0.0.1> wrote in message
>news:3d341c93$1@10.1.10.29...
>>
>> "Miha Markic" <miham@pleasenospam.spin.si> wrote:
>> >Hi
>> >
>> >See XmlDocument and XmlNode
>> >
>> >You have to load document
>> >XmlDocument dom = new XmlDocument ();
>> >dom.Load ("...");
>> >// then select node
>> >XmlNode node = dom.SelectSingleNode("...");
>> >node.attributes["address"].Value = "world";
>> >// and then you could save
>> >dom.Save(...);
>> >
>> >Miha
>> >
>> >"barknee@westrew.com" <none@none.com> wrote in message
>> >news:3d32d0e5$1@10.1.10.29...
>> >>
>> >> Which .NET class to use to modify xml on the fly?
>> >>
>> >> IE. <node address="hello">
>> >>
>> >> change to <node address="world">
>> >>
>> >>
>> >
>> >
>>
>> Thanks. I have that but now I cannot get the file to close. I need the
>> file to save, but apparently even thought the Document object goes out
of
>> scope a HANDLE to the file is still open. ???
>
>
know that you implement some code to change your web.config file. I'm trying
to do the same.
I can change it successfully, but then when I check my config file using
"ConfigSettings", he gaves back to me the old values
Seems to me that the application loads the file into memory and then, if I
want I have to reload the config file.
Can you tell me how to do this?
"Nelson" <nelson.carvalho@movensis.com> wrote:
>know that you implement some code to change your web.config file. I'm trying
>to do the same.
>I can change it successfully, but then when I check my config file using
>"ConfigSettings", he gaves back to me the old values
>
>
>
>Seems to me that the application loads the file into memory and then, if
I
>want I have to reload the config file.
>
>Can you tell me how to do this?
>
Here's a sample. Place your names in as needed:
public void ModifyAttribute(string strXPath, string strKey, string strNewValue)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(@"c:\adbs\bin\web.config");
XmlNodeList nodeList = doc.SelectNodes(strXPath);
foreach (XmlNode node in nodeList)
{
string strValue = node.Name;
if(node.Attributes["key"].Value == strKey)
{
node.Attributes["value"].Value = strNewValue;
}
}
doc.Save(@"c:\adbs\bin\web.config");
}
catch(Exception ex)
{
throw ex;
}
}
public void ModifyAllAttributes(string strXPath, string strAttribute, string
strNewValue)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(@"c:\adbs\bin\web.config");
XmlNodeList nodeList = doc.SelectNodes("//ADBSSectionGroup//ADBSSoapTracing//add");
foreach (XmlNode node in nodeList)
{
node.Attributes["value"].Value = strNewValue;
}
doc.Save(@"c:\adbs\bin\web.config");
}
catch(Exception ex)
{
throw ex;
}
}