Hyperlink asp control doesn't appear when set

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mbewers1
    New Member
    • Feb 2009
    • 68

    Hyperlink asp control doesn't appear when set

    Hi, I've got an XML file with one <title> and one <link> attribute.
    What I want to do is implement a hyperlink with the name of the <title> element and whose NavigateUrl property is set to the <link> element.

    For an unusual reason, the hyperlink never appears but, when debugged, both items from the XML file are appended to the hyperlink. What have I done wrong?

    Cheers,
    Matt

    Code:
    private void ReadXML()
        {
            using (StreamReader sr = File.OpenText(xmlFile))
            {
                while (!sr.EndOfStream && sr.Read() > 0)
                {
                    string line = sr.ReadLine();
                    if (line.Contains("<title>"))
                    {
                        // read until we reach </title>
                        HyperLink4.Text = line.Replace("<title>", ""); // read until <title>
                        HyperLink4.Text = line.Replace("</title>", ""); // read until <title>
                    }
                    // link should follow straight after
                    else if (line.Contains("<link>"))
                    {
                        HyperLink4.NavigateUrl = line.Replace("<link>", "");
                        HyperLink4.NavigateUrl = line.Replace("</link>", ""); // read until <title>
                    }
                }
            }
        }
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Could you post your input and expected output?
    It looks like you have something like
    <title>Search </title><link>htt p://www.google.ca</link>
    Which ends up being:
    Searchhttp://www.google.ca

    Did you want your output in html link form, eg
    <a href="http://www.google.ca"> Search</a>
    or some other different form?

    Comment

    • mbewers1
      New Member
      • Feb 2009
      • 68

      #3
      Sure, my input is:

      <rss version="2.0" xmlns:media="ht tp://search.yahoo.co m/mrss/"><channel>
      <title>BBC News | UK | UK Edition</title>
      <link>http://news.bbc.co.uk/go/rss/-/1/hi/uk/default.stm</link>

      <item>
      <title>Clegg bids to cool coalition talk</title>
      <description>Ni ck Clegg is to say he is "not the kingmaker" amid speculation about the Lib Dems' possible role in a coalition government.
      </description>
      <link>http://news.bbc.co.uk/go/rss/-/1/hi/uk_politics/8566448.stm</link>
      </item>


      An I expect my output to be:

      BBC News | UK | UK Edition

      Get all the latest news, live updates and content about the UK from across the BBC.


      Clegg bids to cool coalition talk

      Nick Clegg is to say he is "not the kingmaker" amid speculation about the Lib Dems' possible role in a coalition government.

      BBC, News, BBC News, news online, world, uk, international, foreign, british, online, service


      ---------------------------
      I guess HTML form would be more flexible than an asp:Hyperlink object but it doesn't really matter, I just want to turn the titles within the file into hyperlinks which the user can use to go directly to the report

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Ah, I see, the problem is that you're overwriting it. eg, you'd get

        Text = "BBC News | UK | UK Edition</title>" with <title> removed, then
        Text = "<title>BBC News | UK | UK Edition" with </title> removed.

        Code:
        string line = sr.ReadLine(); 
        int i = line.IndexOf("<title>");
        int j = line.IndexOf("</title>");
        
        // If you have a multi-line title, this won't deal with it.
        if(i != -1 && j != -1)
        {
          HyperLink4.Text = line.Substring(i, j-i);
        }
        and do the same thing with link.

        You are assuming each element only comes on one line each, correct?

        Comment

        • mbewers1
          New Member
          • Feb 2009
          • 68

          #5
          It's OK, I've solved this by using a better a approach - using Repeaters, Literals and Hyperlinks.

          For the benefit of newbies to rss, just like me, this is the site I used:

          Comment

          Working...