Displaying data from two XML feeds using C# razor

Hi all

I currently have a XML feed displaying ok, the problem is, I need to show data on the same page from another XML feed, as the one below. Just not sure how to add another feed into the mix, any suggestions how I can work/add to the code below?

Do I need to add two: XmlDocument xmlDoc = new XmlDocument();
I’d also like to display data from: http://www.example.com/feed/?post_type=events

@using System
@using System.Xml
@{
    XmlDocument xmlDoc = new XmlDocument();
    string url = "http://www.example.com/feed/?post_type=news";
    xmlDoc.Load(url);

    //Get root node
    XmlElement rss = xmlDoc.DocumentElement;

    //Get channel node
    XmlNode channel = rss.FirstChild;

    //Create XmlNodeList object to hold collection of childNodes
    XmlNodeList childnodes = channel.ChildNodes;
}

@{
    //iterate through xml nodes
    foreach (XmlNode node in childnodes)
    {
        //display RSS feed
        DateTime dt;
        if (node.Name == "item")
        {
            <ul class="event">
                <li class="date-wrapper">
                    <ul class="new-date">
                        @if (DateTime.TryParse(node["pubDate"].InnerText, out dt))
                        {
                            string newDay = dt.ToString("dd");
                            string newMonth = dt.ToString("MMM");
                            string newYear = dt.ToString("yyyy");

                            <li class="date-month">@newMonth</li>
                            <li class="date-day">@newDay</li>
                            <li class="date-year">@newYear</li>
                        }
                    </ul>
                </li>
                <li>
                    <ul>
                        <li><h3><a href="@node["link"].InnerText" target="_blank">@node["title"].InnerText</a></h3></li>
                        <li>@Html.Raw(@node["description"].InnerText)</li>
                    </ul>
                </li>
            </ul>
        }
    }
}

Many thanks,
Barry

What I suggest you do, is create a single object class that contains all the fields you need to display the feed. Then parse each feed into those objects and put them in a list together. Then on your view, you loop through that list and change your view to load that object

Thanks NightStalker

This was not a MVC. I was including razor files inside the CMS, so no view or controller, the code was running as plan logic on the page from the razor view.

I managed to fix it by including the separate snippets(XML feeds) inside their own razor file, I then simpler included these on the page as separate include files, done the trick in this instance :smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.