Search code examples
c#xmlserializationxmlreader

C# XML Reader returning none when trying to access XML file


I am deserializing following xml which is generated by serialization.

<BattInfo>
  <Battery>
    <BattName>TestBattery</BattName>
    <NumCellSeries>12</NumCellSeries>
    <NumCellParallel>10</NumCellParallel>
    <CelltoPackResistanceSum>3</CelltoPackResistanceSum>
    <BattThermalResistance>15</BattThermalResistance>
    <BattHeatCapacity>12</BattHeatCapacity>
  </Battery>
</BattInfo> 

Code I am using is:

public class BattModel
{
    public string BattName { get; set; }
    public double NumCellSeries { get; set; }
    public double NumCellParallel { get; set; }
    public double CelltoPackResistanceSum { get; set; }
    public double BattThermalResistance { get; set; }
    public double BattHeatCapacity { get; set; }

}

public class BattInfo
{
    [XmlElement("Battery")]
    public List<BattModel> Battery { get; set; }

    public BattInfo()
    {
        this.Battery = new List<BattModel>();
    }
    public BattInfo(params BattModel[] data) : this()
    {
        this.Battery.AddRange(data);
    }
    public void Save(string filename)
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        XmlWriter writer = XmlWriter.Create(filename, settings);
        XmlSerializer serializer = new XmlSerializer(typeof(BattInfo));
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("", "");
        serializer.Serialize(writer, this,ns);
        writer.Flush();
        writer.Close();
    }
    public BattInfo Load(string filename)
    {
        XmlReader reader = XmlReader.Create(filename);
        XmlSerializer serializer = new XmlSerializer(typeof(BattInfo));
        return (BattInfo)serializer.Deserialize(reader);
    }


}

The xml gets serialized properly. But when I try to read it from the xml that is generated using serialization, it returns none while reading the file through xmlreader.


Solution

  • I have made the Load method static and tried this... It works fine here. Here is the code:

    void Main()
    {
        new BattInfo(new BattModel(){ BattName= "test"}).Save(@"tmp.xml");  
        Console.Out.Write(BattInfo.Load(@"tmp.xml").Battery[0].BattName);
    }
    
    // Define other methods and classes here
    public class BattModel
    {
        public string BattName { get; set; } 
        public double NumCellSeries { get; set; }
        public double NumCellParallel { get; set; }
        public double CelltoPackResistanceSum { get; set; }
        public double BattThermalResistance { get; set; }
        public double BattHeatCapacity { get; set; }
    
    }
    
    public class BattInfo
    {
        [XmlElement("Battery")]
        public List<BattModel> Battery { get; set; }
    
        public BattInfo()
        {
            this.Battery = new List<BattModel>();
        }
        public BattInfo(params BattModel[] data) : this()
        {
            this.Battery.AddRange(data);
        }
        public void Save(string filename)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(filename, settings);
            XmlSerializer serializer = new XmlSerializer(typeof(BattInfo));
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", "");
            serializer.Serialize(writer, this,ns);
            writer.Flush();
            writer.Close();
        }
        public static BattInfo Load(string filename)
        {
            XmlReader reader = XmlReader.Create(filename);
            XmlSerializer serializer = new XmlSerializer(typeof(BattInfo));
            return (BattInfo)serializer.Deserialize(reader);
        }
    } 
    

    Were you expecting the Load method to populate the existing instance you called it from? In short, XmlSerializer is strictly constructive so you can't by design.