473,231 Members | 1,836 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

Problem using FileStream and XmlDocument

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"?>
<myTag myValue="1">test</myTag>

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

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']")


xmlNode = xmlDocument.ImportNode(xmlNodeNew.FirstChild, True)

End Sub

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

Does anyone know what I'm doing wrong?

Nov 12 '05 #1
1 6784
"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
Derek Harmon
Nov 12 '05 #2

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

Similar topics

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()...
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...
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...
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...
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"?>...
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...
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,...
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...
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. ...
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.