0

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.

TFive
  • 31
  • 1
  • 7
  • You can try to refer here: https://learn.microsoft.com/en-us/dotnet/standard/serialization/examples-of-xml-serialization#serializing-a-class-that-contains-a-field-returning-a-complex-object – Miraziz Jan 31 '21 at 10:27

2 Answers2

2

you have made certain mistakes while implementing the code and I am assuming that you have used some xml librabry.

your code should be like below to bind with your models.

using System;
using System.Xml.Serialization;
using System.Collections.Generic;

namespace Xml2CSharp
{
    [XmlRoot(ElementName="Person")]
    public class Person 
    {
        [XmlElement(ElementName="Name")]
        public string Name { get; set; }
    
        [XmlElement(ElementName="Age")]
        public string Age { get; set; }
   }

   [XmlRoot(ElementName="PersonList")]
   public class PersonList 
   {
       [XmlElement(ElementName="Person")]
       public List<Person> Person { get; set; }
   }

   [XmlRoot(ElementName="SectionList")]
   public class SectionList 
   {
       [XmlElement(ElementName="SectionName")]
       public string SectionName { get; set; }
    
       [XmlElement(ElementName="PersonList")]
       public PersonList PersonList { get; set; }
   }
}

Reference: https://xmltocsharp.azurewebsites.net/

Nayan
  • 164
  • 7
2

Try following which eliminates one tag like your original posting:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            Root root = new Root() {
                SectionList =  new List<SectionList>() {
                    new SectionList()
                    {
                        SectionName = "1",
                        PersonList = new List<Person>() {
                            new Person() { Name = "Ace", Age = "30"}
                        }
                    }
                }
            };

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(FILENAME, settings);
            XmlSerializer serializer = new XmlSerializer(typeof(Root));
            serializer.Serialize(writer, root);
        }
    }
    public class Person
    {   
        public string Name{ get; set; }
        public string Age{ get; set; }
    }
    public class Root
    {
        [XmlElement()]
        public List<SectionList> SectionList { get; set; }
    }

    public class SectionList
    {
        [XmlElement("Person")]
        public List<Person> PersonList { get; set; }
        public string SectionName { get; set; }

    }


    
}
jdweng
  • 33,250
  • 2
  • 15
  • 20