实体对象代码:
/// <summary> /// 微信消息通知基类 /// </summary> [Serializable] [XmlRoot("xml")] public class RequestMessBaseInfo { /// <summary> /// 开发者微信号 /// </summary> [XmlElement("ToUserName")] public string ToUserName { get; set; } /// <summary> /// 发送方帐号(一个OpenID) /// </summary> [XmlElement("FromUserName")] public string FromUserName { get; set; } /// <summary> /// 消息创建时间 (整型) /// </summary> [XmlElement("CreateTime")] public string CreateTime { get; set; } /// <summary> /// 消息类型 /// </summary> [XmlElement("MsgType")] public string MsgType { get; set; } /// <summary> /// 扩展字段 保存接收到的原消息内容 /// </summary> public string PostXml { get; set; } }
/// <summary> /// 微信文本消息通知辅助类 /// </summary> [Serializable] [XmlRoot("xml")] public partial class TextRequestMess : RequestMessBaseInfo { /// <summary> /// 文本消息内容 /// </summary> [XmlElement("Content")] public string Content { get; set; } /// <summary> /// 消息id,64位整型 /// </summary> [XmlElement("MsgId")] public string MsgId { get; set; } }
需要进行反序列化的文本消息内容:
<xml> <ToUserName> <![CDATA[gh_99d549298914]]></ToUserName> <FromUserName> <![CDATA[o3ayljuhKnKZUqdTlk-sMwzU-rJw]]></FromUserName> <CreateTime>1392020290</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[姓名 ]]></Content> <MsgId>5978681621118464471</MsgId> </xml>
XML反序列化辅助类:XmlSerializeHelp
/// <summary> /// /// </summary> public class XmlSerializeHelp { /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="xmlStr"></param> /// <param name="encoding"></param> /// <exception cref="ArgumentNullException"></exception> /// <returns></returns> public static T XmlDeserialize<T>(string xmlStr, Encoding encoding) { if (string.IsNullOrEmpty(xmlStr)) throw new ArgumentNullException("xmlStr"); if (encoding == null) throw new ArgumentNullException("encoding"); var mySerializer = new XmlSerializer(typeof(T)); using (var ms = new MemoryStream(encoding.GetBytes(xmlStr))) { using (var sr = new StreamReader(ms, encoding)) { return (T)mySerializer.Deserialize(sr); } } } /// <summary> /// /// </summary> /// <param name="o"></param> /// <param name="encoding"></param> /// <returns></returns> public static string XmlSerialize(object o, Encoding encoding) { if (o == null) throw new ArgumentNullException("o"); if (encoding == null) throw new ArgumentNullException("encoding"); var ser = new XmlSerializer(o.GetType()); using (var ms = new MemoryStream()) { using (var writer = new XmlTextWriter(ms, encoding)) { writer.Formatting = Formatting.Indented; ser.Serialize(writer, o); } return encoding.GetString(ms.ToArray()); } } }
最后在对 内容进行 反序列化的时候出现 下面错误:
System.InvalidOperationException: 无法生成临时类(result=1)。
error CS0013: 将元数据写入文件“”时发生错误 --“?.”
在 System.Xml.Serialization.Compiler.Compile(Assembly parent, String ns, XmlSerializerCompilerParameters xmlParameters, Evidence evidence)
在 System.Xml.Serialization.TempAssembly.GenerateAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence, XmlSerializerCompilerParameters parameters, Assembly assembly, Hashtable assemblies)
在 System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, String location, Evidence evidence)
在 System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
在 GeneralCommonLibrary.XMLHelp.XMLSerializeHelp.XmlDeserialize[T](String xmlStr, Encoding encoding)
注:以前用的挺好,突然之间服务器就出现这个奇葩错误
网站部署环境:Windows Server 2008
各位大神知道这个问题怎么解决么?
用你的代码能成功反序列化,应该是服务器站点设置问题
XmlElement对应的是对象吧
比如节点
<Content><![CDATA[姓名 ]]></Content>
他还有一个子节点<![CDATA[姓名 ]]>,你得指定这个子节点反序列化成什么类型啊
public partial class TextRequestMess : RequestMessBaseInfo {
/// <summary>
/// 文本消息内容
/// </summary>
[XmlElement("Content")]
public Test Content { get; set; }
}
public class Test
{
[XmlText]
public string Text{get;set;}
}
应该不是我实体的问题,同样的XML内容我测试的时候是可以正常反序列化的。这个是服务器突然出现的错误,检测了一下Windows日志,发现有多处记录虚拟内存不足了,可能是服务器问题。