Link to home
Start Free TrialLog in
Avatar of JRockFL
JRockFLFlag for United States of America

asked on

Deserialize xml

How can I deserialize the values for LineupName and ChannelCount?
I'm able to deserialize the list of channels, my CreateObject method is not returning anything.
<Lineup>
      <LineupName></LineupName>
      <ChannelCount>1</ChannelCount>
      <Channels>
            <ChannelID>1</ChannelID>
            <ChannelNumber>1</ChannelNumber>
            <StationName>Movies</StationName>
            <Category>Movies</Category>
      </Channels>
<Lineup>


            string xmlPath = @"C:\Channels.xml";
            string elementName = "Lineup";
            Lineup lineup = new Lineup();
            lineup.ChannelCount = CreateObject(xmlPath, elementName, typeof(ChannelCount)) as ChannelCount;
            lineup.Channels = CreateList<Channels>(xmlPath, elementName, typeof(List<Channels>));          
            return lineup;



        private List<T> CreateList<T>(string xml, string elementName, Type t)
        {
            XmlSerializer serializer = new XmlSerializer(t, new XmlRootAttribute() { ElementName = elementName });
            return serializer.Deserialize(XmlReader.Create(xml)) as List<T>;
        }

        private object CreateObject(string xml, string elementName, Type t)
        {
            XmlSerializer serializer = new XmlSerializer(t, new XmlRootAttribute() { ElementName = elementName });            
            return serializer.Deserialize(XmlReader.Create(xml));
        }


    [Serializable]
    public class Lineup
    {
        public LineupName LineupName { get; set; }
        public ChannelCount ChannelCount { get; set; }
        public List<Channels> Channels { get; set; }
    }

    [Serializable]
    public class LineupName
    {
        public string Name { get; set; }
    }

    [Serializable]
    public class ChannelCount
    {
        public int Count { get; set; }
    }

    [Serializable]
    public class Channels
    {
        public int ChannelID { get; set; }
        public int ChannelNumber { get; set; }
        public string StationName { get; set; }
        public string Category { get; set; }
    }
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

I suggest that you change your code and xml as follows:

<Lineup>
  <LineupName>Line up Name</LineupName>
  <Channels>
    <Channel>
      <ChannelID>1</ChannelID>
      <ChannelNumber>1</ChannelNumber>
      <StationName>Movie</StationName>
      <Category>Movies</Category>
    </Channel>
  </Channels>
</Lineup>

Open in new window


string xmlPath = @"C:\Channels.xml";
string elementName = "Lineup";
Lineup lineup = (Lineup)CreateObject(xmlPath, elementName, typeof(Lineup));


private List<T> CreateList<T>(string xml, string elementName, Type t)
{
	XmlSerializer serializer = new XmlSerializer(t, new XmlRootAttribute() { ElementName = elementName });
	return serializer.Deserialize(XmlReader.Create(xml)) as List<T>;
}

private object CreateObject(string xml, string elementName, Type t)
{
	XmlSerializer serializer = new XmlSerializer(t, new XmlRootAttribute() { ElementName = elementName });            
	return serializer.Deserialize(XmlReader.Create(xml));
}


	[Serializable]
	public class Lineup
	{
		public string LineupName { get; set; }
		public int ChannelCount
		{
			get
			{
				return Channels.Count;
			}
		}
		
		public List<Channel> Channels { get; set; }
	}

	[Serializable]
	public class Channel
	{
		public int ChannelID { get; set; }
		public int ChannelNumber { get; set; }
		public string StationName { get; set; }
		public string Category { get; set; }
	}

Open in new window

Avatar of saragani
saragani

Hi, if the XML structure is at the following, then you can load all of the LineUp in in deserialization command:

<?xml version="1.0"?>
<Lineup>
  <LineupName>
    <Name>Hello World</Name>
  </LineupName>
  <ChannelCount>
    <Count>1</Count>
  </ChannelCount>
  <Channels>
    <Channels>
      <ChannelID>1</ChannelID>
      <ChannelNumber>1</ChannelNumber>
      <StationName>Movies</StationName>
      <Category>Movies</Category>
    </Channels>
  </Channels>
</Lineup>


And:
            using (FileStream fs = new FileStream(xmlPath, FileMode.Open))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Lineup));
                lineup = (Lineup)serializer.Deserialize(fs);
            };
wdosanjos would work if your LineupName and your ChannelCount are going to be simple (int and string).


However, if you are going to add functions, more properties etc to those classes then you will need to use XML structure.
Avatar of JRockFL

ASKER

Hello. thank you for the replies.
I'm not able to change to format of the XML. That is how it is being provided to me.
Please provide a sample xml that includes more than one channel, as that affects the solution.
Avatar of JRockFL

ASKER

<Lineup>
      <LineupName></LineupName>
      <ChannelCount>1</ChannelCount>
      <Channels>
            <ChannelID>1</ChannelID>
            <ChannelNumber>1</ChannelNumber>
            <StationName>Movies</StationName>
            <Category>Movies</Category>
      </Channels>
      <Channels>
            <ChannelID>2</ChannelID>
            <ChannelNumber>2</ChannelNumber>
            <StationName>Movies</StationName>
            <Category>Movies</Category>
      </Channels>
<Lineup>
ASKER CERTIFIED SOLUTION
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial