XML Load and Save Exclusively

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.
 
M

Martin Honnen

scottiedog said:
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.

Doesn't

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
// now add a node e.g.
xmlDoc.DocumentElement.AppendChild(xmlDoc.CreateElement("foo"));
xmlDoc.Save(path);

suffice? Or is that not what you call "exclusively"? In that case you
could change your using block to

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.

// now add a node e.g.
xmlDoc.DocumentElement.AppendChild(xmlDoc.CreateElement("foo"));

fs.Position = 0;
xmldoc.Save(fs);
}

As long as you add stuff that way the old content should be completely
overwritten.
 
S

scottiedog

The problem was not necessary adding a node part. It was the save.
But you got it by putting the position = 0, there is no more second
Xml. Thank you.
 
S

scottiedog

When I said exclusive meaning at file system level so that I no other
process or thread can read or write the file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top