0

I have an XML file eg:

<?xml version="1.0" encoding="utf-8"?>  
    <items>             
      <item>
        <id>1</id>
        <details></details>
        <description></description>
      </item>
       <item>
        <id>2</id>
        <details>
        </details>
        <description></description>
      </item>
    </items>

Now say suppose I want to modify an XML file such that I want to add some data to details tag for item with id=2. Using XML serializer, I would have to read the whole XML file then select item with item Id 2 and modify that class object and write whole file again? So for every update I would have to read the whole xml file into memory, then edit it in memory and then re-write as a whole to the disk?

Is there any other way to achieve this? Like could I have alogic which would simply update the node to the existing XML file?

Arti
  • 2,993
  • 11
  • 68
  • 121
  • 1
    think about it....if you want to edit file manually...you have to open the file first right? whole content will be loaded in memory before you do any change...Same is with editing file programatically... – Viru Apr 14 '16 at 05:54
  • 1
    You can do streaming transformations and insertions, see [How to: Perform Streaming Transform of Large XML Documents](https://msdn.microsoft.com/en-us/library/bb387013.aspx). But you are still going to be reading and rewriting the entire file -- just not holding it all in memory at once. – dbc Apr 14 '16 at 06:10
  • 1
    [Efficient Techniques for Modifying Large XML Files](https://msdn.microsoft.com/en-us/library/aa302289.aspx) might also help, as might [Modifying XML file in-place?](https://stackoverflow.com/questions/7751260). But since an XML file is just a text file, see [How to insert characters to a file using C#](http://stackoverflow.com/questions/98484), which explains that random insert/deletes inside text files is in general not supported. – dbc Apr 14 '16 at 06:18

0 Answers0