views:

193

answers:

3

I have some XML that I am trying serialize like so:

                        string Value = @"<MSG>
                        <ABCID>
                          <ID>0123456789A</ID>
                          <ID>0123456790B</ID>
                        </ABCID>
                        <DATETIME>2010-01-07T13:00:09</DATETIME>
                        </MSG>";

            try
            {
                XmlMsg Msg = (XmlMsg)new XmlSerializer(typeof(XmlMsg)).Deserialize(new System.IO.StringReader(Value));
            }
            catch (System.Exception ex)
            {

            }

Normally I only receive one ID in the ABCID node so its ok however a new requirement needs more than one ID so when I serialize it I want to see all ID's, at the moment it just shows the first ID.

This is my serializing class:

[XmlRoot("MSG")]
public class XmlMsg
{

    [XmlElement("ABCID", IsNullable = true)]
    public SubNodes AbcId { get; set; }

    [XmlElement("DATETIME", IsNullable = true)]
    public string DateTime { get; set; }
}


public class SubNodes
{



    [XmlElement("ID", IsNullable = true)]
    public string Id { get; set; }

}
A: 

cant you have something like this in your class:

private List<string> _IDList = new List<string>();

...

[XmlElement(ElementName= "ID")]
        public List<string> IDList
        {
            get
            {
                return _IDList;
            }
            set
            {
                _IDList = value;
            }
        }
Mark Redman
A: 

In your code there is actually only one ID for each XmlMsg, since the SubNodes class contains only a single ID property. You probably want to change AbcId into a collection of strings instead.

Anders Fjeldstad
A: 

You need to set the Id property of the SubNodes class as an array element of the appropriate data type. Here's a working example using a console application).

using System;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    [XmlRoot("MSG")]
    public class XmlMsg
    {

        [XmlElement("ABCID", IsNullable = true)]
        public SubNodes AbcId { get; set; }

        [XmlElement("DATETIME", IsNullable = true)]
        public string DateTime { get; set; }
    }

    public class SubNodes
    {
        [XmlElement("ID", IsNullable = true)]
        public string[] Id { get; set; }
    }

    class Program
    {
        static void Main()
        {
            string value = @"<MSG>
                        <ABCID>
                          <ID>0123456789A</ID>
                          <ID>0123456790B</ID>
                        </ABCID>
                        <DATETIME>2010-01-07T13:00:09</DATETIME>
                        </MSG>";

            try
            {
                XmlMsg message = (XmlMsg)new XmlSerializer(typeof(XmlMsg)).Deserialize(new System.IO.StringReader(value));

                foreach (var subNode in message.AbcId.Id)
                {
                    Console.WriteLine(subNode);
                }

            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.Read();
        }
    }
}
Kane