Solved

Opening utf-16 xml files in Visual Studio 2005

Posted on 2009-05-20
6
660 Views
Last Modified: 2013-11-26
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
Comment
Question by:rdefuria
  • 3
  • 3
6 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24438062
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
 

Author Comment

by:rdefuria
ID: 24442079
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24446580
If they are using XSLT transformation, the default is to use UTF-16 encoding, but you can override that.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:rdefuria
ID: 24446608
Oh, really?   Can you tell me how to have them override that?  That will suffice as a solution...
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 24446624
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
 

Author Closing Comment

by:rdefuria
ID: 31583557
Turns out they could change it from utf-16 to utf-8 using your suggestion.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

861 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now