Modify XML on the fly


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Modify XML on the fly

  1. #1
    barknee@westrew.com Guest

    Modify XML on the fly


    Which .NET class to use to modify xml on the fly?

    IE. <node address="hello">

    change to <node address="world">



  2. #2
    got it Guest

    Re: Modify XML on the fly


    "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">
    >
    >



  3. #3
    Miha Markic Guest

    Re: Modify XML on the fly

    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">
    >
    >




  4. #4
    barknee@westrew.com Guest

    Re: Modify XML on the fly


    "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. ???

  5. #5
    Miha Markic Guest

    Re: Modify XML on the fly

    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. ???




  6. #6
    barknee@westrew.com Guest

    Re: Modify XML on the fly


    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. ???

    >
    >



  7. #7
    Nelson Guest

    RE: Modify XML on the fly

    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?




  8. #8
    Ted Guest

    RE: Modify XML on the fly


    "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;
    }
    }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center