XmlSerializer one Class that has multiple classes properties

Posted by pee2002 on Stack Overflow See other posts from Stack Overflow or by pee2002
Published on 2010-05-29T18:33:31Z Indexed on 2010/05/29 18:42 UTC
Read the original article Hit count: 1251

Filed under:
|

Hi there!

Objective: Serialize all the public properties of the ClassMain:

public class ClassMain
{
    ClassA classA = new ClassA();
    public ClassMain()
    {
        classA.Prop1 = "Prop1";
        classA.Prop2 = "Prop2";
    }

    public string Prop1 { get; set; }
    public string Prop2 { get; set; }

    public ClassA ClassA
    {
        get
        {
            return classA;
        }
    }
}

ClassA:

public class ClassA
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
}

This is how i´m serializing:

    private void button1_Click(object sender, EventArgs e)
    {
        ClassMain classMain = new ClassMain();
        classMain.Prop1 = "Prop1";
        classMain.Prop2 = "Prop2";

        XmlSerializer mySerializer = new XmlSerializer(typeof(ClassMain));
        StreamWriter myWriter = new StreamWriter("xml1.xml");
        mySerializer.Serialize(myWriter, classMain);
        myWriter.Close();
    }

In this case, the xml output is:

<?xml version="1.0" encoding="utf-8"?>

<ClassMain xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <Prop1>Prop1</Prop1>

  <Prop2>Prop2</Prop2>

</ClassMain>

As you see, is lacking the ClassA properties.

Can anyone help me out?

Regards

© Stack Overflow or respective owner

Related posts about .NET

Related posts about xmlserializer