c# - 为 xml 反序列化自动创建的 C# 类不起作用

标签 c# xml deserialization xml-namespaces

我正在努力为这个 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>

我尝试使用自动创建的代码(在 VisualStudio 12 中:编辑 -> 选择性粘贴 -> 将 XML 粘贴为类):

/// <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 的帮助下反序列化:

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 创建反序列化类吗?

最佳答案

我试了很多东西,终于弄明白了。您发布的 Xml 无效,因为 xsi:type 在反序列化中不起作用。

有效的 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>
                <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>

希望这已经足够清楚了。不确定如何从 Envelope 中删除类型,所以这可能不是您想要的解决方案。

我用于从 Envelope 获取内容的方法是 XDocument.Descendants(elemmentName),它返回数组或该名称的元素列表,然后您可以填充对象。它的工作更多,但我认为它比转换 xml 以进行反序列化更好。

关于c# - 为 xml 反序列化自动创建的 C# 类不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34067090/

相关文章:

java - 如何将 JSON 数组映射/反序列化为 POJO 或 POJO 列表?

java - 具有多级多态类型层次结构的 Jackson 反序列化器

c# - 我无法在 HTML 生成的 PDF 中呈现图片

c# - 调用有错误的存储过程,继续并获取完整输出

php - 使用 php 将数据更新/附加到 xml 文件

xml - 为什么在 XML 中使用空元素?

c# - 使用 Json.Net 反序列化时设置必填字段

c# - 我如何在 Unity3D 中进行触摸操作?

c# - 使用 SqlBulkCopy 时如何防止重复条目?

xml - 尝试检测 PL/SQL 过程中的非法 XML 字符