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