Good day, Hashcode.

Something I can’t see into one thing:

When working with xml, if I take a file from a stream, a copy of the XDocument is created:

if (doc == null) { doc = XDocument.Load(stream, LoadOptions.None); } doc.Element("someElement").Add(... ); 

When saving to the stream, the contents of this file is added to the tail of the existing one.

 doc.Save(stream); 

It turns out in the end a rare nonsense (the original file does not go anywhere, just a corrected file is added to it in the tail, which is nonsense).

How to make

  1. Is the saved xml file overwriting the file from the stream?
  2. Well, or alternatively - the file was opened from the stream for a change, and did not create a local copy.

In theory, it should be simple, but something I'm stupid today ... :)

    2 answers 2

    Try calling the Seek (0, SeekOrigin.Begin) method before saving to the stream.

    • Yes. Just before calling Seek() you need to check whether the stream supports moving - the CanSeek property. - andreycha
    • I have already managed to make it my own way, now I'll check this option, ATP. - Olter
    • Works. Perhaps this option would be better than the witch that I invented. )) I accept. - Olter

    Read about threads at least this . A stream is just a source of information and says that a copy is being made is not true. An instance of the class is created based on the data obtained from the stream.

    You do not have a hard link between the XDocument and the flow, when a change in the flow leads to a change in the XDocument and vice versa.

    The stream serves the purpose of abstracting whether a file is a piece of memory or anything else.

    How to fix the situation, you answered @nolka .

    • "Creates a new System.Xml.Linq.XDocument Instance using the specified string" is of course not creating a new object? The link is nothing, I am also familiar with MSDN, thanks. - Olter
    • @Olter, and where does the creation of a new object? The link leads to a description of what a FLOW is . You are apparently familiar with msdn, but do not communicate :-) In your question, you mixed the concept of file, stream, and xml and blame the FLOW for your troubles, but I tried to explain it to you with my answer - for what is a minus right away? - Chad
    • Minus - for the actual error. Here you write to me:> The stream is just a source of> information and says that> a copy is being created - not true. You do not have> a hard link XDocument and a thread. And I, for example, see this:! [Alt text] [1] If you can prove to me that this is not the case, and there is no hard link between the stream and the XDocument - I will be grateful. [1]: i.imgur.com/9gpO3.png - Olter
    • one
      Okay, cant with terminology, it seems. And, by the way, you write it yourself: 1) it says that a copy is being created - not true 2) it says that an XDocument is being created (I meant to create an instance of a class under the copy). :) - Olter
    • one
      Understood :-) It seemed to me that you think that the stream itself, and not its data, appears in the XDocument, based on paragraph 2. "How to do it in order". And you get upset that XDocument creates a copy, and does not work directly. - Chad