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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MC

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

    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!

  • Paul E Collins

    #2
    Re: XML deserializer sees zero-length file, "root element is missing"

    "MC" <for.address.lo ok@www.ai.uga.e du.slash.mcwrot e:
    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.


    Comment

    • Peter Duniho

      #3
      Re: XML deserializer sees zero-length file, &quot;root element is missing&quot;

      On Wed, 23 Apr 2008 06:41:52 -0700, MC
      <for.address.lo ok@www.ai.uga.e du.slash.mcwrot e:
      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

      Comment

      • =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=

        #4
        RE: XML deserializer sees zero-length file, &quot;root element is missing&quot;

        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




        "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!
        >
        >

        Comment

        • Michael A. Covington

          #5
          Re: XML deserializer sees zero-length file, &quot;root element is missing&quot;


          "Paul E Collins" <find_my_real_a ddress@CL4.orgw rote in message
          news:HpSdnWr9Ao B_0ZLVnZ2dnUVZ8 s6inZ2d@bt.com. ..
          "MC" <for.address.lo ok@www.ai.uga.e du.slash.mcwrot e:
          >
          >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.



          Comment

          • Michael A. Covington

            #6
            Re: XML deserializer sees zero-length file, &quot;root element is missing&quot;

            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(ne w FileStream(file name,
            FileMode.Open, FileAccess.Read , FileShare.Read) ))
            {
            XmlSerializer x = new XmlSerializer(t his.GetType());
            foo j = (foo)x.Deserial ize(infile); // foo stands for the name of my
            big class
            infile.Close();
            }

            Saving a file looks like this:

            using (StreamWriter outfile = new StreamWriter(fi lename))
            {
            XmlSerializer x = new XmlSerializer(t his.GetType());
            x.Serialize(out file,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?


            Comment

            • Michael A. Covington

              #7
              Re: XML deserializer sees zero-length file, &quot;root element is missing&quot;


              "Peter Bromberg [C# MVP]" <pbromberg@yaho o.NoSpamMaam.co mwrote in message
              news:4CD5BCD6-08D0-4679-8B8C-DFA26E27E735@mi crosoft.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.D eserialize. 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.


              Comment

              • Peter Duniho

                #8
                Re: XML deserializer sees zero-length file, &quot;root element is missing&quot;

                On Wed, 23 Apr 2008 14:52:44 -0700, Michael A. Covington
                <look@www.covin gtoninnovations .com.for.addres swrote:
                [...]
                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

                Comment

                • MC

                  #9
                  (New clue) Re: XML deserializer sees zero-length file, &quot;root element is missing&quot;

                  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(fi lename))
                  {
                  XmlSerializer x = new XmlSerializer(t his.GetType());
                  x.Serialize(out file,this);
                  outfile.Close() ;
                  }
                  Is there any way this could produce a zero-length empty file without
                  throwing an exception?

                  Comment

                  • Paul E Collins

                    #10
                    Re: (New clue) Re: XML deserializer sees zero-length file, &quot;root element is missing&quot;

                    "MC" <for.address.lo ok@www.ai.uga.e du.slash.mcwrot e:
                    >using (StreamWriter outfile = new StreamWriter(fi lename))
                    > {
                    > XmlSerializer x = new XmlSerializer(t his.GetType());
                    > x.Serialize(out file,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.


                    Comment

                    • MC

                      #11
                      Re: XML deserializer sees zero-length file, &quot;root element is missing&quot;

                      Thanks, Pete. My latest thinking is that Microsoft (on
                      support.microso ft.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.

                      Comment

                      • MC

                        #12
                        Re: (New clue) Re: XML deserializer sees zero-length file, &quot;root element is missing&quot;


                        "Paul E Collins" <find_my_real_a ddress@CL4.orgw rote in message
                        news:6q2dnaiuH-ZH1I3VnZ2dneKdn ZydnZ2d@bt.com. ..
                        "MC" <for.address.lo ok@www.ai.uga.e du.slash.mcwrot e:
                        >
                        >>using (StreamWriter outfile = new StreamWriter(fi lename))
                        >> {
                        >> XmlSerializer x = new XmlSerializer(t his.GetType());
                        >> x.Serialize(out file,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.

                        Comment

                        Working...