C# XML递归反序列化
我正在尝试反序列化一些XML。Kronos_WCF对象反序列化很好,但响应对象没有。我缺少一些递归反序列化技术吗 以下是我试图反序列化的XML:C# XML递归反序列化,c#,xml,serialization,xml-serialization,C#,Xml,Serialization,Xml Serialization,我正在尝试反序列化一些XML。Kronos_WCF对象反序列化很好,但响应对象没有。我缺少一些递归反序列化技术吗 以下是我试图反序列化的XML: <?xml version='1.0' encoding='UTF-8' ?> <Kronos_WFC Version="1.0" WFCVersion="6.2.0.4" TimeStamp="6/15/2011 9:15AM GMT-04:00"> <Response Status="Success" Timeout=
<?xml version='1.0' encoding='UTF-8' ?>
<Kronos_WFC Version="1.0" WFCVersion="6.2.0.4" TimeStamp="6/15/2011 9:15AM GMT-04:00">
<Response Status="Success" Timeout="1800" PersonKey="-1" Object="System" UserName="User" Action="Logon" PersonNumber="User">
</Response>
<Response Status="Success" Object="System" UserName="User" Action="Logoff">
</Response>
</Kronos_WFC>
这是我的反序列化程序:
public static T Deserialize<T>(this string xml)
{
var ser = new XmlSerializer(typeof (T));
object obj;
using (var stringReader = new StringReader(xml))
{
using (var xmlReader = new XmlTextReader(stringReader))
{
obj = ser.Deserialize(xmlReader);
}
}
return (T) obj;
}
publicstatict反序列化(此字符串为xml)
{
var ser=新的XmlSerializer(typeof(T));
对象对象对象;
使用(var stringReader=newstringreader(xml))
{
使用(var xmlReader=新的XmlTextReader(stringReader))
{
obj=序列反序列化(xmlReader);
}
}
返回(T)obj;
}
以下是我在VS2010中看到的屏幕截图:
以下是使用XSD.exe生成的类的代码:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[XmlRootAttribute("Kronos_WFC", Namespace = "", IsNullable = false)]
public class Kronos_WFCType
{
private object[] m_itemsField;
private string m_timeStampField;
private string m_versionField;
private string m_wFcVersionField;
/// <remarks/>
[XmlElementAttribute("Request", typeof(RequestType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[XmlElementAttribute("Response", typeof(ResponseType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[XmlElementAttribute("Transaction", typeof(TransactionType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public object[] Items
{
get
{
return m_itemsField;
}
set
{
m_itemsField = value;
}
}
[XmlAttributeAttribute()]
public string TimeStamp
{
get
{
return m_timeStampField;
}
set
{
m_timeStampField = value;
}
}
/// <remarks/>
[XmlAttributeAttribute()]
public string Version
{
get
{
return m_versionField;
}
set
{
m_versionField = value;
}
}
/// <remarks/>
[XmlAttributeAttribute()]
public string WFCVersion
{
get
{
return m_wFcVersionField;
}
set
{
m_wFcVersionField = value;
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public class ResponseType
{
private string messageField;
private string sequenceField;
private string statusField;
private string transactionSequenceField;
/// <remarks/>
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Message
{
get
{
return messageField;
}
set
{
messageField = value;
}
}
/// <remarks/>
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Sequence
{
get
{
return sequenceField;
}
set
{
sequenceField = value;
}
}
/// <remarks/>
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Status
{
get
{
return statusField;
}
set
{
statusField = value;
}
}
/// <remarks/>
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string TransactionSequence
{
get
{
return transactionSequenceField;
}
set
{
transactionSequenceField = value;
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”、“4.0.30319.1”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[XmlRootAttribute(“Kronos_WFC”,Namespace=”“,IsNullable=false)]
公共类Kronos_WFCType
{
私有对象[]m_itemsField;
私有字符串m_timeStampField;
私有字符串m_versionField;
私有字符串m_wFcVersionField;
///
[xmlementAttribute(“请求”,typeof(RequestType),Form=System.Xml.Schema.XmlSchemaForm.Unqualified]
[XmlElementAttribute(“响应”,typeof(ResponseType),Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[XmlElementAttribute(“Transaction”,typeof(TransactionType),Form=System.Xml.Schema.XmlSchemaForm.Unqualified]
公共对象[]项
{
得到
{
返回m_itemsField;
}
设置
{
m_itemsField=值;
}
}
[XmlAttributeAttribute()]
公共字符串时间戳
{
得到
{
返回m_timestamper字段;
}
设置
{
m_timeStampField=值;
}
}
///
[XmlAttributeAttribute()]
公共字符串版本
{
得到
{
返回m_version字段;
}
设置
{
m_versionField=值;
}
}
///
[XmlAttributeAttribute()]
公共字符串WFCVersion
{
得到
{
返回m_wFcVersionField;
}
设置
{
m_wFcVersionField=值;
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”,“4.0.30319.1”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
公共类响应类型
{
私有字符串消息字段;
私有字符串序列字段;
私有字符串状态字段;
私有字符串transactionSequenceField;
///
[xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共字符串消息
{
得到
{
返回消息字段;
}
设置
{
messageField=值;
}
}
///
[xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共字符串序列
{
得到
{
返回序列字段;
}
设置
{
sequenceField=值;
}
}
///
[xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共字符串状态
{
得到
{
返回状态字段;
}
设置
{
statusField=值;
}
}
///
[xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共字符串事务处理序列
{
得到
{
返回transactionSequenceField;
}
设置
{
transactionSequenceField=值;
}
}
}
任何帮助都将不胜感激。xml中的响应属性似乎与类中的属性不匹配。
响应中定义的状态
具有错误的属性
应该是
[XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Status
事实上是这样的
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
因此,xml反序列化程序正在寻找
<Response><Status>Success</Status></Response>
成功
这将至少允许您反序列化响应。状态
xml代码段似乎与类定义不匹配。如果您可以发布
Kronos\u WFC
类的代码,这会有所帮助。响应
类我添加了与类没有直接关系的类,但您不应该使用新的XmlTextReader()
。自.NET 2.0以来,它一直被弃用。改用XmlReader.Create()
。我注意到了我自己,但它不会填充它找到的那些吗?我从API供应商提供的XSD生成了这个。在使用XSD.exe生成类时,是否有方法设置此设置,或者只需手动更改?谢谢。如果xsd来自某个供应商,而xml响应来自同一个供应商的产品,我会和该供应商谈谈。@Cyberdrew re:xsd我不知道。相反,我将修改提供的XSD以匹配您正在接收的xml,从而生成正确的类。我还要问供应商为什么XSD和示例xml不匹配。谢谢,我会的。谢谢你的帮助。恕我直言,这个产品的API是垃圾。