添加&lt; div>标记为每个<li> - HtmlGenericControl

时间:2016-04-09 18:00:57

标签: c# asp.net webforms

我正在阅读RSS XML新闻,并将每个新闻添加到<li>

我需要每个<li>标记位于< div >标记内,如何通过HtmlGenericControl进行标记?

喜欢这个

          <div>
            <li><span>Ações contra dengue prosseguem em Araruama &ndash; <a href="#">Leia mais</a></span></li>
        </div>
        <div>
            <li><span>Laboratório em Campinas identifica 'impressão digital' do vírus da zika &ndash; <a href="#">Leia mais</a></span></li>
        </div>
        <div>
            <li><span>Agentes arrombam casas fechadas em Recife contra o Aedes aegypti &ndash; <a href="#">Leia mais</a></span></li>
        </div>
        <div>
            <li><span>Universidade de Sorocaba coordena três estudos sobre o Aedes aegypti &ndash; <a href="#">Leia mais</a></span></li>
        </div>

代码:

    private void AddMenuItem()
{

    XmlDocument rssXmlDoc = new XmlDocument();

    // Load the RSS file from the RSS URL
    rssXmlDoc.Load("http://g1.globo.com/dynamo/luta-contra-a-dengue/rss.xml");

    // Parse the Items in the RSS file
    XmlNodeList rssNodes = rssXmlDoc.SelectNodes("rss/channel/item");

    StringBuilder rssContent = new StringBuilder();

    // Iterate through the items in the RSS file
    foreach (XmlNode rssNode in rssNodes)
    {

        XmlNode rssSubNode = rssNode.SelectSingleNode("title");
        string title = rssSubNode != null ? rssSubNode.InnerText : "";

        rssSubNode = rssNode.SelectSingleNode("link");
        string link = rssSubNode != null ? rssSubNode.InnerText : "";



        HtmlGenericControl li = new HtmlGenericControl("li");
        noticias.Controls.Add(li);

        HtmlGenericControl span = new HtmlGenericControl("span");
        span.InnerText = title;           

        HtmlGenericControl anchor = new HtmlGenericControl("a");
        anchor.Attributes.Add("href", link);
        anchor.InnerText = "Leia Mais";

        li.Controls.Add(span);
        li.Controls.Add(anchor);

1 个答案:

答案 0 :(得分:0)

您需要在li中插入div元素,并将div添加到noticias容器中:

foreach (XmlNode rssNode in rssNodes)
{
    ...

    HtmlGenericControl div = new HtmlGenericControl("div");
    noticias.Controls.Add(div);

    HtmlGenericControl li = new HtmlGenericControl("li");
    div.Controls.Add(li);

    HtmlGenericControl span = new HtmlGenericControl("span");
    span.InnerText = title;

    ...
}