由于字符而无法从 XmlDocument 加载方法获得正确的结果

发布于 2024-11-19 22:56:47 字数 703 浏览 1 评论 0原文

编辑:这是通过更改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾`听者〃 2024-11-26 22:56:48

我不知道这是否有帮助,但请记住 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.

埋葬我深情 2024-11-26 22:56:47

我相信你所做的事情是正确的。您最终应该将字符编码为十六进制数字对,每个数字前面都有一个百分比符号,例如,å 变为%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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文