- XML code
<?xml version="1.0" encoding="utf-16"?><Response> <Id>e5cd5120-219c-4d84-9b1b-b609ecbab930</Id> <Uid>0</Uid> <Name>王五</Name> <Sex>男</Sex> <DOB>1992-3-12 0:00:00</DOB> <MarrigeStatus>1</MarrigeStatus> <IDType>身份证号</IDType> <IDNo>622723199203211421</IDNo> <Address>北京市丰台区</Address> <Zip>744402</Zip> <TelephoneNo>010-8575742</TelephoneNo> <Mobile>13957841245</Mobile> <CreateDate>2011-6-22 14:02:29</CreateDate></Response>
将这个XML文件反序列化成对象:
- C# code
public static Object XmlDeserialize(string xml,Type type) { object obj = null; using (FileStream fs = new FileStream(fileName,FileMode.Open,FileAccess.ReadWrite)) { XmlSerializer xs = new XmlSerializer(type); obj = xs.Deserialize(fs); } return obj; }
用这个方法反序列化结果报错说:
System.Web.Services.Protocols.SoapException: 服务器无法处理请求。 ---> System.InvalidOperationException: XML 文档(2, 2)中有错误。 ---> System.InvalidOperationException: 不应有 <Response xmlns=''>。
在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderUser.Read3_User()
--- 内部异常堆栈跟踪的结尾 ---
在 System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
在 System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
------解决方案--------------------------------------------------------
参考