?
Solved

Deserialize xml

Posted on 2011-05-09
7
Medium Priority
?
409 Views
Last Modified: 2012-05-11
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; }
    }
0
Comment
Question by:JRockFL
  • 3
  • 2
  • 2
7 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35721697
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

0
 
LVL 11

Expert Comment

by:saragani
ID: 35721707
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);
            };
0
 
LVL 11

Expert Comment

by:saragani
ID: 35721730
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.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 8

Author Comment

by:JRockFL
ID: 35721789
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.
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35721943
Please provide a sample xml that includes more than one channel, as that affects the solution.
0
 
LVL 8

Author Comment

by:JRockFL
ID: 35722088
<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>
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 total points
ID: 35722249
Please try the updated code below.  I changed your classes to match the xml you have.

string xmlPath = @"C:\temp\Channels.xml";
string elementName = "Lineup";
Lineup lineup = (Lineup)CreateObject(xmlPath, elementName, typeof(Lineup));
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 string LineupName { get; set; }
		public int ChannelCount { get; set; }
		[XmlElement]
		public Channels[] Channels { 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; }
	}

Open in new window

0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Suggested Courses

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question