为xml反序列化自动创建的C#类不起作用 [英] Automatically created C# classes for xml deserialization don't work
问题描述
我正在为此XML创建反序列化类:
I am struggling to create deserialization classes for this xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:stn="urn:response">
<SOAP-ENV:Body>
<Response>
<Records xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="stn:Record[1]">
<item xsi:type="stn:Record">
<person xsi:type="xsd:string">John Johnos</person>
<address xsi:type="xsd:string">Some Street 1</address>
<age xsi:type="xsd:string">24</age>
</item>
</Records>
<status xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="stn:status[1]">
<item xsi:type="stn:status">
<status xsi:type="xsd:string">success</status>
<message xsi:type="xsd:string"/>
</item>
</status>
</Response>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
我尝试使用自动创建的代码(在Visual Studio 12中:编辑->选择性粘贴->粘贴XML作为类):
I have tried to use automatically created code (in VisualStudio 12: Edit -> Paste Special -> Paste XML as Classes):
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
public partial class Envelope
{
private EnvelopeBody bodyField;
private string encodingStyleField;
/// <remarks/>
public EnvelopeBody Body
{
get
{
return this.bodyField;
}
set
{
this.bodyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
public string encodingStyle
{
get
{
return this.encodingStyleField;
}
set
{
this.encodingStyleField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public partial class EnvelopeBody
{
private Response responseField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "")]
public Response Response
{
get
{
return this.responseField;
}
set
{
this.responseField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Response
{
private ResponseRecords recordsField;
private ResponseStatus statusField;
/// <remarks/>
public ResponseRecords Records
{
get
{
return this.recordsField;
}
set
{
this.recordsField = value;
}
}
/// <remarks/>
public ResponseStatus status
{
get
{
return this.statusField;
}
set
{
this.statusField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ResponseRecords
{
private ResponseRecordsItem itemField;
private string arrayTypeField;
/// <remarks/>
public ResponseRecordsItem item
{
get
{
return this.itemField;
}
set
{
this.itemField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://schemas.xmlsoap.org/soap/encoding/")]
public string arrayType
{
get
{
return this.arrayTypeField;
}
set
{
this.arrayTypeField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ResponseRecordsItem
{
private string personField;
private string addressField;
private byte ageField;
/// <remarks/>
public string person
{
get
{
return this.personField;
}
set
{
this.personField = value;
}
}
/// <remarks/>
public string address
{
get
{
return this.addressField;
}
set
{
this.addressField = value;
}
}
/// <remarks/>
public byte age
{
get
{
return this.ageField;
}
set
{
this.ageField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ResponseStatus
{
private ResponseStatusItem itemField;
private string arrayTypeField;
/// <remarks/>
public ResponseStatusItem item
{
get
{
return this.itemField;
}
set
{
this.itemField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://schemas.xmlsoap.org/soap/encoding/")]
public string arrayType
{
get
{
return this.arrayTypeField;
}
set
{
this.arrayTypeField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ResponseStatusItem
{
private string statusField;
private object messageField;
/// <remarks/>
public string status
{
get
{
return this.statusField;
}
set
{
this.statusField = value;
}
}
/// <remarks/>
public object message
{
get
{
return this.messageField;
}
set
{
this.messageField = value;
}
}
}
我试图在帮助下进行反序列化的XMLSerializer:
I tried to deserialize with help of XMLSerializer:
var serializer = new XmlSerializer(typeof(Envelope));
var reader = new StringReader(response);
var flResponse = (Envelope)serializer.Deserialize(reader);
我收到的错误消息:
Message=The specified type was not recognized: name='Array', namespace='http://schemas.xmlsoap.org/soap/encoding/', at <Records xmlns=''>.
您能帮我为该xml扩展反序列化类吗?
Could you please help me to greate deserialization classes for this xml?
推荐答案
我尝试了很多事情,最后弄清楚了。您发布的xml无效,因为xsi:type不能在反序列化中使用。
I tried a lot of thing and finaly figured it out. Xml you posted is invalid because xsi:type doesn't work in deserialization.
有效的XML应该如下所示:
Valid XML should look like:
<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:stn="urn:response">
<SOAP-ENV:Body>
<Response>
<Records>
<item>
<person >John Johnos</person>
<address >Some Street 1</address>
<age >24</age>
</item>
</Records>
<status>
<item>
<status >success</status>
<message/>
</item>
</status>
</Response>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
代码应如下所示:
XDocument xml = XDocument.Parse(xmlInput);
XmlSerializer serializer = new XmlSerializer(typeof(Response));
using (StringReader stream = new StringReader(items[0].ToString()))
{
var output = (Response)serializer.Deserialize(stream);
}
自动生成类将来自:
<Response>
<Records xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<item>
<person>John Johnos</person>
<address >Some Street 1</address>
<age>24</age>
</item>
</Records>
<status xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<item >
<status >success</status>
<message />
</item>
</status>
</Response>
希望这很清楚。不知道如何摆脱信封的类型,所以,这可能不是您想要的解决方案。
Hope this is clear enough. Not sure how to get rid of types from Envelope so, this is probably not the solution you want.
我用来从信封中获取东西的方法是 XDocument.Descendants(elemmentName)
返回该名称的数组或元素列表,然后可以填充对象。它的工作更多,但我认为它比转换xml进行反序列化更好。
Method i use for getting things from Envelope is XDocument.Descendants(elemmentName)
which return array or List of elements of that name and then you can fill objects. Its more work, but i think its better than transforming xml for deserialization.
这篇关于为xml反序列化自动创建的C#类不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!