当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《自动创建的用于xml反序列化的C#类不起作用》经验,为你挑选了2个好方法。

我正在努力为这个xml创建反序列化类:



    
        
            
                
                    John Johnos
                    
Some Street 1
24
success

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

/// 
[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;

    /// 
    public EnvelopeBody Body
    {
        get
        {
            return this.bodyField;
        }
        set
        {
            this.bodyField = value;
        }
    }

    /// 
    [System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
    public string encodingStyle
    {
        get
        {
            return this.encodingStyleField;
        }
        set
        {
            this.encodingStyleField = value;
        }
    }
}

/// 
[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;

    /// 
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "")]
    public Response Response
    {
        get
        {
            return this.responseField;
        }
        set
        {
            this.responseField = value;
        }
    }
}

/// 
[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;

    /// 
    public ResponseRecords Records
    {
        get
        {
            return this.recordsField;
        }
        set
        {
            this.recordsField = value;
        }
    }

    /// 
    public ResponseStatus status
    {
        get
        {
            return this.statusField;
        }
        set
        {
            this.statusField = value;
        }
    }
}

/// 
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ResponseRecords
{

    private ResponseRecordsItem itemField;

    private string arrayTypeField;

    /// 
    public ResponseRecordsItem item
    {
        get
        {
            return this.itemField;
        }
        set
        {
            this.itemField = value;
        }
    }

    /// 
    [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;
        }
    }
}

/// 
[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;

    /// 
    public string person
    {
        get
        {
            return this.personField;
        }
        set
        {
            this.personField = value;
        }
    }

    /// 
    public string address
    {
        get
        {
            return this.addressField;
        }
        set
        {
            this.addressField = value;
        }
    }

    /// 
    public byte age
    {
        get
        {
            return this.ageField;
        }
        set
        {
            this.ageField = value;
        }
    }
}

/// 
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ResponseStatus
{

    private ResponseStatusItem itemField;

    private string arrayTypeField;

    /// 
    public ResponseStatusItem item
    {
        get
        {
            return this.itemField;
        }
        set
        {
            this.itemField = value;
        }
    }

    /// 
    [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;
        }
    }
}

/// 
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ResponseStatusItem
{

    private string statusField;

    private object messageField;

    /// 
    public string status
    {
        get
        {
            return this.statusField;
        }
        set
        {
            this.statusField = value;
        }
    }

    /// 
    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 .

你能不能帮我改一下这个xml的反序列化类?



1> 小智..:

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

有效的XML应如下所示:



    
        
            
                
                    John Johnos
                    
Some Street 1
24
success

代码应该如下所示:

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);
}

Autogenerate类将来自:


  
    
      John Johnos
      
Some Street 1
24
success

希望这很清楚.不知道如何从Envelope中删除类型,这可能不是你想要的解决方案.

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



2> Randy..:

为什么不为整个模式生成序列化库?

    从邮件中的URL下载XSD架构文件并将其保存在某处

    http://schemas.xmlsoap.org/soap/encoding/

    打开Visual Studio命令提示符并输入以下命令

    xsd/classes SoapEncoding.xsd

    输出将是一个标题为的文件SoapEncoding.cs.

    将此文件导入项目并尝试再次反序列化该消息.

如果一切顺利,这一切都应该有效.

推荐阅读
黄晓敏3023
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有