反序列化XML

反序列化XML

问题描述:

大家好,
以下是我用来反序列化的xml:

Hi All,
below is the xml I am using to deserialize:

<messages>
  <import>
    <errormessage key="1" value="1"></errormessage>
    <errormessage key="2" value="2"></errormessage>
    <errormessage key="3" value="4"></errormessage>
    <errormessage key="4" value="4"></errormessage>
    <errormessage key="5" value="5"></errormessage>
    <errormessage key="6" value="6"></errormessage>
    <errormessage key="7" value="7"></errormessage>
  </import>
  <qc>
    <errormessage key="11" value="11"></errormessage>
    <errormessage key="21" value="21"></errormessage>
    <errormessage key="31" value="41"></errormessage>
    <errormessage key="41" value="41"></errormessage>
    <errormessage key="51" value="51"></errormessage>
    <errormessage key="61" value="61"></errormessage>
    <errormessage key="71" value="71"></errormessage>
  </qc>
</messages>



下面是反序列化该xml文件的代码



and below here is the code to deserialize that xml file

[System.SerializableAttribute()]
    public class Messages
    {
        private IMPORT[] IMPORTField;
        private QC[] QCField;
        [System.Xml.Serialization.XmlElementAttribute("IMPORT")]
        public IMPORT[] IMPORTElement
        {
            get
            {
                return this.IMPORTField;
            }
            set
            {
                this.IMPORTField = value;
            }
        }
        [System.Xml.Serialization.XmlElementAttribute("QC")]
        public QC[] QCElement
        {
            get
            {
                return this.QCField;
            }
            set
            {
                this.QCField = value;
            }
        }
    }
    [System.SerializableAttribute()]
    public class IMPORT
    {
        private ErrorMessage[] pageElementField;
        [System.Xml.Serialization.XmlElementAttribute("ErrorMessage", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public ErrorMessage[] PageElement
        {
            get
            {
                return this.pageElementField;
            }
            set
            {
                this.pageElementField = value;
            }
        }

    }
    [System.SerializableAttribute()]
    public class QC
    {
        private ErrorMessage[] QCpageElementField;
        [System.Xml.Serialization.XmlElementAttribute("ErrorMessage", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public ErrorMessage[] QCPageElement
        {
            get
            {
                return this.QCpageElementField;
            }
            set
            {
                this.QCpageElementField = value;
            }
        }
    }
    [System.SerializableAttribute()]
    public class ErrorMessage
    {
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string key;
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string Value;
    }


现在,我可以获取用于QC和Import的对象.
如果仅需要读取QC数据,该如何处理?
有什么方法可以调用该对象吗?
在此先感谢

问候:
Chaitanya.E
*已删除邮件*
SA


Now I am able to get the objects for both QC and Import.
How do we approach if we need only QC Data to be read?
Is there any way to call that object?
Thanks in Advance

Regards:
Chaitanya.E
*REMOVED MAIL*
SA

首先,这是您的设计问题:您没有看到自己的未来发展.您首先以某种方式说:我要序列化和反序列化
整个消息容器",然后您说:有时只是QC".

(顺便说一句,为清楚起见,我建议您不要违反Microsoft命名规则:消息"类中的名称不应为复数形式,不欢迎使用缩写形式,等等.此外,建议并允许其使用语法来缩写属性)应用时的名称:[System.Serializable]而不是[System.SerializableAttribute])

尽管如此,一般问题本身还是有道理的.

好的.您使用的这种序列化旨在一次序列化和反序列化整个对象树.所有属性都是静态的,因此您无法在运行时修改序列化行为.

第一种选择是在单独的步骤中进行反序列化.首先,对整个事物进行反序列化.这将是一个单独的新对象图.然后将新的QC部分数据克隆到以前存在的对象图上.
不要忘记您的数组是引用类型,所以克隆它,而不仅仅是保存变量副本;). (对于这个琐碎的注释,很抱歉,我只是提到了这种情况.)

第二种选择是首先将整个消息容器分为ImportQC.也就是说,您总是将它们分别序列化,并且仅在代码级别组合它们.每个流将包含QCImport部分,从不包含两者.那意味着分开的流,分开的文件,所以我不知道您是否可以考虑这一点.

有更多的高级选项需要降到较低的级别.可以创建ISerializable的实现而不是属性或自定义序列化程序.但是,正如我在对问题的第一条评论中所问的那样,在您提供更多扩展之前,考虑这些方式绝对是没有意义的.

谢谢.
First, this is a problem of your design: you did not see your own future development. Somehow, you first said: "I want to serialize and deserialize
whole Message container", and later you say: "sometimes, just the QC".

(By the way, for clarity, I would recommend not to violate Microsoft naming rules: you class "Messages" should not have a name in plural, abbreviations are not welcome, etc. Also, it is recommended and allowed by syntax to abbreviate attribute name at the point of application: [System.Serializable] instead of [System.SerializableAttribute])

Nevertheless, the general problem itself makes sense.

All right. This kind of serialization you use is designed to serialize and deserialize whole object tree at once. All the attributes are static, so you cannot modify serialization behavior during run time.

First option is to do deserialization in separate steps. First, deserialize whole thing. It will be a separate new object graph. Then clone new QC part of data onto your previously existing object graph.
Don''t forget your array is reference type, so clone it, not just save a variable copy ;). (Sorry for this trivial note, I mention it just it case.)

Second option is to split whole message container into Import and QC in first place. That is, you always serialize them separately and only combine them at the level of your code. Each stream will contain either QC or Import part, never both. That means separate streams, separate files, so I don''t know if you can consider this.

There are more advances options which require going to lower level. Implementation of ISerializable instead of attributes or custom serializer can be created. But thinking about those ways makes absolutely no sense until you provide more expanations, as I asked you in my first comment to your question.

Thank you.