Search code examples
c#xmlxmlreaderstringreader

XmlReader creates empty string C#


I am using StringReader to read an xml stream and then I want to read that stream as XML to Serialize and Desiarilize it. Although it was working fine for some reason my XmlReader variable returns null. The XML as I see is the same as before.

My code is:

  StringReader stringReader = new StringReader(result);
        stringReader.ReadLine();//omit the first line that contains xml encoding
        XmlReader xmlReader = XmlReader.Create(stringReader);
        XmlSerializer XmlSerializer = new XmlSerializer(typeof(ResponseDoc));
        ResponseDoc XmlResponseDoc = (ResponseDoc)XmlSerializer.Deserialize(xmlReader);

The XML format of the result viariable is

    <?xml version="1.0" encoding="utf-8"?>
<ResponseDoc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <response>
    <entitylineNumber>1</entitylineNumber>
    <statusCode>Success</statusCode>
    <entityUid>0BA0C06E9CBAA3D890D025A37A577DDB074BA9A9</entityUid>
    <entityMark>1000000115468</entityMark>
  </response>
</ResponseDoc>

My xmlReader variable is null .I also tried to remove the stringReader.ReadLine(); but it didn't change anything. I used that because if the encoding line exists it fails to serialize it.

The ResponseDoc class :

#region XSD Schema
        [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 string markUid;

            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 = "Mark", Namespace = "")]
            public string Mark
            {
                get
                {
                    return this.markUid;
                }
                set
                {
                    this.markUid = 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;
                }
            }

        }

        #endregion

UPDATE: I see that all of you give me answer about my XSD schema. The problem is not to deserialize the XML. My problem is that the variable xmlReader in

XmlReader xmlReader = XmlReader.Create(stringReader);

is {None} altough I have the xml answer in the result variable and I pass it to stringReader. I don't understand why I some give me negative in my question. I believe it is quite clear what is my problem and I am sure I give enough information about my code.If I knew more I would solve it myself. I have upload a screenshot as well with the strinReader and the xmlReader Screenshot


Solution

  • ElementName in System.Xml.Serialization.XmlElement attribute of each property must match the element name in XML. Do the following and you are done with the fix:

    ElementName for ResponseDoc class should be ResponseDoc instead of Responses

    and in ResponseDoc class ElementName for response property should be response instead of Response:

    ResponseDoc class should look like:

    [XmlRoot(ElementName = "ResponseDoc", 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;
            }
        }
    }
    

    and in ResponseType class make sure ElementName for each property is according to XML:

    ElementName for entitylineNumber property should be entitylineNumber instead of inv_number

    ElementName for statusCode property should be statusCode instead of StatusCode

    same rule for other properties as below:

    public partial class ResponseType
        {
            private int entitylineNumberField;
    
            private string statusCodeField;
    
            private string uid;
    
            private long entityMark;
    
            [XmlElement(ElementName = "entitylineNumber", 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 = "entityUid", 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);
                }
            }
        }