加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 07:56:24 所属栏目:百科 来源:网络整理
导读:我正在努力为此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://ww
我正在努力为此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转换为反序列化更好.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读