Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
below is the xml I am using to deserialize:
XML
<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>


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

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
Posted
Updated 13-Dec-10 19:43pm
v2
Comments
JF2015 14-Dec-10 1:43am    
Edited to add code formatting and remove mail address. Never post your mail address to avoid being spammed.
senguptaamlan 14-Dec-10 2:13am    
please provide the deserialization code that you are using
Sergey Alexandrovich Kryukov 14-Dec-10 16:01pm    
Chaitanya, please explain what do you mean by "only QC Data"? You see, serialization is functional: new object graph is created from scratch, using parameter-less constructors. If you think of having some object graph, making changes on Import part of data, and later update just the QC Data leaving previously modified Import data as it, this is not what serialization does in principle.

Still, the problem can be solved in some ways, but before I even think about it, I need better understanding of what you want.

Please clarify.

1 solution

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.
 
Share this answer
 
Comments
Espen Harlinn 26-Feb-11 10:55am    
Good reply - a 5
Sergey Alexandrovich Kryukov 26-Feb-11 20:13pm    
Thank you,
--SA

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