locked
edit/update xml file using c# RRS feed

  • Question

  • Hi All,

    Iam a newbie to this field and Iam having an xml file as shown below and i want to change the value of the "ServerAddress" key value from test to hello using c#

    <?xml version="1.0" encoding="utf-8" ?>
    <appSettings>
    <add key="ServerAddress" value="test" />
    </appsettings>

    Kindly help me on the same
    Thanks,
    Ram

    ram

    mercredi 23 janvier 2013 06:21

Réponses

  • Hi,

    if you want to query by key name insteed of index then follow this code :

     XmlDocument doc = new XmlDocument();
                string path = @"C:\MyFiles\TestApp\WindowsFormsApplication1\WindowsFormsApplication1\XMLFile1.xml";
                doc.Load(path);
                IEnumerator ie = doc.SelectNodes("/appSettings/add").GetEnumerator();
    
                while (ie.MoveNext())
                {
                    if ((ie.Current as XmlNode).Attributes["key"].Value == "ServerAddress")
                    {
                        (ie.Current as XmlNode).Attributes["value"].Value = "hello";
                    }
                }
    
                doc.Save(path);

    Hope my these posts may help.

    One good question is equivalent to ten best answers.

    • Marqué comme réponse coolram4u mercredi 23 janvier 2013 08:44
    mercredi 23 janvier 2013 08:02

Toutes les réponses

  • Hi Ram,

    please follow the code :

     XmlDocument doc = new XmlDocument();
                string path = @"C:\MyFiles\TestApp\WindowsFormsApplication1\WindowsFormsApplication1\XMLFile1.xml";
                doc.Load(path); 
                doc.SelectSingleNode("/appSettings/add").Attributes["value"].Value = "hello";
                doc.Save(path);

    Do not forget to put System.XML namespace at top.

    And Path variable in my example is the absolute path of the XML file that you have to modify.

    Hope this helps.


    One good question is equivalent to ten best answers.

    mercredi 23 janvier 2013 07:19
  • Hi,

    You don't have to manually edit the XML file. .NET has classes and helpers in place to facilitate this.

    Have a look at this codeproject article for an idea on how to do this. It's a bit old, but still applies.

    Hope this helps


    Please mark the best replies as answers
    Blog: bloggingabout.net/blogs/rick
    Twitter: @rickvdbosch

    mercredi 23 janvier 2013 07:21
  • first of all thanks for the reply shyam, iam having one more doubt here, suppose if i have more key value pairs in the same xml for example as shown below and i want to change only the value having key name with "ServerAddress" only, then how to approach that, could you please suggest me :

    <?xml version="1.0" encoding="utf-8"?>
    <appSettings>
      <add key="URL" value="http://" />
      <add key="Server2ClientPort" value="7890" />
      <add key="ServerAddress" value="test" />  
    </appSettings>

    Thanks,

    Ram


    ram

    mercredi 23 janvier 2013 07:30
  • first of all thanks for the reply shyam, iam having one more doubt here, suppose if i have more key value pairs in the same xml for example as shown below and i want to change only the value having key name with "ServerAddress" only, then how to approach that, could you please suggest me :

    <?xml version="1.0" encoding="utf-8"?>
    <appSettings>
      <add key="URL" value="http://" />
      <add key="Server2ClientPort" value="7890" />
      <add key="ServerAddress" value="test" />  
    </appSettings>

    Thanks,

    Ram


    ram

    Hi,

    then you need to do like this:

     XmlDocument doc = new XmlDocument();
                string path = @"C:\MyFiles\TestApp\WindowsFormsApplication1\WindowsFormsApplication1\XMLFile1.xml";
                doc.Load(path);
                //doc.SelectSingleNode("/appSettings/add").Attributes["value"].Value = "hello";
                doc.SelectNodes("/appSettings/add").Item(2).Attributes["value"].Value = "hello";
                doc.Save(path);
    where number passed in Item method is index number of the node where you want to make changes.
    Hope this helps.

    One good question is equivalent to ten best answers.



    • Modifié Shyam Kr mercredi 23 janvier 2013 07:53
    • Proposé comme réponse Jason Dot Wang jeudi 24 janvier 2013 07:07
    mercredi 23 janvier 2013 07:52
  • Hi,

    if you want to query by key name insteed of index then follow this code :

     XmlDocument doc = new XmlDocument();
                string path = @"C:\MyFiles\TestApp\WindowsFormsApplication1\WindowsFormsApplication1\XMLFile1.xml";
                doc.Load(path);
                IEnumerator ie = doc.SelectNodes("/appSettings/add").GetEnumerator();
    
                while (ie.MoveNext())
                {
                    if ((ie.Current as XmlNode).Attributes["key"].Value == "ServerAddress")
                    {
                        (ie.Current as XmlNode).Attributes["value"].Value = "hello";
                    }
                }
    
                doc.Save(path);

    Hope my these posts may help.

    One good question is equivalent to ten best answers.

    • Marqué comme réponse coolram4u mercredi 23 janvier 2013 08:44
    mercredi 23 janvier 2013 08:02
  • thanks a lot shyam,

    it really worked for me


    ram

    mercredi 23 janvier 2013 08:45