System.InvalidOperationException: XML文档(3, 2)中有错误。 ---> System.InvalidOperation
<app>
<name>Cfxpre</name>
<type>三维设计</type>
<icon>/appform/images/apps/cfxpre.png</icon>
<path>/ansys_inc/v145/CFX/bin/cfx5pre</path>
<appname>Cfxpre</appname>
<resource>cfxpre</resource>
<OS>linux</OS>
</app>
<app>
<name>Cfxpost</name>
<type>三维设计</type>
<icon>/appform/images/apps/cfxpost.png</icon>
<path>/ansys_inc/v145/CFX/bin/cfx5post</path>
<appname>Cfxpost</appname>
<resource>cfxpost</resource>
<OS>linux</OS>
</app>
</result>
未处理的异常: System.InvalidOperationException: XML 文档(3, 2)中有错误。 ---> System.InvalidOperationException: 不应有 <result xmlns=''>。
在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderList1.Read7_ArrayOfCloundAppInfo()
--- 内部异常堆栈跟踪的结尾 ---
在 Sysware.Cloud.JHAdaptor.CloudAdaptor.GetAppInfoList(String token) 位置 E:\Clound_Work\Cloud\JH\Sysware.Cloud.JHAdaptor\CloudAdaptor.cs:行号 171
在 Sysware.Cloud.JHTest.Program.Main(String[] args) 位置 E:\Clound_Work\Cloud\JH\Sysware.Cloud.JHTest\Program.cs:行号 28
请按任意键继续. . .
88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
问题
System.InvalidOperationException: XML文档(3, 2)中有错误。 ---> System.InvalidOperationException: 不应有 <result xmlns=''>。
原因:
XML的类型转换不同所造成的;
代码如下
第一
public static List<CloundAppInfo> GetAppInfoList(String token)
{
List<CloundAppInfo> cloundList = new List<CloundAppInfo>();
CloundAppInfo list = new CloundAppInfo();
String appStr = String.Empty;
Hashtable hashTable = new Hashtable();
hashTable.Add("token", token);
appStr = JHGeneralPackag.callRFApi("applist", hashTable);
-----------------------------------这句有问题返回的xml反序列化造成的类型不同---------------------------------
cloundList = XmlUtil.Deserialize(typeof(List<CloundAppInfo>), appStr) as List<CloundAppInfo>;
-------------------------------------------------------------------------------------------------------------
return cloundList;
}
第二
public static object Deserialize(Type p, String xml)
{
using (StringReader sr = new StringReader(xml))
{
XmlSerializer xmldes = new XmlSerializer(p);
------------------------------------这句代码返回object有问题-----------------------------------
Object retXml = xmldes.Deserialize(sr);
-----------------------------------------------------------------------
return retXml;
}
}
注意
我的appstr返回的XML的格式为
<app>
<name>Cfxpre</name>
<type>三维设计</type>
<icon>/appform/images/apps/cfxpre.png</icon>
<path>/ansys_inc/v145/CFX/bin/cfx5pre</path>
<appname>Cfxpre</appname>
<resource>cfxpre</resource>
<OS>linux</OS>
</app>
第三
我的解决方案
private void SerializeObject(string Xmlname)
{
XmlSerializer ser = new XmlSerializer(typeof(对象类型));
TextWriter writer = new StreamWriter(Xmlname);
ser.Serialize(writer, 对象);//要序列化的对象
writer.Close();
}
第二
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[Serializable()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false, ElementName = "Response")]