Yoko Yoko

Yoko Yoko

  • NA
  • 12
  • 4.2k

XML Generated with XML serializer is creating empty tags

May 19 2015 1:18 AM
Hello All,
 I feel this is a basic question. Please help me to resolve this. I am new to C# development.
I tried to create an xml out of the student object which has a list of mark objects as a member. 
 
public class student
{
public String Name { get; set; }
public String Class { get; set; }
public List<Mark> MarkList { get; set; }
}
 
public class Mark
{
string Subject { get; set; }
int Score { get; set; }
public Mark(string sub, int score)
{
Subject = sub;
Score = score;
}
public Mark()
{
}
}
 ----------------------------
 
private void savefile(object sender, RoutedEventArgs e)
{
List<Mark> markList = new List<Mark>();
markList.Add(new Mark("Physics",22));
markList.Add(new Mark("Chemistry", 23));
markList.Add(new Mark("Biology", 24));
markList.Add(new Mark("Maths", 25));
student s1 = new student();
s1.Name = "Bob";
s1.Class = "p1";
s1.MarkList =markList;
Serialise(s1);
}
static public void Serialise(student details)
{
XmlSerializer serializer = new XmlSerializer(typeof(student));
using (TextWriter writer = new StreamWriter(@"C:\Xml.xml"))
{
serializer.Serialize(writer, details);
}
}
 
 
On serializing , I am getting the output as follows.
 
<?xml version="1.0" encoding="utf-8"?>
<student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>Bob</Name>
<Class>p1</Class>
<MarkList>
<Mark />
<Mark />
<Mark />
<Mark />
</MarkList>
</student>
The Mark tags are not populating the data.
Please help..
Thanks in advance.
 
 
 
 
 
 

Answers (2)