473,799 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ 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">tes t</myTag>
</root>

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

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

xmlDocument_2.L oad("C:\Inetpub \wwwroot\test_2 .xml")
xmlNodeNew = xmlDocument_2.S electSingleNode ("root/myTag[@myValue='2']")

xmlDocument.Loa d(fileStream)

xmlNode = xmlDocument.Imp ortNode(xmlNode New.FirstChild, True)
xmlDocument.Doc umentElement.Ap pendChild(xmlNo de)
xmlDocument.Sav e(fileStream)

fileStream.Flus h()
fileStream.Clos e()
End Sub

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

Does anyone know what I'm doing wrong?

Thanks,
Shawn
Nov 12 '05 #1
1 6824
"Shawn" <bo********@hot mail.com> wrote in message news:40******@n ews.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.Begi n)

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
2907
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() function 3. use imagecopyresampled to resize src image and store in newly created image from step 2.
2
9246
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 problems doing this. I wanted to implemented a generic "Helper" class like this: /** * Helper
0
2139
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 file it gives me the error: The instruction at "0x635e0719" referenced memory at "0x00000000". The momory could not be "read. Could anyone please help out with this
3
7313
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 file has one less byte than the original file (although the file itself still works fine). I was hoping someone could explain what is going on (why is there a byte missing, and what might that byte be). string originalFile =...
3
2379
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"?> <StyledLayerDescriptor version="1.0.0" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd"
3
1962
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 here it is. Seems to have something to do with doing ShowDialog Easy to reproduce this project, simply add two forms.
7
1950
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, FileMode.Open,FileAccess.Read)) { try {
2
2027
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 file name (which are both concatenated to filePath) is correct and leads to the existing directory. User on which application is running has full permision to the destination folder. On MSDN stands that FileMode.Append ensures that if file exists...
3
2237
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. Sample Program This small program should suffice to show my problem. I have saved this file as "main.cpp". #include<vector> #include <iostream> struct I{
0
9550
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10269
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10248
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9085
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7573
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5469
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4148
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 we have to send another system
2
3764
muto222
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.