2

I have a problem with serializing my objects. I implemented IXmlSerializable interface and initialize object of XmlSerializer(for example, serializer). But sometimes after calling serializer.Serialize(writer, data) my output file looks like this: enter image description here

why do I have such behavior?

public class MyClass : IData
{
        private static readonly XmlSerializer _formatter = new XmlSerializer(typeof(MyData));

    public void Save(string filePath)
        {
            using (StreamWriter writer = new StreamWriter(filePath))
            {
                Save(writer);
                writer.Close();
            }
        }


    public void Save(TextWriter Writer)
        {
            MyData data = GetMyDataObject();

            _formatter.Serialize(Writer, data);
        }

    private MyData GetMyDataObject()
        {
            MyData data = new MyData ();
            foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(typeof(IData)))
                pd.SetValue(data, pd.GetValue(this));
            return data;
        }
}


    public class MyData : IData, IXmlSerializable
{

    public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteAttributeString("Number", Number);
            if (HardwareInformation != null)
            {
                writer.WriteStartElement("HWInfoList");
                foreach (KeyValuePair<string, string> kw in HardwareInformation)
                {
                    writer.WriteStartElement("HWInfo");
                    writer.WriteAttributeString("Key", kw.Key);
                    writer.WriteAttributeString("Value", kw.Value);
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();
            }
        }
}
public interface IData
{

Dictionary<string, string> HardwareInformation { get; set; }
string Number { get; set; }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Developex
  • 328
  • 5
  • 11
  • 3
    More code, please. In particular, did you flush and close the writer properly? – Jeremy McGee Oct 03 '11 at 13:20
  • 1
    hard to say without code...p.s. you can add picture to question not linking somewhere else. – Renatas M. Oct 03 '11 at 13:21
  • Please post the code you've already written, you'll have more chances to get an answer. – Seb Oct 03 '11 at 13:29
  • Also, post a little of your output instead of posting a picture we can't read. – John Saunders Oct 03 '11 at 14:41
  • Did you choose to implement IXmlSerializable because of the dictionary ? There are other (easier) ways to achieve this, like having a property dedicated to the XmlSerialization – Seb Oct 03 '11 at 14:49
  • to Seb! Yes, I chose IXmlSerializable because of the dictionary. Can you describe this achieve more ? – Developex Oct 03 '11 at 15:02

1 Answers1

0

How are you serializing? Here is an example of a Serialize Function

   private XDocument Serialize<T>(object obj)
    {
        XDocument ReturnValue = new XDocument();

        XmlSerializer Serializer = new XmlSerializer(typeof(T));
        System.IO.StringWriter sw = new System.IO.StringWriter();        
        System.IO.StringReader sr; 
        Serializer.Serialize(sw, obj);
        sr = new System.IO.StringReader(sw.ToString());

        ReturnValue = XDocument.Load(sr);

        return ReturnValue;
    }
msmucker0527
  • 5,164
  • 2
  • 22
  • 36