• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 683
  • Last Modified:

Opening utf-16 xml files in Visual Studio 2005

Hello,

If I edit the following xml file in Vistual Studio 2005 (File -> Open -> File...), it shows me nothing as the Error List tab contains the following error:
Error      1      Invalid token 'Text' at root level of document.      C:\Users\rich\Downloads\sample.xml      1      1      Miscellaneous Files

Here's the simple xml file.
<?xml version="1.0" encoding="utf-16"?>
<Report />

Changing the xml file to be utf-8 as follows:
<?xml version="1.0" encoding="utf-8"?>
<AssessmentReport />

allows VS to open the file correctly.

Why is this?  Please note that I am getting this xml file from a third party, so it's not really possible to change the utf-16 to utf-8 in practice.

-Rich
0
rdefuria
Asked:
rdefuria
  • 3
  • 3
1 Solution
 
Bob LearnedCommented:
I really don't understand why any XML needs any more than UTF-8 or ISO-8559-1 (1252) encoding.  I don't know of any way around this, other than changing the encoding.
0
 
rdefuriaAuthor Commented:
Yeah, I agree... but the provider says that is the default output of whatever .NET process they are using to create the .xml.

I guess I am forced to keep editing / reading it with Notepad or Notepad++ instead of VS or MSIE...
0
 
Bob LearnedCommented:
If they are using XSLT transformation, the default is to use UTF-16 encoding, but you can override that.
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
rdefuriaAuthor Commented:
Oh, really?   Can you tell me how to have them override that?  That will suffice as a solution...
0
 
Bob LearnedCommented:
I used to have code for that, but it got lost somewhere...

But, I believe that it is something like this:

http://social.msdn.microsoft.com/forums/en-US/xmlandnetfx/thread/6db1a7a9-6c3a-46ff-a696-0f04d7afbef5

XslCompiledTransform xslt1 = new XslCompiledTransform();
            xslt1.Load(reader1);

            XPathDocument doc1 = new XPathDocument(new StringReader(source));

            StringBuilder sb1 = new StringBuilder();

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Encoding = System.Text.Encoding.UTF8;
            XmlWriter xsltOutput1 = XmlWriter.Create(sb1,settings);

0
 
rdefuriaAuthor Commented:
Turns out they could change it from utf-16 to utf-8 using your suggestion.
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now