S
scottiedog
Hi,
The following code just appends another copy of Xml to existing one.
using (FileStream fs = new FileStream(path, FileMode.Open,
FileAccess.ReadWrite, FileShare.None))
{
xmldoc = new XmlDocument();
xmldoc.Load(fs);
// would like to do something, like add a node.
// for example below, do nothing here.
xmldoc.Save(fs);
}
I would like to read an Xml file exclusively, append a node and save.
The above code, which does nothing other than just open and save
immediately, will attach two copies like this. Note the second
instance of xml declaration.
<?xml version="1.0"?>
<Books>
<Book ID="001">
<Author>Mark</Author>
<Publisher>Sams</Publisher>
</Book>
<Book ID="005">
<Book>Peter</Book>
<Publisher>Que Publishing</Publisher>
</Book>
<Book ID="002">
<Author>Joe</Author>
<Publisher>AWL</Publisher>
</Book>
</Books><?xml version="1.0"?>
<Books>
<Book ID="001">
<Author>Mark</Author>
<Publisher>Sams</Publisher>
</Book>
<Book ID="005">
<Book>Peter</Book>
<Publisher>Que Publishing</Publisher>
</Book>
<Book ID="002">
<Author>Joe</Author>
<Publisher>AWL</Publisher>
</Book>
</Books>
When I open the file with Truncate, then I lose ability to read the
XML because it is truncated immediately. How would I go about opening
an Xml for exclusive append node and save properly? Thanks.
The following code just appends another copy of Xml to existing one.
using (FileStream fs = new FileStream(path, FileMode.Open,
FileAccess.ReadWrite, FileShare.None))
{
xmldoc = new XmlDocument();
xmldoc.Load(fs);
// would like to do something, like add a node.
// for example below, do nothing here.
xmldoc.Save(fs);
}
I would like to read an Xml file exclusively, append a node and save.
The above code, which does nothing other than just open and save
immediately, will attach two copies like this. Note the second
instance of xml declaration.
<?xml version="1.0"?>
<Books>
<Book ID="001">
<Author>Mark</Author>
<Publisher>Sams</Publisher>
</Book>
<Book ID="005">
<Book>Peter</Book>
<Publisher>Que Publishing</Publisher>
</Book>
<Book ID="002">
<Author>Joe</Author>
<Publisher>AWL</Publisher>
</Book>
</Books><?xml version="1.0"?>
<Books>
<Book ID="001">
<Author>Mark</Author>
<Publisher>Sams</Publisher>
</Book>
<Book ID="005">
<Book>Peter</Book>
<Publisher>Que Publishing</Publisher>
</Book>
<Book ID="002">
<Author>Joe</Author>
<Publisher>AWL</Publisher>
</Book>
</Books>
When I open the file with Truncate, then I lose ability to read the
XML because it is truncated immediately. How would I go about opening
an Xml for exclusive append node and save properly? Thanks.