简体   繁体   中英

Why XMLTextReader returning none when passed StringReader object to it?

I want to deserialize the xml string into an object of the type class defined as follows

   [Serializable]
    [XmlRoot("rt")]
    public class XMLSessionParameters
    {
        [XmlArrayItem("el")]
        public List<Elements> Elements { get; set; }
    }

    public class Elements
    {
        [XmlAttribute("nm")]
        public string Name { get; set; }

        [XmlAttribute("vl")]
        public string Value { get; set; }
    }

Following is the XML, which I want to deserialize

<rt>
  <el nm="Name" vl="ABCD_test"/>
  <el nm="Dual" vl="AA"/>
  <el nm="Quad" vl="ABCD"/>
  <el nm="YYMMDD" vl="120614"/>
</rt>

And following are the methods which I am using for deserialization of the XML string

public static XMLSessionParameters DeserializeSessionParameters(string xmlString)
    {
        XMLSessionParameters parameters = (XMLSessionParameters)Deserialize(typeof(XMLSessionParameters), xmlString);
        XElement root = XElement.Parse(xmlString);
        List<XElement> fileElements = root.Elements().ToList();
        foreach (XElement fileEle in fileElements)
        {
            string xml = fileEle.ToString();
            Elements ele = (Elements)Deserialize(typeof(Elements), xml);                
            parameters.Elements.Add(ele);
        }
        return parameters;
    }

private static object Deserialize(Type type, string XmlString)
{
    XmlSerializer serializer = new XmlSerializer(type);
    StringReader stringReader = new StringReader(XmlString);
    XmlReader xmlReader = new XmlTextReader(stringReader);
    object serializedObj = serializer.Deserialize(xmlReader);
    return serializedObj;
}

When I pass the above mentioned XML as string and when Deserialize function is invoked, XMLReader object posseses the value as None and my program ends abruptly, without giving any exceptions. What can be the cause for such a behavior?

The xml does not match the classes; you need:

    [XmlElement("el")]
    public List<Elements> Elements { get; set; }

Your usage of [XmlArrayItem] (with an implicit [XmlArray] ) would work for the xml:

<rt>
  <Elements>
    <el nm="Name" vl="ABCD_test"/>
    <el nm="Dual" vl="AA"/>
    <el nm="Quad" vl="ABCD"/>
    <el nm="YYMMDD" vl="120614"/>
  </Elements>
</rt>

(note the extra Elements wrapper element). If you use [XmlElement] instead, this is removed. Also: remove [Serializable] : XmlSerializer doesn't need that.

A simple way to check that your attributes are correct: set up some typical objects and serialize them - compare what you get to the xml you need.


Full example:

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

static class Program
{
    static void Main()
    {
        string xmlString = @"<rt>
  <el nm=""Name"" vl=""ABCD_test""/>
  <el nm=""Dual"" vl=""AA""/>
  <el nm=""Quad"" vl=""ABCD""/>
  <el nm=""YYMMDD"" vl=""120614""/>
</rt>";
        XMLSessionParameters parameters = (XMLSessionParameters)Deserialize(typeof(XMLSessionParameters), xmlString);
        // parameters now has 4 elements, all correctly configured
    }
    private static object Deserialize(Type type, string XmlString)
    {
        XmlSerializer serializer = new XmlSerializer(type);
        StringReader stringReader = new StringReader(XmlString);
        XmlReader xmlReader = new XmlTextReader(stringReader);
        object serializedObj = serializer.Deserialize(xmlReader);
        return serializedObj;
    }

}

[XmlRoot("rt")]
public class XMLSessionParameters
{
    [XmlElement("el")]
    public List<Elements> Elements { get; set; }
}

public class Elements
{
    [XmlAttribute("nm")]
    public string Name { get; set; }

    [XmlAttribute("vl")]
    public string Value { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM