反序列化的XML使用XmlSerializer的符号() [英] Deserialize XML with ampersand using XmlSerializer()
本文介绍了反序列化的XML使用XmlSerializer的符号()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当XML有类似的数据下面的code休息洛德与放大器;法眼。有什么建议么? 谢谢你,肯
私人牛逼的GetResponse< T>(字符串的APIObject,查询字符串)
{
//反序列化XML转换成指定的类型。
HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(BuildRequestUri(APIObject中,查询));
使用(HttpWebResponse RESP =(HttpWebResponse)request.GetResponse())
{
尝试
{
XmlSerializer的SER =新的XmlSerializer(typeof运算(T));
返程(T)ser.Deserialize(resp.GetResponseStream());
}
赶上(例外五)
{
错误= e.InnerException.ToString();
返回默认(T);
}
}
}
解决方案
XML标签内部的文字符号 不是由XML标准允许的, 而这样的文件将无法解析 任何XML解析器。
在StackOverflow上其他类似的问题:
- 我如何逃脱XML与符号
- 为什么不能RSS处理&符号?
- 解析XML与符号
- <一个href="http://stackoverflow.com/questions/1744046/deserialize-xml-with-ampersand-using-xmlserializer">Deserialize使用XmlSerializer的() XML与符号
- 还有更多!
The following code breaks when the XML has data like "Lord & Hogan". Any suggestions? Thanks, Ken
private T GetResponse<T>(String apiObject, String query)
{
//Deserialize XML into the type specified.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(BuildRequestUri(apiObject, query));
using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
{
try
{
XmlSerializer ser = new XmlSerializer(typeof(T));
return (T)ser.Deserialize(resp.GetResponseStream());
}
catch (Exception e)
{
error = e.InnerException.ToString();
return default(T);
}
}
}
解决方案
From here:
A literal ampersand inside an XML tag is not allowed by the XML standard, and such a document will fail to parse by any XML parser.
Other similar questions on StackOverflow:
- How do I escape ampersands in XML
- Why can’t RSS handle the Ampersand?
- parsing XML with ampersand
- Deserialize XML with ampersand using XmlSerializer()
- there are more!
这篇关于反序列化的XML使用XmlSerializer的符号()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文