通过System.Xml.Serialization加载XML数据 [英] Load XML Data via System.Xml.Serialization

查看:89
本文介绍了通过System.Xml.Serialization加载XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个成为XmlDocument的类,并通过反序列化将信息放入一个类中(Function LoadConfigSpecific).好的,这个工作很好:)

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);
}



在另一个函数中,我将执行相反操作,将必须拥有的类保存到XmlDocument中.就像您可以在函数SaveConfigSpecific中看到的那样.但这不起作用:sigh:



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();
}



确切的是,当他们将数据加载到XmlDoc时发生了什么



Exactly what happen is when they will load the data to the XmlDoc

m_cXmlDoc.Load(oStream);


他们将退出并显示错误异常
System.Xml.XmlException-消息="Falta el elementoraíz". (="Root Element Failed!");

我可以说我怎么做不好:((<
供您参考,下面是我将数据加载到XmlDoc的类,并且它们具有根元素


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
}

推荐答案

调用XmlSerializer.Serialize时,流指针将移动到流的末尾,因此XmlDocument.Load 将失败,因为它不会得到任何数据.尝试在流上调用Seek 以将流指针移到流的开头.
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.


我强烈建议切换到DataContract:

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

它具有最小的干扰性(您只需要向类和成员添加一些属性),可以持久存储任意对象图(不仅是一个对象,甚至不一定是树,而是任意图),它不需要实现任何接口,并且可以不必强制使用任何特殊的基类-完全的自由性和鲁棒性.
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.


这篇关于通过System.Xml.Serialization加载XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆