Solved

XML Read Error: There is no Unicode byte order mark

Posted on 2013-10-29
10
1,605 Views
Last Modified: 2013-10-29
I'm trying to read an XML file that our customer will be sending us weekly.  I'm writing code to read it as below:
        Dim sWorkRequested As String
        Dim sPoNumber As String

        Dim m_xmld As XmlDocument
        Dim m_nodelist As XmlNodeList
        Dim m_node As XmlNode
        m_xmld = New XmlDocument

        m_xmld.Load("JobList.xml")    '<---Error occurs here

        m_nodelist = m_xmld.SelectNodes("/JobRequest")

        For Each m_node In m_nodelist
            sWorkRequested = m_node.Item("WorkRequested").InnerText
            sPoNumber = m_node.Item("PoNumber").InnerText

            Debug.Print("sWorkRequested" & sWorkRequested)
            Debug.Print("sPoNumber" & sPoNumber)
        Next

Open in new window


However I'm getting an error:
"There is no Unicode byte order mark"

Researching the issue, it seems like the first line of the XML
<?xml version="1.0" encoding="utf-16"?>
Should be
<?xml version="1.0">

However, I can't ask the customer to change, I need to take the encoding tag into account.

So, how do I do this within the code sample above?

TIA


Here's a sample of the XML file:
<?xml version="1.0" encoding="utf-16"?>
<JobRequest>
  <WorkRequested>Structural Analysis</WorkRequested>
  <PoNumber>POZ000000076219</PoNumber>
  <PoAmount>500</PoAmount>
</JobRequest>

Open in new window

0
Comment
Question by:Clif
  • 4
  • 4
  • 2
10 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39608740
Is this document really UTF-16 encoded? If so, then it needs byte order marks. Otherwise, you need to ask that the source application change the encoding type. Generally speaking, UTF-8 is probably what you're after.
0
 
LVL 10

Author Comment

by:Clif
ID: 39609018
If, just for grins & giggles, I take out the (encoding="utf-16") tag, the code reads the file correctly.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39609123
Exactly. If you specify UTF-16 in your document, then byte order marks are required. Your document does not provide them, hence the error.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 10

Author Comment

by:Clif
ID: 39609203
I understand the error, as I said in the OP, I have done some research.

The question is, how to resolve the error without asking the customer to change their file (the customer is always right)?

In my research, I've seem some suggestions at a solution, but they were either in C# or I did not understand what was being suggested.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39609564
Can you ask the customer if they intended to send the XML file with the utf field marked with uft-16 when the file they are transmitting is not in that format. Maybe they are not aware that the file is being transmitted in the wrong format. If they say that they are aware of that and it is the way they wish of doing it then you can write code to open the file in text mode and change the utf-16 to utf-8 and save the file back to the file system and then process your XML file as normal.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39609576
If it works without it, then strip it out:

e.g.

Using buffer As New System.IO.StringWriter()
    Using reader As New System.IO.StreamReader("input.xml")
        Dim xmlDeclaration As String = reader.ReadLine()

        xmlDeclaration = xmlDeclaration.Replace("encoding=""utf-16""", String.Empty)

        buffer.WriteLine(xmlDeclaration)

        While Not reader.EndOfStream buffer.WriteLine(reader.ReadLine())
    End Using

    Dim moddedXml As String = buffer.ToString()
    Dim xdoc As New System.Xml.XmlDocument()

    xdoc.LoadXml(moddedXml)
End Using

Open in new window


From a design perspective, you are coding around bad data rather than fixing the bad data. If the user decides to properly encode the file at some point without telling you, then your new logic breaks.
0
 
LVL 10

Author Comment

by:Clif
ID: 39609614
FernandoSoto,
Unfortunately I cannot ask the customer to change their coding.  Apparently it works for their other vendors.



kaufmed,
Your code has an error (While not ended)

If it should be UTF-8, why not do this in your code:
xmlDeclaration = xmlDeclaration.Replace("encoding=""utf-16""", "encoding=""utf-8""")

Would that not solve the issue of it breaking should the customer suddenly decide to code it correctly?

I await your "fixed" code.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39609668
Hi Clif;

My post was not asking you to tell the customer that they are doing it wrong but only to verify that the utf-16 is not matching what they are sending out. If they say this is what they want then all is good and fine, you have documentation of this is, it is called CYA. Otherwise just code around the badly formed XML document as I stated in my last post. Just remember as @kaufmed stated, if they correct the issues in the future that will break your code
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39609909
I had a feeling that line would break. (Too much C# on the brain, I suppose.) The While loop should be:

While Not reader.EndOfStream
    buffer.WriteLine(reader.ReadLine())
End While

Open in new window


If it should be UTF-8, why not do this in your code:
Well, you can, but do you know for a fact that it is encoded as UTF-8? (More than likely it is, so what were talking about here is really more of principle rather than procedure.) Whether you replace the "UTF-16" with an empty string or a "UTF-8" is probably inconsequential for the immediate need.
0
 
LVL 10

Author Closing Comment

by:Clif
ID: 39609973
It works.  I'll keep your concerns in mind.

Thanks.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Pagebreak issue while printing the aspx page 3 24
VB.NET 1 21
No Data for DropDown List 2 22
Help with preventing selection from a combobox 10 18
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question