General serialization of lists in C #

I am trying to serialize multiple object lists in xml. Lists have different types, but they must all have the same attributes in the top list of objects.

What I'm trying to get is the “count” at the top level and the name of the object for all the items in the list:

 <JobResult Count="2">
       <Job>
           <Id>1</Id>
       </Job>
       <Job>
           <Id>2</Id>
       </Job>
    </JobResult>

Then for another list:

 <PersonResult Count="1">
       <Person>
           <Id>1</Id>
       </Person>
 </PersonResult>

The code I use is:

[XmlRoot()]
    public class Result<T>
    {
        [XmlElement()]
        public List<T> Items { get; set; }

        public Result()
        {
            this.Items = new List<T>();
        }

        [XmlAttribute("Count")]
        public int ItemCount
        {
            get
            {
                return this.Items.Count;
            }
            set
            {

            }
        }
    }
var jobs= new Result<Job>();
var persons= new Result<Person>();

I get:

<ResultOfJob Count="2">
       <Item>
           <Id>1</Id>
       </Item>
       <Item>
           <Id>2</Id>
       </Item>
    </ResultOfJob >

I tried to assign the so-called attribute, but instead of & item instead of <_x007B_0_x007D_>.

[XmlElement({0})]
 public List<T> Items { get; set; }

What is the best way to handle element names dynamically?

+3
source share
2 answers

The way I finished this solution creates a result class in a similar way:

public abstract class Result<T> 
{
    [XmlIgnore]
    public abstract List<T> Items {get;set;}

    [XmlAttribute]
    public int ResultCount
    {
        get
        {
            return this.Items.Count;
        }
        set { }
    }


    public Result()
    {
        this.Items = new List<T>();
    }

, ResultCount . - , .

:

public class ChallengesResult : Result<ChallengeResource>
{
    public ChallengesResult()
        : base()
    {
    }


    [XmlElement("Challenge")]
    public override List<ChallengeResource> Items { get; set; }
}

[XmlIgnore] Result [XmlElement].

+3

Job/Person,

public class Job
{
    [XmlElement]
    public int Id;
}

public class Person
{
    [XmlElement]
    public int Id;
}

JobResult Xml,

private static string GetJobResultXml()
{
    var jobs = new Result<Job>();
    jobs.Items.Add(new Job() { Id = 1 });
    jobs.Items.Add(new Job() { Id = 2 });

    XmlSerializerNamespaces xmlnsEmpty = new XmlSerializerNamespaces();
    xmlnsEmpty.Add("", "");

    XmlWriterSettings xws = new XmlWriterSettings();
    xws.OmitXmlDeclaration = true;
    xws.ConformanceLevel = ConformanceLevel.Auto;
    xws.Indent = true;

    XmlAttributeOverrides overrides = new XmlAttributeOverrides();
    XmlAttributes attr = new XmlAttributes();
    attr.XmlRoot = new XmlRootAttribute("JobResult");
    overrides.Add(jobs.GetType(), attr);

    XmlAttributes attr1 = new XmlAttributes();
    attr1.XmlElements.Add(new XmlElementAttribute("Job", typeof(Job)));
    overrides.Add(jobs.GetType(), "Items", attr1);

    StringBuilder xmlString = new StringBuilder();
    using (XmlWriter xtw = XmlTextWriter.Create(xmlString, xws))
    {
        XmlSerializer serializer = new XmlSerializer(jobs.GetType(), overrides);
        serializer.Serialize(xtw, jobs, xmlnsEmpty);

        xtw.Flush();
    }

    return xmlString.ToString();
}

PersonResult xml, ,

private static string GetPersonResultXml()
{
    var jobs = new Result<Person>();
    jobs.Items.Add(new Person() { Id = 1 });
    jobs.Items.Add(new Person() { Id = 2 });

    XmlSerializerNamespaces xmlnsEmpty = new XmlSerializerNamespaces();
    xmlnsEmpty.Add("", "");

    XmlWriterSettings xws = new XmlWriterSettings();
    xws.OmitXmlDeclaration = true;
    xws.ConformanceLevel = ConformanceLevel.Auto;
    xws.Indent = true;

    XmlAttributeOverrides overrides = new XmlAttributeOverrides();
    XmlAttributes attr = new XmlAttributes();
    attr.XmlRoot = new XmlRootAttribute("PersonResult");
    overrides.Add(jobs.GetType(), attr);

    XmlAttributes attr1 = new XmlAttributes();
    attr1.XmlElements.Add(new XmlElementAttribute("Person", typeof(Person)));
    overrides.Add(jobs.GetType(), "Items", attr1);

    StringBuilder xmlString = new StringBuilder();
    using (XmlWriter xtw = XmlTextWriter.Create(xmlString, xws))
    {
        XmlSerializer serializer = new XmlSerializer(jobs.GetType(), overrides);
        serializer.Serialize(xtw, jobs, xmlnsEmpty);

        xtw.Flush();
    }

    return xmlString.ToString();
}

, .

XmlAttributes .

http://msdn.microsoft.com/en-us/library/sx1a4zea(v=vs.80).aspx

0
source

All Articles