Morning Guys,
I have a collection that descends from List and has a public property. The Xml serializer does not pick up my proeprty. The list items serialize fine. I have tried the XmlAttribute attribute to no avail. Do you guys have a solution?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
var people = new PersonCollection
{
new Person { FirstName="Sue", Age=17 },
new Person { FirstName="Joe", Age=21 }
};
people.FavoritePerson = "Sue";
var x = new XmlSerializer(people.GetType());
var b = new StringBuilder();
var w = XmlTextWriter.Create(b, new XmlWriterSettings { NewLineChars = "\r\n", Indent = true });
x.Serialize(w, people);
var s = b.ToString();
}
}
[XmlRoot(ElementName="People")]
public class PersonCollection : List<Person>
{
//DOES NOT WORK! ARGHHH
[XmlAttribute]
public string FavoritePerson { get; set; }
}
public class Person
{
[XmlAttribute]
public string FirstName { get; set; }
[XmlAttribute]
public int Age { get; set; }
}
I'm getting the following xml
<?xml version="1.0" encoding="utf-16"?>
<People xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Person FirstName="Sue" Age="17" />
<Person FirstName="Joe" Age="21" />
</People>
I would like to get this
<?xml version="1.0" encoding="utf-16"?>
<People FavoritePerson="Sue" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Person FirstName="Sue" Age="17" />
<Person FirstName="Joe" Age="21" />
</People>
I went ahead and solved the problem by implementing IXmlSerializable. If a simpler solution exists, post it!
[XmlRoot(ElementName="People")]
public class PersonCollection : List<Person>, IXmlSerializable
{
//IT WORKS NOW!!! Too bad we have to implement IXmlSerializable
[XmlAttribute]
public string FavoritePerson { get; set; }
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
FavoritePerson = reader[0];
while (reader.Read())
{
if (reader.Name == "Person")
{
var p = new Person();
p.FirstName = reader[0];
p.Age = int.Parse( reader[1] );
Add(p);
}
}
}
public void WriteXml(XmlWriter writer)
{
writer.WriteAttributeString("FavoritePerson", FavoritePerson);
foreach (var p in this)
{
writer.WriteStartElement("Person");
writer.WriteAttributeString("FirstName", p.FirstName);
writer.WriteAttributeString("Age", p.Age.ToString());
writer.WriteEndElement();
}
}
}
This isn't an answer to the question, but I thought I'd make a suggestion to ease in code development.
Add a new Add
method to the PersonCollection
class as such:
public class PersonCollection : List<Person>, IXmlSerializable
{
...
public void Add(string firstName, int age)
{
this.Add(new Person(firstName, age));
}
...
}
Then, by doing this, you can simplify your collection initializer syntax to:
var people = new PersonCollection
{
{ "Sue", 17 },
{ "Joe", 21 }
};
people.FavoritePerson = "Sue";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With