根据 XSD 生成的 class 文件反序列化 XML 字符串

Deserialize an XML string based on class file generated with XSD

我正在尝试根据 XSD 模式创建的 classes 反序列化一个 XML 答案,但它总是 returns null。

XML 文件具有这种格式

    <?xml version="1.0" encoding="utf-16"?>
<Responses xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Response>
        <inv_number>1</inv_number>
        <StatusCode>Success</StatusCode>
        <Uid>271D95D28716B37A330A5A476AE530206828B103</Uid>
        <Mark>1000000912965</Mark>
      </Response>
      <Response>
        <inv_number>2</inv_number>
        <StatusCode>ValidationError</StatusCode>
        <Errors>
          <Error>
            <Message>Author AFM is not the same with User AFM</Message>
            <Code>-1</Code>
          </Error>
        </Errors>
      </Response>
</Responses>

我的 class 文件基于 XSD 模式(用 xsd.exe 生成)是:

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.6.1055.0.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class ResponseDoc {

    private ResponseType[] responseField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("response")]
    public ResponseType[] response {
        get {
            return this.responseField;
        }
        set {
            this.responseField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class ResponseType {

    private int entitylineNumberField;

    private string statusCodeField;

    private object[] itemsField;

    /// <remarks/>
    public int entitylineNumber {
        get {
            return this.entitylineNumberField;
        }
        set {
            this.entitylineNumberField = value;
        }
    }

    /// <remarks/>
    public string statusCode {
        get {
            return this.statusCodeField;
        }
        set {
            this.statusCodeField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("entityMark", typeof(long))]
    [System.Xml.Serialization.XmlElementAttribute("entityUid", typeof(string))]
    [System.Xml.Serialization.XmlElementAttribute("errors", typeof(ResponseTypeErrors))]
    public object[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class ResponseTypeErrors {

    private ErrorType[] errorField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("error")]
    public ErrorType[] error {
        get {
            return this.errorField;
        }
        set {
            this.errorField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class ErrorType {

    private string messageField;

    private int codeField;

    /// <remarks/>
    public string message {
        get {
            return this.messageField;
        }
        set {
            this.messageField = value;
        }
    }

    /// <remarks/>
    public int code {
        get {
            return this.codeField;
        }
        set {
            this.codeField = value;
        }
    }
}

我反序列化 XML 答案的代码是

XmlSerializer serializer = new XmlSerializer(typeof(ResponseType),new XmlRootAttribute("Responses"));
ResponseType resultingResponse = (ResponseType)serializer.Deserialize(new StringReader(result));

如果我不把

new XmlRootAttribute("Responses")

它给了我一个例外

system.invalidOperaionException  <Responses xmlns=''> was not expected.

如果我使用它,它只会给我空的结果。 我也尝试删除

[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]

按照建议 但没有任何改变。

尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            StreamReader sReader = new StreamReader(FILENAME);
            //read past the unicode in first line
            sReader.ReadLine();

            XmlReader reader = XmlReader.Create(sReader);

            XmlSerializer serializer = new XmlSerializer(typeof(ResponseDoc));
            ResponseDoc responseDoc = (ResponseDoc)serializer.Deserialize(reader);
        }
    }
    [XmlRoot(ElementName = "Responses", Namespace = "")]
    public partial class ResponseDoc
    {

        private ResponseType[] responseField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElement(ElementName = "Response", Namespace = "")]
        public ResponseType[] response
        {
            get
            {
                return this.responseField;
            }
            set
            {
                this.responseField = value;
            }
        }
    }

    [XmlRoot(ElementName = "Response", Namespace = "")]
    public partial class ResponseType
    {

        private int entitylineNumberField;

        private string statusCodeField;

        private string uid;

        private long entityMark;


        private ResponseTypeErrors[] responseTypeErrors;

        /// <remarks/>
        [XmlElement(ElementName = "inv_number", Namespace = "")]
        public string entitylineNumber
        {
            get
            {
                return this.entitylineNumberField.ToString();
            }
            set
            {
                this.entitylineNumberField = int.Parse(value);
            }
        }

        /// <remarks/>
        [XmlElement(ElementName = "StatusCode", Namespace = "")]
        public string statusCode
        {
            get
            {
                return this.statusCodeField;
            }
            set
            {
                this.statusCodeField = value;
            }
        }

        [XmlElement(ElementName = "Uid", Namespace = "")]
        public string Uid
        {
            get
            {
                return this.uid;
            }
            set
            {
                this.uid = value;
            }
        }


        [XmlElement(ElementName = "entityMark", Namespace = "")]
        public string EntityMark
        {
            get
            {
                return this.entityMark.ToString();
            }
            set
            {
                this.entityMark = long.Parse(value);
            }
        }
        [XmlElement(ElementName = "Errors", Namespace = "")]
        public ResponseTypeErrors[] Errors
        {
            get
            {
                return this.responseTypeErrors;
            }
            set
            {
                this.responseTypeErrors = value;
            }
        }
    }

    [XmlRoot(ElementName = "Errors", Namespace = "")]
    public partial class ResponseTypeErrors
    {

        private ErrorType[] errorField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("Error")]
        public ErrorType[] error
        {
            get
            {
                return this.errorField;
            }
            set
            {
                this.errorField = value;
            }
        }
    }

    [XmlRoot(ElementName = "Error", Namespace = "")]
    public partial class ErrorType
    {

        private string messageField;

        private int codeField;

        /// <remarks/>
        [XmlElement(ElementName = "Message", Namespace = "")]
        public string message
        {
            get
            {
                return this.messageField;
            }
            set
            {
                this.messageField = value;
            }
        }

        /// <remarks/>
        [XmlElement(ElementName = "Code", Namespace = "")]
        public int code
        {
            get
            {
                return this.codeField;
            }
            set
            {
                this.codeField = value;
            }
        }
    }

}