471,325 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

XML Serialization and Generic Collection

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; }
}
}
Feb 22 '06 #1
3 11331
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."

Feb 23 '06 #2
Ive got it. In my original code I have private properties as readonly. It
cannot be :)

"Mirek Endys" <Mi****@community.nospam> wrote in message
news:O0*************@TK2MSFTNGP11.phx.gbl...
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; }
}
}

Feb 23 '06 #3
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."

Feb 24 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Joe | last post: by
reply views Thread by crazyone | last post: by
3 posts views Thread by Wiebe Tijsma | last post: by
2 posts views Thread by Peter Duniho | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.