• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 552
  • Last Modified:

Reading xml from ashx

My code works fine for reading from an xml file.
But, now I need to read it from a ashx.
No records are being returned this way. Any ideas?

            string xmlPath = @"C:\Development\Channels.xml";
            //string xmlPath = String.Format(@"http://website.com/data.ashx?zipcode={0}", _zipCode);
            string elementName = "Lineup";

            Lineup lineup = new Lineup();          
            lineup.Channels = CreateList<Channels>(xmlPath, elementName, typeof(List<Channels>));

        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>;
        }
0
JRockFL
Asked:
JRockFL
  • 5
  • 4
1 Solution
 
wdosanjosCommented:
http://website.com/data.ashx?zipcode=33764   returns 404.  That's your issue.
0
 
JRockFLAuthor Commented:
I did not post the real url.
The real url returns results.
0
 
wdosanjosCommented:
I suspect that the content of the C:\Development\Channels.xml file is not quite the same as the content of http://website.com/data.ashx?zipcode={0}

If that's not the case, can you either post the actual url or the content of http://website.com/data.ashx?zipcode={0} ?

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
JRockFLAuthor Commented:
It is the same, I can use an xml document and load it that way.
But i need deserialize it to my list.

I cant post the data
0
 
wdosanjosCommented:
Please try the following.  If that does not work, check the value of the content variable to make sure it's returning what you expect from the site.

        private List<T> CreateList<T>(string xml, string elementName, Type t)
        {
            var content = new System.Net.WebClient().DownloadString(xml);
            XmlSerializer serializer = new XmlSerializer(t, new XmlRootAttribute() { ElementName = elementName });
            return serializer.Deserialize(new StringReader(content)) as List<T>;
        }

Open in new window

0
 
JRockFLAuthor Commented:
That didnt work, but you are right. The content I was working with locally does not follow the same format.

This is the correct format
<Lineup>
      <LineupName></LineupName>
      <ChannelCount>2</ChannelCount>
      <Channels>
            <Channel>
                  <ChannelID>1</ChannelID>
                  <ChannelNumber>1</ChannelNumber>
                  <StationName>Movies</StationName>
                  <Category>Movies</Category>
            </Channel>
            <Channel>
                  <ChannelID>2</ChannelID>
                  <ChannelNumber>2</ChannelNumber>
                  <StationName>Movies</StationName>
                  <Category>Movies</Category>
            </Channel>
      </Channels>
</Lineup>
0
 
JRockFLAuthor Commented:
Ok, Almost there.
lineup.Channels.Count = 1
So it is returning all the channels, but now I need to map each channel to its object and return a list of them

string xmlPath = @"C:\Development\Channels.xml";          
 string elementName = "Lineup";
            Lineup lineup = new Lineup();          
            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>;
        }


    [Serializable]
    public class Lineup
    {
        public string LineupName { get; set; }
        public int ChannelCount { get; set; }
        public List<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; }
    }
0
 
wdosanjosCommented:
Please try the following:

string xmlPath = @"C:\Development\Channels.xml";          
 string elementName = "Lineup";
            Lineup lineup = Deserialize<Lineup>(xmlPath, elementName);          
            return lineup;


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


    [Serializable]
    public class Lineup
    {
        public string LineupName { get; set; }
        public int ChannelCount { get; set; }
        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
 
JRockFLAuthor Commented:
Thank you so much, that worked. I will study this to learn what I did wrong.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now