Reading XML Encoding errors

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

    #1

    Reading XML Encoding errors

    I am programming an XML reader in VB.NET 2005 and it works fairly well.
    Once in a while though I encounter an old XML file without the header
    <?xml version="1.0" encoding="UTF-8"?>
    It craps out on the Load with an error similar to "Invalid character in the
    given encoding. Line 3, position 5475070".
    After some research the character in question is the copyright character. My
    question is how can i force the reader to assume UTF-8?
    It seems like my other newer files do not have this problem, just my older
    files. I want to be able to catch this error
    and then attempt to load the file. It also seems like this older file does
    not have a BOM so Im assuming the XML reader has no idea how to interpret
    it. Im hoping i can force a UTF-8 read of the XML file.

    As a secondary question, it seems like these older XML files were
    originally written out as one or two huge lines. is there a way to output a
    copy
    that is more user readable in the node-type format with line breaks and all?

    Thanks for any help
    AGP



  • Herfried K. Wagner [MVP]

    #2
    Re: Reading XML Encoding errors

    "AGP" <sindizzy.pak@s ofthome.netschr ieb:
    Once in a while though I encounter an old XML file without the header
    <?xml version="1.0" encoding="UTF-8"?>
    It craps out on the Load with an error similar to "Invalid character in
    the given encoding. Line 3, position 5475070".
    After some research the character in question is the copyright character.
    My question is how can i force the reader to assume UTF-8?
    It seems like my other newer files do not have this problem, just my older
    files. I want to be able to catch this error
    and then attempt to load the file. It also seems like this older file does
    not have a BOM so Im assuming the XML reader has no idea how to interpret
    it. Im hoping i can force a UTF-8 read of the XML file.
    IIRC UTF-8 is the default encoding for XML files.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

    Comment

    • AGP

      #3
      Re: Reading XML Encoding errors


      "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atwrot e in message
      news:O13Pgm1AIH A.4584@TK2MSFTN GP03.phx.gbl...
      "AGP" <sindizzy.pak@s ofthome.netschr ieb:
      >Once in a while though I encounter an old XML file without the header
      ><?xml version="1.0" encoding="UTF-8"?>
      >It craps out on the Load with an error similar to "Invalid character in
      >the given encoding. Line 3, position 5475070".
      >After some research the character in question is the copyright character.
      >My question is how can i force the reader to assume UTF-8?
      >It seems like my other newer files do not have this problem, just my
      >older files. I want to be able to catch this error
      >and then attempt to load the file. It also seems like this older file
      >does not have a BOM so Im assuming the XML reader has no idea how to
      >interpret it. Im hoping i can force a UTF-8 read of the XML file.
      >
      IIRC UTF-8 is the default encoding for XML files.
      >
      ok so then why do i get the error? I did a test and loaded the XML into
      notepad
      and then saved that file as Text UTF-8 and it seems that file is read
      correctly. So my
      question is why does the original not load properly?

      AGP


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Reading XML Encoding errors

        "AGP" <sindizzy.pak@s ofthome.netschr ieb:
        >IIRC UTF-8 is the default encoding for XML files.
        >
        ok so then why do i get the error? I did a test and loaded the XML into
        notepad
        and then saved that file as Text UTF-8 and it seems that file is read
        correctly. So my
        question is why does the original not load properly?
        Maybe it's stored in an encoding other than UTF-8, Windows ANSI, for
        example.

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

        Comment

        • Martin Honnen

          #5
          Re: Reading XML Encoding errors

          AGP wrote:
          I will ask but the source could be from a variety of providers so I may end
          up
          with no concrete answer as to whats used for encoding. However i did open
          the file in Notepad and added the XML declaration and then just did a plain
          old save and the file still errors out. But in my mind of there is no
          declaration then
          the functions assume a UTF-8 correct? But not sure why if this is the case
          why
          I still get an error.
          Try whether using
          New StreamReader("f ile.xml", Encoding.Defaul t)
          works with those files.


          --

          Martin Honnen --- MVP XML
          http://JavaScript.FAQTs.com/

          Comment

          Working...