question

NicholasPiazza-0093 avatar image
0 Votes"
NicholasPiazza-0093 asked NicholasPiazza-0093 commented

Any way to Update an XML file?

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?

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DanielZhang-MSFT avatar image
1 Vote"
DanielZhang-MSFT answered NicholasPiazza-0093 commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks, that worked. Apparently, the file was still open from the read when I tried to write to it.

0 Votes 0 ·
SimpleSamples avatar image
0 Votes"
SimpleSamples answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
0 Votes"
Castorix31 answered NicholasPiazza-0093 commented

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


· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That will rewrite the whole file, right?

0 Votes 0 ·

I have an XML file that looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<Introduction>
<noShowIntro>false</noShowIntro>
</Introduction>

I have a method like the following to update the value of the 'noShowIntro' element as shown below.


         public void SaveShowIntroFlag (bool flag)
         {
             var doc = new XmlDocument ();
    
             doc.Load (pathIntro);
    
             XmlNode root = doc.DocumentElement;
    
             root.SelectSingleNode ("noShowIntro").InnerText = flag.ToString ();
             doc.Save (pathIntro);
    
         }

However, when I run this, I get the following exception on the call to 'doc.Save'

82961-ioexception.png



To my knowledge, no other process should have that file open. What is going on please?

0 Votes 0 ·
ioexception.png (22.5 KiB)
DanielZhang-MSFT avatar image DanielZhang-MSFT NicholasPiazza-0093 ·

Hi @NicholasPiazza-0093,
Just running your above code will not cause an error, have you used XML file in other code?
Please provide more code to reproduce the situation.
Best Regards,
Daniel Zhang

0 Votes 0 ·

I have another method that reads the value from the XML file, but when I try to include the code, it exceeds the 1000 character limit for this reply.

0 Votes 0 ·
Show more comments

83385-gsif.txt



To get past the 1000 character limit problem, I have attached the method I use to read the value from the XML file. This method and the previously shown write method are the only two methods that access the file.

0 Votes 0 ·
gsif.txt (1.2 KiB)