Any way to Update an XML file?

Nicholas Piazza 531 Reputation points
2021-03-30T16:42:00.63+00:00

I have an XML file that I read using XmlReader() when my application is started to get some values to use in setting variables. At runtime, the user has the option to change one or more of the variables, which I would like to save back to the XML file when the application closes. Looking at XmlWriter, I don't see any method that looks like it could update an element without rewriting the whole file. Is there a way using System.Xml to update just one or two elements in an XML file without rewriting the whole file?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,239 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,611 Reputation points
    2021-04-01T05:47:16.107+00:00

    Hi NicholasPiazza-0093,
    XmlReader implements IDisposable, so I suggest you can wrap all your code inside a using statement.
    And I made test with your code, it worked fine.
    Here is my code example:

      private bool GetShowIntroFlag()  
            {  
                using (var reader = XmlReader.Create(@"C:\Users\Desktop\test.xml"))   
                {  
                    reader.MoveToContent();  
          
                    while (reader.Read())  
                    {  
                        if (reader.NodeType == XmlNodeType.Element &&  
                            reader.Name == "noShowIntro")  
                        {  
                            _ = reader.Read();  
          
                            if (reader.NodeType == XmlNodeType.Text)  
                            {  
                                switch (reader.Value.ToLower())  
                                {  
                                    case "false":  
                                        return false;  
                                    case "true":  
                                        return true;  
                                    default:  
                                        throw new FileFormatException  
                                            (string.Format  
                                                ($"Unknown noShowIntro value {reader.Value.ToLower()}"));  
                                }   
                            }   
                        }   
                    }  
          
                }   
                return false;  
            }   
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Sam of Simple Samples 5,516 Reputation points
    2021-03-30T17:31:40.103+00:00

    No. The sizes and positions of the updated data would be different thereby altering the position of data following any such update. If the amount of data is large enough that rewriting all of it is a concern then consider a database.

    0 comments No comments

  2. Castorix31 81,721 Reputation points
    2021-03-30T17:34:37.307+00:00

    You can use XmlDocument Class
    You can find samples in MSDN forums archives, like for example :
    edit/update xml file using c#