由于字符而无法从 XmlDocument 加载方法获得正确的结果
编辑:这是通过更改 Web 服务解释 GET 请求的方式(为 UTF-8)来解决的。
我将一个字符串发送到一个 URL,该 URL 返回一个 XML 给我。但如果我发送特殊字符(例如 å、ä、ö),生成的 XML 就不正确。我无法让它发挥作用。
string name= "abc def åäö";
//name= Uri.EscapeUriString(address); - i also tried this but it messes up the åäö chars in the resulting xml
string uri = "http://blablabla&address=" + name+ "&outputFormat=xml";
System.Xml.XmlDocument x = new System.Xml.XmlDocument();
x.Load(uri);
XmlElement root = x.DocumentElement;
foreach (XmlNode node in root.ChildNodes)
{
XmlAttribute attr = (XmlAttribute)node.Attributes.GetNamedItem("name");
System.Windows.Forms.MessageBox.Show(attr.Value);
}
EDIT: This was solved by changing how the webservice interprates GET-requests (to UTF-8).
I send a string to an URL that returns an XML to me. But the resulting XML is not correct if I send special characters, such as å, ä, ö. And I cant get it to work.
string name= "abc def åäö";
//name= Uri.EscapeUriString(address); - i also tried this but it messes up the åäö chars in the resulting xml
string uri = "http://blablabla&address=" + name+ "&outputFormat=xml";
System.Xml.XmlDocument x = new System.Xml.XmlDocument();
x.Load(uri);
XmlElement root = x.DocumentElement;
foreach (XmlNode node in root.ChildNodes)
{
XmlAttribute attr = (XmlAttribute)node.Attributes.GetNamedItem("name");
System.Windows.Forms.MessageBox.Show(attr.Value);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道这是否有帮助,但请记住 attr.Value 将转义 Xml 类型转义。不过,这看起来并不是问题所在。不要检查 attr.Value,而是检查 node.OuterXml 以查看是否符合您的期望。
I don't know if this helps, but remember that attr.Value is going to escape Xml-type escaping. That doesn't seem like it could be the problem, though. Instead of checking attr.Value, check node.OuterXml to see if that matches your expectations.
我相信你所做的事情是正确的。您最终应该将字符编码为十六进制数字对,每个数字前面都有一个百分比符号,例如,
å
变为%c3%a5
。 Web 服务器/应用程序服务器应该透明地将这些解码回相应的字符。另请参阅 HttpUtility.UrlEncodeUnicode(string),它将使用替代编码,尽管我不确定这是否会由所有网络服务器处理。
I believe what you are doing is correct. You should end up with the characters encoded as pairs of hex digits, each preceded by a percentage symbol, e.g.
å
becomes%c3%a5
. The web server/application server should transparently decode these back to the corresponding characters.Also see HttpUtility.UrlEncodeUnicode(string), which will uses an alternative encoding, although I'm not sure that this will be handled by all web servers.