How to convert this:
public class Person
{
public string Name{ get; set; }
public string Age{ get; set; }
}
public class SectionList
{
public List<Person> PersonList { get; set; }
public string SectionName { get; set; }
}
public static List<SectionList> sectionList = new List<SectionList>();
into this XML:
<SectionList>
<SectionName>1</SectionName>
<PersonList>
<Person>
<Name>Ace</Name>
<Age>30</Age>
</Person>
<Person>
<Name>Zeus</Name>
<Age>2</Age>
</Person>
</PersonList>
</SectionList>
It took me about 6 hours to find and experiment. But not yet successful Please help. Or something else, but must show PersonList too
This code i got But the result was not what I wanted https://stackoverflow.com/a/6115782/8902883
public void SerializeObject<T>(T serializableObject, string fileName)
{
if (serializableObject == null) { return; }
try
{
XmlDocument xmlDocument = new XmlDocument();
XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
using (MemoryStream stream = new MemoryStream())
{
serializer.Serialize(stream, serializableObject);
stream.Position = 0;
xmlDocument.Load(stream);
xmlDocument.Save(fileName);
}
}
catch (Exception ex)
{
//Log exception here
}
}
Output:
<?xml version="1.0"?>
<ArrayOfSectionList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SectionList>
<PersonList />
<SectionName>1</SectionName>
</SectionList>
<SectionList>
<PersonList />
<SectionName>2</SectionName>
</SectionList>
</ArrayOfSectionList>
//
Thank you so much, it's my mistake. After I add a "Person" to the" SectionList"
SectionList.Add (Person)
Person.Clear();
"SectionName" remains, but all "Person" is missing. Now I fixed it. Thank you so much.