I have problems in serializing a list.
Here is my class:

public partial class ProblemType 
{
        private List<DateTimeType> dateTimeField;
        public ProblemType()
        {
            this.dateTimeField = new List<DateTimeType>();
        }
        public List<DateTimeType> DateTime
        {
            get
            {
                return this.dateTimeField;
            }
            set
            {
                this.dateTimeField = value;
            }
        }
 }
public partial class DateTimeType
    {
        private CodedDescriptionType2 typeField;
        private string exactDateTimeField;
        public DateTimeType()
        {
            this.typeField = new CodedDescriptionType2();
        }
        public CodedDescriptionType2 Type
        {
            get
            {
                return this.typeField;
            }
            set
            {
                this.typeField = value;
            }
        }
        public string ExactDateTime
        {
            get
            {
                return this.exactDateTimeField;
            }
            set
            {
                this.exactDateTimeField = value;
            }
        }
    }

My calling code is like this:

ProblemType pt =  new ProblemType();
pt.DateTime = GetDateTimeRange();

public List<DateTimeType> GetDateTimeRange()
{
   	List< DateTimeType> dttList = new List< DateTimeType>();
 	DateTimeType dtt1 = new CCRG.DateTimeType();
DateTimeType dtt2 = new CCRG.DateTimeType();
                dtt1.Type = GetType1();
                dtt1.ExactDateTime = GetExactdateTime1();
                dttList.Add(dtt1);
 dtt2.Type = GetType2();
                dtt2.ExactDateTime = GetExactdateTime2();
                dttList.Add(dtt2);
            return dttList;
}

When I seralize I get XMl like this:

<ProblemType>
 <DateTime>
   <DateTimeType>
      <Type><Text>Episode Begin Datetime</Text></Type>
     <ExactDateTime>04/01/2010</ExactDateTime> 
  </DateTimeType>
  <DateTimeType>
      <Type><Text>Episode End Datetime</Text></Type>
      <ExactDateTime>04/02/2010</ExactDateTime> 
  </DateTimeType>
 </DateTime>
</ProblemType>

I have one problem, I need to get rid of the tag <DateTimeType> becasue the receiving program does not validate it. My output should look like this:

<ProblemType>
 <DateTime>
  <Type><Text>Episode Begin Datetime</Text></Type>
  <ExactDateTime>04/01/2010</ExactDateTime> 
  </DateTime>
<DateTime>
  <Type><Text>Episode End Datetime</Text></Type>
  <ExactDateTime>04/02/2010</ExactDateTime> 
 </DateTime>
</ProblemType>

I have tried several methods and failed. Can somene please help me fix this problem?

Recommended Answers

All 4 Replies

I am not sure that you can do what you want with the built in serializers.
You may need to develop your own.
How are you doing the serialization now?

cGH = new cGHHerlper()
cG = cGH.GetRecord("CG");
xsg = new XmlSerializer(cG.GetType());
twg = new StreamWriter("cG.xml");
xsg.Serialize(twg, cG);
twg.Close();

nick.crane is correct. Since you are using the built in serializer, and you have the DateTimeType object, it's going to include it. Implement IXmlSerializable and define how you want your objects serialized.

I think that the following might do what you want.
It uses XmlAttributeOverrides to remove the list element.

// create object to serialize
ProblemType cG = new ProblemType();
DateTimeType dtt ;
for (int i = 0; i < 2; i++)
{
    dtt = new DateTimeType();
    dtt.ExactDateTime="10/10/2010";
    dtt.Type = new CodedDescriptionType2(string.Format("type {0}",i));
    cG.DateTime.Add(dtt);
}

// create DateTimeType list override
XmlElementAttribute myElementAttribute = new XmlElementAttribute();
myElementAttribute.ElementName = "DateTime";

XmlAttributes myAttributes = new XmlAttributes();
myAttributes.XmlElements.Add(myElementAttribute);

XmlAttributeOverrides myOverrides = new XmlAttributeOverrides();
myOverrides.Add(typeof(ProblemType), "DateTime", myAttributes);
    
// serialise the object
var xsg = new XmlSerializer(cG.GetType(), myOverrides);
var twg = new StreamWriter("cG.xml");
xsg.Serialize(twg, cG);
twg.Close();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.