XML Serialization and Generic Collection

M

Mirek Endys

I have a problem with XMLSerialization. My object are more complex, but for
the solution i written example of my code bellow.
All is working, in the XML file i have all elements and their attributes, I
have there two XMLElements named MyItem, but these elements has no attribute
'Name'.

Any idea what is wrong?
Thanks.


class Main
{
public void Execute()
{
MyCollection mCol = new MyCollection();
MyObject mObj = new MyObject();

mObj.ItemCol.Add(new MyItem("FirstName"));
mObj.ItemCol.Add(new MyItem("SecondName"));
mCol.Add(mObj);
mCol.Save("C:\\temp.xml");
}
}

class MyCollection : List<MyObject>
{
public void Save(string path)
{
XmlSerializer xmlSerializer = new
XmlSerializer(typeof(MyCollection), new
XmlRootAttribute("MyCollectionStore"));
TextWriter writer = new StreamWriter(path);
xmlSerializer.Serialize(writer, this);
writer.Close();
}
}

class MyObject
{
private List<MyItem> _ItemCol = new List<MyItem>();

[XmlArray]
public List<MyItem> ItemCol
{
get { return _ItemCol; }
set { _ItemCol = value; }
}

}

class MyItem
{

private string _Name;

public MyItem (string name)
{
_Name = name;
}

[XmlAttribute]
public string Name
{
get { return _Name; }
set { _Name = value; }
}
}
 
K

Kevin Yu [MSFT]

Hi Mirek,

The code seems not working. There is something wrong when reflecting type
MyCollection. Could you please post the code that is simplified and works
fine on your machine? Thanks!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
M

Mirek Endys

Ive got it. In my original code I have private properties as readonly. It
cannot be :)
 
K

Kevin Yu [MSFT]

It was nice to know that you have had the problem resolved. Thanks for
sharing your experience with all the people here. If you have any
questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top