JRockFL
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</ChannelCo unt>
<Channels>
<ChannelID>1</ChannelID>
<ChannelNumber>1</ChannelN umber>
<StationName>Movies</Stati onName>
<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>(xmlPa th, 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(Xml Reader.Cre ate(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(Xml Reader.Cre ate(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; }
}
I'm able to deserialize the list of channels, my CreateObject method is not returning anything.
<Lineup>
<LineupName></LineupName>
<ChannelCount>1</ChannelCo
<Channels>
<ChannelID>1</ChannelID>
<ChannelNumber>1</ChannelN
<StationName>Movies</Stati
<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>(xmlPa
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(Xml
}
private object CreateObject(string xml, string elementName, Type t)
{
XmlSerializer serializer = new XmlSerializer(t, new XmlRootAttribute() { ElementName = elementName });
return serializer.Deserialize(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; }
}
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</ChannelN umber>
<StationName>Movies</Stati onName>
<Category>Movies</Category >
</Channels>
</Channels>
</Lineup>
And:
using (FileStream fs = new FileStream(xmlPath, FileMode.Open))
{
XmlSerializer serializer = new XmlSerializer(typeof(Lineu p));
lineup = (Lineup)serializer.Deseria lize(fs);
};
<?xml version="1.0"?>
<Lineup>
<LineupName>
<Name>Hello World</Name>
</LineupName>
<ChannelCount>
<Count>1</Count>
</ChannelCount>
<Channels>
<Channels>
<ChannelID>1</ChannelID>
<ChannelNumber>1</ChannelN
<StationName>Movies</Stati
<Category>Movies</Category
</Channels>
</Channels>
</Lineup>
And:
using (FileStream fs = new FileStream(xmlPath, FileMode.Open))
{
XmlSerializer serializer = new XmlSerializer(typeof(Lineu
lineup = (Lineup)serializer.Deseria
};
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.
However, if you are going to add functions, more properties etc to those classes then you will need to use XML structure.
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.
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.
ASKER
<Lineup>
<LineupName></LineupName>
<ChannelCount>1</ChannelCo unt>
<Channels>
<ChannelID>1</ChannelID>
<ChannelNumber>1</ChannelN umber>
<StationName>Movies</Stati onName>
<Category>Movies</Category >
</Channels>
<Channels>
<ChannelID>2</ChannelID>
<ChannelNumber>2</ChannelN umber>
<StationName>Movies</Stati onName>
<Category>Movies</Category >
</Channels>
<Lineup>
<LineupName></LineupName>
<ChannelCount>1</ChannelCo
<Channels>
<ChannelID>1</ChannelID>
<ChannelNumber>1</ChannelN
<StationName>Movies</Stati
<Category>Movies</Category
</Channels>
<Channels>
<ChannelID>2</ChannelID>
<ChannelNumber>2</ChannelN
<StationName>Movies</Stati
<Category>Movies</Category
</Channels>
<Lineup>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
Open in new window