XmlDocument.Load()方法可以处理unicode字符吗? [英] Can XmlDocument.Load() method handle unicode characters?

查看:91
本文介绍了XmlDocument.Load()方法可以处理unicode字符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,


我花了很长时间试图获得xmldocument.load方法

来处理UTF-8字符,但没有运气。每次加载一个

文档包含欧洲字符(例如下面的那个,从谷歌地图API输出

),它总是说位置无效的字符

229,我相信是?字符。


任何人都可以指出我如何使用xmldocument.load()方法加载这样的

文件,或者其他一些更好的

如何做到这一点?


谢谢!


------------ ---示例XML文件------------------

<?xml version =" 1.0"编码= QUOT; UTF-8英寸?>

- < kml xmlns =" http://earth.google.com/kml/2.0">

- <响应>

< name> germaniastr 134,berlin berlin< / name>

- <状态>

< code> 200< / code>

< request> geocode< / request>

< / Status>

- < Placemark>

<地址> Germaniastra?134,12099 Tempelhof,柏林,德国< /

地址>

- < AddressDetails Accuracy =" 8"

xmlns =" urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">

- < Country>

< CountryNameCode> DE< / CountryNameCode>

- < AdministrativeArea>

< AdministrativeAreaName>柏林< / AdministrativeAreaName>

- < SubAdministrativeArea>

< SubAdministrativeAreaName> Berlin< / SubAdministrativeAreaName>

- < Locality>

< LocalityName> Berlin< / LocalityName> ;

- < DependentLocality>

< DependentLocalityName> Tempelhof< / DependentLocalityName>

- < Thoroughfare>

< ThoroughfareName> Germaniastra?e 134< / ThoroughfareName>

< / Thoroughfare>

- < PostalCode>

< PostalCodeNumber> 12099< / PostalCodeNumber>

< / PostalCode>

< / DependentLocality>

< / Locality>

< / SubAdministrativeArea& gt;

< / AdministrativeArea>

< / Country>

< / AddressDetails>

- < Point>

< coordinates> 13.399486,52.464476,0< / coordinates>

< / Point>

< / Placemark> ;

< /响应>

< / kml>

解决方案

< blockquote> * la*****@gmail.com 在microsoft.public.dotnet.xml中写道:


我花了很长时间试图让xmldocument.load方法处理UTF-8字符,但没有运气。每次加载
文档都包含欧洲字符(例如下面的那个,从谷歌地图API输出
),它总是说位置无效的字符
229,我相信是 ;?"字符。



然后,您的文档很可能不是UTF-8编码的。您将需要检查哪个字节实际位于该位置,例如使用

十六进制编辑器(例如,在Visual Studio中使用File.OpenFile ... / e:Binary)。如果

了?编码为两个字节C3 9F然后'或者不是违规的

字符,或者你有其他的编码问题(例如,你可能已经告诉XML了b $ b)处理器文件是US-ASCII编码的。


请注意,在Internet Explorer中加载XML文档并复制和粘贴结果并没有任何帮助调试这种

问题,压缩文档并将其加载到某个Web服务器

是一种更明智的方法。

-

Bj?rn H?hrmann·mailto:bj **** @ hoehrmann.de· http://bjoern.hoehrmann.de

Weinh。海峡。 22·Telefon:+49(0)621/4309674· http://www.bjoernsworld.de

68309曼海姆·PGP Pub。 KeyID:0xA4357E78· http://www.websitedev.de/


感谢您的回复,Bj?rn。由于此文件来自在线的

动态URL,我只是使用XmlDocument.Load(URL)方法来加载xml文件。在这种情况下,如何在加载文档之前告诉XML处理器编码文件的
是多少?我从IE的文件中保存了

样本XML文件(动态地从谷歌地图生成) -


>保存作为...,并将文件上传到 http://www.usctimes.com/gmap /



geo.xml。它似乎在浏览器中打开,这是否意味着

什么?


那么很可能你的文件不是UTF- 8编码。您将需要检查哪个字节实际位于该位置,例如使用

十六进制编辑器(例如,在Visual Studio中使用File.OpenFile ... / e:Binary)。如果

了?编码为两个字节C3 9F然后'或者不是违规的

字符,或者你有其他的编码问题(例如,你可能已经告诉XML了b $ b)处理器文件是US-ASCII编码的。


请注意,在Internet Explorer中加载XML文档并复制和粘贴结果并没有任何帮助调试这种

问题,压缩文档并将其加载到某个Web服务器

是一种更明智的方法。

-

Bj?rn H?hrmann·mailto:bjo ... @ hoehrmann.de· http://bjoern.hoehrmann.de

Weinh。海峡。 22·Telefon:+49(0)621/4309674· http://www.bjoernsworld.de

68309曼海姆·PGP Pub。 KeyID:0xA4357E78· http://www.websitedev.de/



la*****@gmail.com 写道:


>

由于此文件来自在线的

动态URL,我只使用了XmlDocument。加载(URL)方法

加载xml文件。在这种情况下,如何在加载文档之前告诉XML处理器编码文件的
是多少?



您不必告诉编码,将URL传递给Load方法

并且XML解析器将检查声明的

编码的XML声明或将检查字节顺序标记,然后将根据那个

信息解码提供给字符的字节。如果那不是

可能会出错。


我已经保存了

示例XML文件(动态地从谷歌地图生成)来自IE的文件 -


>另存为...,并将文件上传到 http://www.usctimes.com/gmap/



geo.xml。它似乎在浏览器中打开,这是否意味着

什么?



它还可以加载.NET和

System.Xml.XmlDocument的Load方法,以便正确编码文件。 .NET解析

就好了(用.NET 1.x和2.0测试过)。


-


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


Dear all,

I''ve spent a long time to try to get the xmldocument.load method
to handle UTF-8 characters, but no luck. Every time it loads a
document contains european characters (such as the one below, output
from google map API), it always said invalid character at position
229, which I believe is the "?" character.

Can anyone point me to the right direction of how to load such
documents using the xmldocument.load() method, or some other better
ways to do this?

Thanks!

---------------sample XML file------------------
<?xml version="1.0" encoding="UTF-8" ?>
- <kml xmlns="http://earth.google.com/kml/2.0">
- <Response>
<name>germaniastr 134, berlin berlin</name>
- <Status>
<code>200</code>
<request>geocode</request>
</Status>
- <Placemark>
<address>Germaniastra?e 134, 12099 Tempelhof, Berlin, Germany</
address>
- <AddressDetails Accuracy="8"
xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
- <Country>
<CountryNameCode>DE</CountryNameCode>
- <AdministrativeArea>
<AdministrativeAreaName>Berlin</AdministrativeAreaName>
- <SubAdministrativeArea>
<SubAdministrativeAreaName>Berlin</SubAdministrativeAreaName>
- <Locality>
<LocalityName>Berlin</LocalityName>
- <DependentLocality>
<DependentLocalityName>Tempelhof</DependentLocalityName>
- <Thoroughfare>
<ThoroughfareName>Germaniastra?e 134</ThoroughfareName>
</Thoroughfare>
- <PostalCode>
<PostalCodeNumber>12099</PostalCodeNumber>
</PostalCode>
</DependentLocality>
</Locality>
</SubAdministrativeArea>
</AdministrativeArea>
</Country>
</AddressDetails>
- <Point>
<coordinates>13.399486,52.464476,0</coordinates>
</Point>
</Placemark>
</Response>
</kml>

解决方案

* la*****@gmail.com wrote in microsoft.public.dotnet.xml:

I''ve spent a long time to try to get the xmldocument.load method
to handle UTF-8 characters, but no luck. Every time it loads a
document contains european characters (such as the one below, output
from google map API), it always said invalid character at position
229, which I believe is the "?" character.

Then it is most likely that your document is not UTF-8 encoded. You will
have to check which bytes are actually at that position, e.g. using a
hex editor (e.g., use File.OpenFile ... /e:Binary in Visual Studio). If
the ? is encoded as two bytes C3 9F then that''s either not the offending
character, or you have other encoding problems (for example, you might
have told the XML processor the document is US-ASCII encoded).

Note that loading XML documents in Internet Explorer and copying and
pasting the results does not help in any way to debug this kind of
problem, compressing the document and loading it up to some web server
is a more sensible approach.
--
Bj?rn H?hrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/


Thanks for your reply, Bj?rn. Since this file is coming from a
dynamic URL online, I just used the XmlDocument.Load(URL) method to
load the xml file. In this case, how do I tell the XML processor what
encoding the file would be before I load the document? I''ve saved the
sample XML file (dynamicaly generated from google map) from IE''s File-

>Save As... , and uploaded the file to http://www.usctimes.com/gmap/

geo.xml . It seems to open fine in the browser, does that means
anything?

Then it is most likely that your document is not UTF-8 encoded. You will
have to check which bytes are actually at that position, e.g. using a
hex editor (e.g., use File.OpenFile ... /e:Binary in Visual Studio). If
the ? is encoded as two bytes C3 9F then that''s either not the offending
character, or you have other encoding problems (for example, you might
have told the XML processor the document is US-ASCII encoded).

Note that loading XML documents in Internet Explorer and copying and
pasting the results does not help in any way to debug this kind of
problem, compressing the document and loading it up to some web server
is a more sensible approach.
--
Bj?rn H?hrmann · mailto:bjo...@hoehrmann.de ·http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 ·http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 ·http://www.websitedev.de/



la*****@gmail.com wrote:

>
Since this file is coming from a
dynamic URL online, I just used the XmlDocument.Load(URL) method to
load the xml file. In this case, how do I tell the XML processor what
encoding the file would be before I load the document?

You don''t have to tell the encoding, pass in the URL to the Load method
and the XML parser will check the XML declaration for the declared
encoding or will check for byte order mark and will then based on that
information decode the bytes served to characters. If that is not
possible you get an error.

I''ve saved the
sample XML file (dynamicaly generated from google map) from IE''s File-

>Save As... , and uploaded the file to http://www.usctimes.com/gmap/

geo.xml . It seems to open fine in the browser, does that means
anything?

It also loads fine with .NET and the Load method of
System.Xml.XmlDocument so that file is properly encoded. And .NET parses
it just fine (tested with .NET 1.x and 2.0).

--

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


这篇关于XmlDocument.Load()方法可以处理unicode字符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆