0

I want the script to show max 26 letters and if there is more I want it to make (...) <-- so that you can se there is more letters in the link.

First I put a bit of a script I have for another site containing a variable to do that, however it doesn't work in RSS:

{
            temp.Add(titel);
            count++;
            string titel_kort = titel;
            if (titel.Length > 26)
            {
                titel_kort = titel.Substring(0, 26) + "...";
            }
}

And this is the script I want to integrate to:

@using System.Xml.XPath;
@using System.Xml;

@{
    try
    {
    XmlTextReader udBrudRSS = new XmlTextReader("http://tidende.dk/rss.aspx");

    XmlDocument doc = new XmlDocument();

    doc.Load(udBrudRSS);

    XmlNodeList rssItems = doc.SelectNodes("//item");
    var count = 0;

    foreach (XmlNode node in rssItems )
    {
        count++;
        if (count > 3) { break; }



          <div class="nyhedlink"><a href="@node["link"].InnerText" target="_blank">- @node["title"].InnerText</a></div>
       }
    }
    catch {}
}
Victor Sigler
  • 23,243
  • 14
  • 88
  • 105

1 Answers1

0

You could something like this :

using (var webclient = new WebClient())
{
    var data = webclient.DownloadData("http://tidende.dk/rss.aspx");
    var oReader = new XmlTextReader(new MemoryStream(data));
    var xml = XDocument.Load(oReader);

    var values = xml.XPathSelectElements("//item").Take(3).Select(p => new
    {
        Link = p.XPathSelectElement("//link").Value,
        Title = (p.XPathSelectElement("./title").Value.Length > 26) ?
                 p.XPathSelectElement("./title").Value.Substring(0, 26).Trim() + "..." :
                 p.XPathSelectElement("./title").Value.Trim()
    });

    foreach (var item in values)
    {
        <div class="nyhedlink"><a href="@item.Link" target="_blank">- @item.Title</a></div>        
    }

 }

Sometimes is better use WebClient to make the petition instead of XmlTextReader see this question for a good explanation.

Community
  • 1
  • 1
Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
  • Thx again, this wont work :( xmltest.xml i only have and rss.aspx from the site where i should get the info. Btw. how should the
    look for title and link? `div class="nyhedlink">- @node["title"].InnerText
    `
    – user3605428 May 06 '14 at 21:35
  • That seems to be the problem yes.. rather difficult.. anyhow this is the answer i get `error CS0117: 'umbraco.item' does not contain a definition for 'Link'` i have tryed to figure it out and it might be something with the node, however i cant find the solution. :( – user3605428 May 07 '14 at 20:49
  • @user3605428 All the code before the `foreach` statement has been tested in my machine please debug inside the `foreach` to see the values inside the `item` variable. – Victor Sigler May 09 '14 at 14:11