parsing problem with unicode (utf-16) xml file

S

srikantht

hi,

i am trying to open a xml file which is in unicode format utf-16.
the file is

<?xml version="1.0" encoding="UTF-16"?>
<CurrentWeather>
<Location>Bombay / Santacruz, India (VABB) 19-07N 072-51E
14M</Location>
<Time>Mar 10, 2006 - 12:40 AM EST / 2006.03.10 0540 UTC</Time>
<Wind> from the WSW (240 degrees) at 5 MPH (4 KT):0</Wind>
<Visibility> 1 mile(s):0</Visibility>
<SkyConditions> partly cloudy</SkyConditions>
<Temperature> 80 F (27 C)</Temperature>
<DewPoint> 66 F (19 C)</DewPoint>
<RelativeHumidity> 61%</RelativeHumidity>
<Pressure> 29.94 in. Hg (1014 hPa)</Pressure>
<Status>Success</Status>
</CurrentWeather>

i am using following code to lode the file

XmlDocument doc = new XmlDocument();
doc.Load("C:\\mm.xml");
XmlDeclaration decl = (XmlDeclaration) doc.FirstChild;
Console.WriteLine("\n{0}", "XML DECLARTION:");
Console.WriteLine("{0}", "Version " + "= " + decl.Version);
Console.WriteLine("{0}", "Encoding " + "= " + decl.Encoding);
Console.WriteLine("{0}", "Standalone " + "= " + decl.Standalone);

but i am getting problem at line 3
the error is thers is no unicode byte order mark.cannot switch to
unicode

please help me
 
J

Jon Skeet [C# MVP]

srikantht said:
i am trying to open a xml file which is in unicode format utf-16.

Hmm... works okay for me. Are you *absolutely sure* that the file is
actually in UTF-16? I get the error you've specified if I try to open a
UTF-8 encoded file with the given data.

Jon
 
S

srikantht

hi
i am sure.but if i saved the same file with utf-8 encoding it is
working fine.
 
J

Jon Skeet [C# MVP]

srikantht said:
i am sure.but if i saved the same file with utf-8 encoding it is
working fine.

Could you mail me the UTF-16 version that failed?

Jon
 
J

Jon Skeet [C# MVP]

srikantht said:
i sended that file.
please help me

Right. As I suspected, it's *not* a Unicode file. Look at it in a
binary file editor, and you'll see that there's one byte per character.

What's generating the file? Any reason not to generate UTF-8 to start
with? That's usually more compact.

Jon
 
S

srikantht

hi,
Originally i got this file content from a web service function. I wrote
a client for web services. In it one function returns a string which
has the xml code .so I wrote that string in to mm.xml file which I hv
sended to u.
So please ,tell me wht I hv to do to wrie it in to utf-16 format.

Thank u for replaying.
Srianth
 
J

Jon Skeet [C# MVP]

srikantht said:
Originally i got this file content from a web service function. I wrote
a client for web services. In it one function returns a string which
has the xml code .so I wrote that string in to mm.xml file which I hv
sended to u.
So please ,tell me wht I hv to do to wrie it in to utf-16 format.

When you save the file, make sure you use Encoding.Unicode. For
instance, you might pass that to the constructor of StreamWriter.

Jon
 
N

Nick Hounsome

srikantht said:
hi,
Originally i got this file content from a web service function. I wrote
a client for web services. In it one function returns a string which
has the xml code .so I wrote that string in to mm.xml file which I hv
sended to u.
So please ,tell me wht I hv to do to wrie it in to utf-16 format.

Thank u for replaying.
Srianth

The problem is that default for writing files is utf-8 so you wrote out a
utf-16 string as utf-8 but the content still claims that it is utf-16.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top