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

XML deserializer sees zero-length file, "root element is missing"

MC
I have a couple of customers who are using a program that deserializes an
XML file, and on some files, the program fails to see the content of the
file -- treats it as zero length and reports "root element is missing" --
even though in Notepad the file looks normal.

Has anybody ever seen this? Any clues? I have not been able to reproduce
it on my computer. In general, when they e-mail me the files, the files
arrive perfectly normal. But on one occasion the e-mailed files were
zero-length.

Thanks!

Jun 27 '08 #1
11 3856
"MC" <fo**************@www.ai.uga.edu.slash.mcwrote:
I have a couple of customers who are using a program that deserializes
an XML file, and on some files, the program fails to see the content
of the file -- treats it as zero length and reports "root element is
missing" -- even though in Notepad the file looks normal.
Could it be a file permissions issue?

Eq.
Jun 27 '08 #2
On Wed, 23 Apr 2008 06:41:52 -0700, MC
<fo**************@www.ai.uga.edu.slash.mcwrote:
I have a couple of customers who are using a program that deserializes
an XML file, and on some files, the program fails to see the content of
the file -- treats it as zero length and reports "root element is
missing" -- even though in Notepad the file looks normal.

Has anybody ever seen this? Any clues? I have not been able to
reproduce it on my computer. In general, when they e-mail me the files,
the files arrive perfectly normal. But on one occasion the e-mailed
files were zero-length.
If the user was able to actually show you a zero-length file at least
once, that makes me suspicious that the other files that aren't
zero-length are not in fact the actual file that was being read. This
could be because the user provided the wrong file, or because you've got
some sort of race condition in which the files were zero-length when your
program tried to read them, but not when the user looked at them later.

How are the files being generated?

Pete
Jun 27 '08 #3
Does your program actually check to see that it is about to deserialize a
well-formed xml document? And handle this expected condition properly? I
realize this may seem obvious...

-- Peter
To be a success, arm yourself with the tools you need and learn how to use
them.

Site: http://www.eggheadcafe.com
http://petesbloggerama.blogspot.com
http://ittyurl.net
"MC" wrote:
I have a couple of customers who are using a program that deserializes an
XML file, and on some files, the program fails to see the content of the
file -- treats it as zero length and reports "root element is missing" --
even though in Notepad the file looks normal.

Has anybody ever seen this? Any clues? I have not been able to reproduce
it on my computer. In general, when they e-mail me the files, the files
arrive perfectly normal. But on one occasion the e-mailed files were
zero-length.

Thanks!

Jun 27 '08 #4

"Paul E Collins" <fi******************@CL4.orgwrote in message
news:Hp******************************@bt.com...
"MC" <fo**************@www.ai.uga.edu.slash.mcwrote:
>I have a couple of customers who are using a program that deserializes an
XML file, and on some files, the program fails to see the content of the
file -- treats it as zero length and reports "root element is missing" --
even though in Notepad the file looks normal.

Could it be a file permissions issue?
Conceivably. If so, the right exception is not being thrown.

One other clue. Normally, when the customer e-mails me the un-openable
files, they look normal and open just fine in my copy of the software.
Today, however, I was sent two of them which arrived as zero-length files.
So Outlook, or whatever they sent their mail with, had the same problem as
my program trying to open the files.

Jun 27 '08 #5
If the user was able to actually show you a zero-length file at least
once, that makes me suspicious that the other files that aren't
zero-length are not in fact the actual file that was being read.
Good point.
>This could be because the user provided the wrong file, or because you've
got some sort of race condition in which the files were zero-length when
your program tried to read them, but not when the user looked at them
later.

How are the files being generated?
The program is basically for creating and editing files containing a large
data structure.

Loading a file looks like this:

using (StreamReader infile = new StreamReader(new FileStream(filename,
FileMode.Open, FileAccess.Read, FileShare.Read)))
{
XmlSerializer x = new XmlSerializer(this.GetType());
foo j = (foo)x.Deserialize(infile); // foo stands for the name of my
big class
infile.Close();
}

Saving a file looks like this:

using (StreamWriter outfile = new StreamWriter(filename))
{
XmlSerializer x = new XmlSerializer(this.GetType());
x.Serialize(outfile,this);
outfile.Close();
}

Since the loading and saving are done at different times (at least minutes
apart) by the same person, I don't see how it could be a race condition.

Is there some error condition that could result in a zero-length file
(either reading or writing) without throwing an exception?
Jun 27 '08 #6

"Peter Bromberg [C# MVP]" <pb*******@yahoo.NoSpamMaam.comwrote in message
news:4C**********************************@microsof t.com...
Does your program actually check to see that it is about to deserialize a
well-formed xml document? And handle this expected condition properly? I
realize this may seem obvious...
See other posting. I'm not quite sure what you mean. I use
XmlSerializer.Deserialize. But the next step is going to be to add some
code that first opens the file and looks at it (to verify that it exists and
isn't empty) before letting the serializer work.
Jun 27 '08 #7
On Wed, 23 Apr 2008 14:52:44 -0700, Michael A. Covington
<lo**@www.covingtoninnovations.com.for.addresswrot e:
[...]
Since the loading and saving are done at different times (at least
minutes
apart) by the same person, I don't see how it could be a race condition.
I don't either. But then, the above is of course a simplification of
what's really going on. It could be that with a concise-but-complete code
sample, an actual problem could be identified. Of course, I suspect that
if you had such a sample and you knew how to get it to misbehave, you
wouldn't be asking the question. :)
Is there some error condition that could result in a zero-length file
(either reading or writing) without throwing an exception?
I should hope not. I admit, I don't know with 100% certainty, but my
approach to an OS API, Windows included, is that it will do exactly what I
tell it to, unless it tells me otherwise. No error implies it completed
the operation I asked for.

The problem has some characteristics of failing to properly flush/close
your output stream, but a) that doesn't explain why the file would show up
with data later on (i.e. when the user goes to email the file to you), and
b) at least in the simplified version you posted, everything appears to be
fine with respect to making sure the output stream is closed.

So I guess I'm out of guesses. Sorry.

One thing you might try is delivering a version of your program with some
instrumentation in it. Have it log user actions, and perhaps also do some
double-checking when the file's written to make sure it winds up the
length you expect (i.e. 0). Heck, you might even write to a
MemoryStream instead, and then do some verification on the length of the
resulting string, as well as making sure that the file written matches the
length and contents of the MemoryStream. That way the file i/o has some
double-checking in it.

Other than that, it's pretty much a mystery to me. :)

Pete
Jun 27 '08 #8
MC
Latest development: The problem seems to be with the program WRITING
zero-length files, not READING them, and earlier, when the customer sent me
"unopenable" files that opened just fine, I'm thinking they were the wrong
files. (Not 100% sure though.)
using (StreamWriter outfile = new StreamWriter(filename))
{
XmlSerializer x = new XmlSerializer(this.GetType());
x.Serialize(outfile,this);
outfile.Close();
}
Is there any way this could produce a zero-length empty file without
throwing an exception?

Jun 27 '08 #9
"MC" <fo**************@www.ai.uga.edu.slash.mcwrote:
>using (StreamWriter outfile = new StreamWriter(filename))
{
XmlSerializer x = new XmlSerializer(this.GetType());
x.Serialize(outfile,this);
outfile.Close();
}

Is there any way this could produce a zero-length empty file without
throwing an exception?
Is it running on the main GUI thread? If not, and if the *other* thread
doesn't handle exceptions, anything that is thrown may just silently
terminate the thread.

Eq.
Jun 27 '08 #10
MC
Thanks, Pete. My latest thinking is that Microsoft (on
support.microsoft.com) has acknowledged some problems with System.Xml.dll
and there have apparently been some buggy versions of XmlSerializer. I'm
guessing that the two sites where the program misbehaves are sites with one
of the bad versions of that file. So I'm going to urge everyone to do a
Windows Update.

Jun 27 '08 #11
MC

"Paul E Collins" <fi******************@CL4.orgwrote in message
news:6q******************************@bt.com...
"MC" <fo**************@www.ai.uga.edu.slash.mcwrote:
>>using (StreamWriter outfile = new StreamWriter(filename))
{
XmlSerializer x = new XmlSerializer(this.GetType());
x.Serialize(outfile,this);
outfile.Close();
}

Is there any way this could produce a zero-length empty file without
throwing an exception?

Is it running on the main GUI thread? If not, and if the *other* thread
doesn't handle exceptions, anything that is thrown may just silently
terminate the thread.
Good thought, but the app is not multi-threaded.

Jun 27 '08 #12

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

Similar topics

1
by: Lana | last post by:
Hello all! I need to add another element to an XML file along with its content, but encountered a problem. I tried with the code provided, but it happens so that i can't run the program from my...
2
by: Jim Witte | last post by:
Hi, How do I set a file input element? If it's a text input, I can set the value property. But this doesn't work with file inputs. And no, I can't change it - I'm writing a script to...
2
by: Evgeny Zoldin | last post by:
Hi ALL. I need to simulate send POST data ( - file upload) as it was submitted in Browser. In C# I create instanse of IE, navigate to page with the form, fill in the fields and try to Submit by...
2
by: mark | last post by:
How do I detect that a particular form element is a file upload or if the file upload has worked? In the Python cgi module documentation I found suggested code... form = cgi.FieldStorage()...
0
by: eric.anderson | last post by:
Is there a way to create a file upload element with only server side code?
4
by: rn5a | last post by:
I am storing the physical path of images, along with 3 more columns, in a MS-Access DB table. I want to provide users the option to change this physical image path (as well as the records in the...
1
by: Sebarry | last post by:
Hi, I'm using the following code from the-stickman.com to create a file upload list in Javascript. I want to modify it so that the file element is only added if the user has provided a description...
0
by: sweavo | last post by:
Hi all, (Python 2.5 under cygwin) I'm reading a bunch of XML files and merging them in memory - each file contains a number of packages which are to be merged: for fname in gInfiles: ...
1
by: Sharad Singh | last post by:
I am new to Javascript/AJAX. I need to upload a file to a server using a file input element(using the 'onchange' event). I need to write a javascript/ajax code for the same. I would be very...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.