473,246 Members | 1,667 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,246 software developers and data experts.

How to close this XML file after reading?

I'm using
XDocument document = XDocument.Load (XmlReader.Create (file));

to load in contents of a XML-file for parsing.
However, i noticed that the file seems to be
locked by the process and i can't save changes
made to it in VS until it ends.

How can i close the file?

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Sep 3 '08 #1
8 22570
On Wed, 03 Sep 2008 15:23:25 -0700, K Viltersten <tm**@viltersten.com>
wrote:
I'm using
XDocument document = XDocument.Load (XmlReader.Create (file));

to load in contents of a XML-file for parsing.
However, i noticed that the file seems to be
locked by the process and i can't save changes
made to it in VS until it ends.

How can i close the file?
Assuming that the entire file is parsed in the Load() method, you could
pass it a TextReader instance instead of the filename. Then you've got
access to the i/o object directly and can close it yourself.

It does seem like the XDocument class itself ought to have a Dispose() or
Close(), or maybe just close the source file immediately after parsing. I
might be missing something here. :)

Pete
Sep 3 '08 #2
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Wed, 03 Sep 2008 15:23:25 -0700, K Viltersten <tm**@viltersten.com>
wrote:
>I'm using
XDocument document = XDocument.Load (XmlReader.Create (file));

to load in contents of a XML-file for parsing.
However, i noticed that the file seems to be
locked by the process and i can't save changes
made to it in VS until it ends.

How can i close the file?
You need to close the reader:

XDocument document;
using (var reader = XmlReader.Create(file))
{
document = XDocument.Load(reader);
}
Assuming that the entire file is parsed in the Load() method, you could
pass it a TextReader instance instead of the filename. Then you've got
access to the i/o object directly and can close it yourself.
He's not passing the filename as it is - he's passing XmlReader already.
It does seem like the XDocument class itself ought to have a Dispose() or
Close(), or maybe just close the source file immediately after parsing. I
might be missing something here. :)
XDocument.Load(string) does close the file after loading it.
XDocument.Load(XmlReader), which is used in this case, obviously doesn't.
Sep 4 '08 #3
Peter Duniho <Np*********@nnowslpianmk.comwrote:
I'm using
XDocument document = XDocument.Load (XmlReader.Create (file));

to load in contents of a XML-file for parsing.
However, i noticed that the file seems to be
locked by the process and i can't save changes
made to it in VS until it ends.

How can i close the file?

Assuming that the entire file is parsed in the Load() method, you could
pass it a TextReader instance instead of the filename. Then you've got
access to the i/o object directly and can close it yourself.

It does seem like the XDocument class itself ought to have a Dispose() or
Close(), or maybe just close the source file immediately after parsing. I
might be missing something here. :)
I think it's the XmlReader which needs to be disposed:

XDocument document;
using (XmlReader reader = XmlReader.Create(file))
{
document = XDocument.Load(reader);
}
....

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 4 '08 #4
On Wed, 03 Sep 2008 22:18:42 -0700, Pavel Minaev <in****@gmail.comwrote:
He's not passing the filename as it is - he's passing XmlReader already.
I don't know how you know that. Nor do I know why anyone would name a
variable referring to an XmlReader "file" (sounds a lot more like a
filename to me). But if that helps the OP, great.
>It does seem like the XDocument class itself ought to have a Dispose()
or
Close(), or maybe just close the source file immediately after
parsing. I
might be missing something here. :)

XDocument.Load(string) does close the file after loading it.
I'll take your word that a version of the XDocument.Load(string) method
does, but based on a previous, recent thread (StreamReader not returning
last line of text) where current behavior is obviously different from
earlier versions, I don't see how you know for sure that the OP is using a
version of .NET in which XDocument.Load(string) closes the file.
XDocument.Load(XmlReader), which is used in this case, obviously doesn't.
How do you know that XDocument.Load(XmlReader) is being used? There are
three single-parameter overloads, and even if it's known that for all
versions of .NET Load(string) closes the file, there's still
Load(TextReader) which I would think would behave a lot more like
Load(XmlReader) with respect to closing the input source when done (i.e.
not doing that).

Pete
Sep 4 '08 #5
On Wed, 03 Sep 2008 22:28:34 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
[...]
>It does seem like the XDocument class itself ought to have a Dispose()
or
Close(), or maybe just close the source file immediately after
parsing. I
might be missing something here. :)

I think it's the XmlReader which needs to be disposed:
Well, yes...or the TextReader. My second paragraph was more about what
the class should have in it, as opposed to what it actually does have in
it. :)
Sep 4 '08 #6
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Wed, 03 Sep 2008 22:18:42 -0700, Pavel Minaev <in****@gmail.comwrote:
>He's not passing the filename as it is - he's passing XmlReader already.

I don't know how you know that.
Well, er... let me just repost it:

XDocument.Load(XmlReader.Create(file));
Sep 4 '08 #7
On Wed, 03 Sep 2008 22:55:04 -0700, Pavel Minaev <in****@gmail.comwrote:
>>He's not passing the filename as it is - he's passing XmlReader
already.

I don't know how you know that.

Well, er... let me just repost it:

XDocument.Load(XmlReader.Create(file));
Huh.

I must be too tired. I read that line a _least_ a dozen times, several
times before I replied, and several more after reading your reply, trying
to glean what sort of secrets you were able to extract from the first post.

Heck, even now, having seen your quote, I'm still having trouble picking
out the XmlReader in the original post.

Sigh...
Sep 4 '08 #8
Peter Duniho <Np*********@nnowslpianmk.comwrote:
On Wed, 03 Sep 2008 22:28:34 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
[...]
It does seem like the XDocument class itself ought to have a Dispose()
or
Close(), or maybe just close the source file immediately after
parsing. I
might be missing something here. :)
I think it's the XmlReader which needs to be disposed:

Well, yes...or the TextReader. My second paragraph was more about what
the class should have in it, as opposed to what it actually does have in
it. :)
But I don't think it should really be up to the XDocument to dispose of
anything in this case. It's not allocating the resources, nor wrapping
them in a permanent way, so I think it's entirely reasonable for the
caller to still "own" them and have to dispose of them.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 4 '08 #9

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

Similar topics

4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
2
by: Brian Ward | last post by:
First: sorry as a relative newbie for previously not including code. My question: Reading C++ books I almost always find programs such as the one below give the following type of code for reading...
3
by: Jofio | last post by:
My PHP code to read a simple text file is listed below. What could have gone wrong here? When I run it, the browser displays: 'COULD NOT Read' opton of the fread() function. I thought the code was...
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
7
by: John Dann | last post by:
I'm tripping over a silly little problem when trying to read a text file, but can't see the fix. Outline code is: ---------------------------------------------------- FS=New FileStream(filename,...
2
by: fool | last post by:
Dear group, I am a beginner in php and I was little bit experience in C language. I want to read a file's content and delete the first line of the file and display those lines which has got...
5
by: Anja | last post by:
Hi everyone, I have a question about text file reading with VBA. I want to read he whole contents of the file in one string variable. I have been able to successfully read lines using: Line...
3
by: Archestic | last post by:
Hi, I have been given the task to make a file reading program which will: Take in a text file Read it and display its contents Do a quick word count Do a vowel count Im kind of new to java...
1
by: Iulian Ilea | last post by:
Hello, Let's say that I have a very big string to write into a file. If I concatenate strings and write the string into file at the end of processing than memory use will increase exponential....
1
by: gowda gopala | last post by:
plz anybody help me about file reading, i want to read file only selected part of it. whether it is possible are not?.
0
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
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"....
0
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...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

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.