Alex Rivera | Logout

XmlRoot() for Xml Serilization does not work

Asked 2009-09-17T19:21:54.377
16

I'm trying to get my httphandler to print out an XML file with the format:

<ScheduledShows>
    <ScheduledShowElement>...</ScheduledShowElement>
    <ScheduledShowElement>...</ScheduledShowElement>
    <ScheduledShowElement>...</ScheduledShowElement>
</ScheduledShows>

But for some reason, the attribute XmlRoot("ScheduledShowElement") in ScheduledShow.cs is not working the way i want it to work. Instead, the output i get is:

<ScheduledShows>
    <ScheduledShow>...<ScheduledShow>
    <ScheduledShow>...<ScheduledShow>
    <ScheduledShow>...<ScheduledShow
</ScheduledShows>

Basically, the name of the node is not being overriden to . How do i get my xml serializer to label nodes as ?

Below is my code and xml output. Thanks!

OneDayScheduleHandler.cs

using System;
using System.Collections.Generic;
using System.Web;

using System.Xml.Serialization;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.IO;
using System.Xml;
using System.Text;
using CommunityServer.Scheduler;
namespace CommunityServer.Scheduler
{

    public class OneDayScheduleHandler : IHttpHandler
    {
        private readonly int NoLimitOnSize = -1;

        public void ProcessRequest(HttpContext context)
        {
            int offsetInDays, timezone, size;

            DateTime selectedDateTime;
            Int32.TryParse(context.Request.QueryString["timezone"], out timezone);
            Int32.TryParse(context.Request.QueryString["daysToOffset"], out offsetInDays);
            if (!String.IsNullOrEmpty(context.Request.QueryString["size"]))
            {
                Int32.TryParse(context.Request.QueryString["size"], out size);
            }
            else
            {
                size = NoLimitOnSize; 
            }

            if (timezone &
Edit
Report

2 Answers

25

I found an answer to my question here:

http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/4b228734-a209-445a-991c-0420b381ac93

I just used [XmlType("")] and that worked.

using System;
using System.Xml.Serialization;


namespace CommunityServer.Scheduler
{

    [XmlType("ScheduledShowElement")]
    public class ScheduledShow
    {

      ...
    }
}
answered 2009-09-18T17:02:51.847
11

[XmlRoot(...)] only affects the outermost element (<ScheduledShows>...</ScheduledShows>). I suspect you want [XmlElement(...)]. Of course, another way is to write an object wrapper:

[XmlRoot("SheduledShows")]
public class Shows {
    [XmlElement("SheduledShowElement")]
    public List<Show> Shows {get;set;}
}

And serialize this wrapper object instead of just a list.

answered 2009-09-17T19:23:03.577

Your Answer