473,484 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem using FileStream and XmlDocument

Hi.
I'm using a FileStream (instead of just the path to the xml file) to load an
XmlDocument. I'm doing this because I need to be able to prevent other
processes to update the file I'm working on. The problem is that I'm
getting multiple processing instructions and multiple root elements in my
xml file when I use a FileStream. Here is an example:

test_1.xml contains:
<?xml version="1.0" encoding="utf-8"?>
<root>
<myTag myValue="1">test</myTag>
</root>

test_2.xml contains:
<?xml version="1.0" encoding="utf-8"?>
<root>
<myTag myValue="2">test</myTag>
</root>

Public Sub test()
Dim fileStream As New FileStream("C:\Inetpub\wwwroot\test_1.xml",
FileMode.Open, FileAccess.ReadWrite, FileShare.None)
Dim xmlDocument As New XmlDocument()
Dim xmlDocument_2 As New XmlDocument()
Dim xmlNode As XmlNode
Dim xmlNodeNew As XmlNode

xmlDocument_2.Load("C:\Inetpub\wwwroot\test_2.xml" )
xmlNodeNew = xmlDocument_2.SelectSingleNode("root/myTag[@myValue='2']")

xmlDocument.Load(fileStream)

xmlNode = xmlDocument.ImportNode(xmlNodeNew.FirstChild, True)
xmlDocument.DocumentElement.AppendChild(xmlNode)
xmlDocument.Save(fileStream)

fileStream.Flush()
fileStream.Close()
End Sub

This is the result:
<?xml version="1.0" encoding="utf-8"?>
<root>
<myTag myValue="1">test</myTag>
</root>
<?xml version="1.0" encoding="utf-8"?>
<root>
<myTag myValue="1">test</myTag>test
</root>

Does anyone know what I'm doing wrong?

Thanks,
Shawn
Nov 12 '05 #1
1 6795
"Shawn" <bo********@hotmail.com> wrote in message news:40******@news.wineasy.se...
processes to update the file I'm working on. The problem is that I'm
getting multiple processing instructions and multiple root elements in my
xml file when I use a FileStream.


It looks like you are using the same FileStream for input and output.
After XmlDocument has finished loading itself from the FileStream,
the "file position" of the FileStream is at the end of the file. Then
when you save the revised document, it appends it from this position
in the file creating what appears to be a duplicate.

Try using

fileStream.Seek( 0, SeekOrigin.Begin)

after loading the XmlDocument to re-set the "file position" to the start
of the file. Then the Save operation should overwrite the existing file
contents.
Derek Harmon
Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2886
by: André Gasser | last post by:
hello newsgroup, I just discovered a weird effect in my php code. here is the flow of my code: 1. upload jepg file to server 2. create new (empty) jpeg file using imagecreatefromjpeg()...
2
9196
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered...
0
2101
by: antsays | last post by:
I am trying to serialize a collection to disk using the code provided. This works just fine but when I try do copy and past the xml file to another location or sometimes even just click on the...
3
7287
by: Terry | last post by:
In the code below, I open a file using filestream, copy it to a byte array, and the write that array out to a new file. But when I check the size of the original file and the new file, the new...
3
2360
by: rrm | last post by:
Hi I am verifying a SLD implementation using xml, however I have som problem using xpath and default namespaces in vb.net Currently I have the following xml <?xml version="1.0"?>...
3
1947
by: Franky | last post by:
Been having a problem using a treeview. Work great the first time the form containing it is entered but not the second time. Took a while to create a small sample that exhibits the problem, but...
7
1938
by: Piotrekk | last post by:
Hi Probability of this phenomena is like 1/200. The only code where I am dealing with file is the following: using (FileStream fs = new FileStream(this.FilePath,...
2
2011
by: Radek | last post by:
Hi, I have the following problem with FileStream. In this line: FileStream file = new FileStream(filePath, FileMode.Append); there is an exception FileNotFoundException. But for sure path and...
3
2220
by: presencia | last post by:
Hi all, I have a problem using gdb. I am using Eclipse-CDT as a frontend, but since this problem is obviously related to the gcc compiler or the gdb itself, I have reproduced it without Eclipse. ...
0
7079
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6949
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6809
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5403
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4527
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3044
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3038
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1355
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
587
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.