Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello all,

I have a class which become a XmlDocument and with a Deserialize put the information in a class (Function LoadConfigSpecific). Ok this work well :)

protected clsConfigGeneric m_cConfigSpecific = null;

private void LoadConfigSpecific()
{
    string sXmlInfo = "";
    sXmlInfo = m_cXmlDoc.OuterXml;
    System.IO.StringReader oStringReader = new System.IO.StringReader(sXmlInfo);

    System.Xml.Serialization.XmlSerializer oXmlSer = new System.Xml.Serialization.XmlSerializer(typeof(clsConfigWEBLog));
    m_cConfigSpecific = new clsConfigWEBLog();
    m_cConfigSpecific = (clsConfigWEBLog)oXmlSer.Deserialize(oStringReader);
}


In a other function I will do the opposite save the class which I have to a XmlDocument. Like do you can see in the function SaveConfigSpecific. But it don't work :sigh:
private void SaveConfigSpecific()
{
   System.Xml.Serialization.XmlSerializer oXmlSer = new System.Xml.Serialization.XmlSerializer(typeof(clsConfigWEBLog));
    oXmlSer.Serialize(oStream, Config);
    m_cXmlDoc.Load(oStream);

    raiseOnEvChangeConfig();
}


Exactly what happen is when they will load the data to the XmlDoc
m_cXmlDoc.Load(oStream);

they will exit with a Error Exception
System.Xml.XmlException - Message = "Falta el elemento raíz." (="Root Element Failed!");

Can me please say how I do bad :((

For your information the Class from which I Load the Data to the XmlDoc is the Following, and they have a Root Element

[XmlRoot("Config")]
public class clsConfigWEBLog : clsConfigGeneric
{
    #region Variables
    private System.DateTime m_dtLastUpdate = new DateTime();
    private clsFtp m_cFtp = new clsFtp();
    private clsTimers m_cTimers = new clsTimers();
    private clsSaveFile m_cSaveFile = new clsSaveFile();
    #endregion
    #region Probiedades
    [XmlAttributeAttribute(DataType = "date")]
    public System.DateTime LastUpdate
    {
        get
        {
            return m_dtLastUpdate;
        }
        set
        {
            try
            {
                m_dtLastUpdate = value;
            }
            catch (Exception eLastUpdate)
            {
                Console.WriteLine("Error al cargar el Valor LastUpdate: " + eLastUpdate.Message);
            }
        }
    }
    [XmlElement(typeof(clsFtp))]
    public clsFtp Ftp
    {
        get
        {
            return m_cFtp;
        }
        set
        {
            m_cFtp = value;
        }
    }
    [XmlElement(typeof(clsTimers))]
    public clsTimers Timers
    {
        get
        {
            return m_cTimers;
        }
        set
        {
            m_cTimers = value;
        }
    }
    [XmlElement(typeof(clsSaveFile))]
    public clsSaveFile SaveFile
    {
        get
        {
            return m_cSaveFile;
        }
        set
        {
            m_cSaveFile = value;
        }
    }
    #endregion
}
Posted

When you call XmlSerializer.Serialize, the stream pointer is moved to the end of the stream, so XmlDocument.Load will fail since it will not get any data. Try calling Seek on the stream to move the stream pointer to the beginning of the stream.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Jan-11 12:45pm    
I did not check up this code (because -- see my answer), but I think such dual use of the same stream means bad design practice (not yours, OP's). It's better to fix design then implementation.
Don't you think so?
Nish Nishant 20-Jan-11 12:49pm    
Yeah I agree, assuming the OP can move to .NET 3.5 or higher.
I strongly recommend switching to DataContract:

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx[^].

It is least obtrusive (you only need to put some attributes to your classes and members), it can persist arbitrary object graph (not just one object, not even necessarily a tree but arbitrary graph), it does not require implementing any interfaces and does not force using any special base classes -- complete freedom and robustness.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900