473,398 Members | 2,343 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,398 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 22628
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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...

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.