首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建动态web.sitemap?

如何创建动态web.sitemap?
EN

Stack Overflow用户
提问于 2012-08-30 17:24:13
回答 1查看 3.1K关注 0票数 3

我想创建一个动态的Web.Sitemap,我的意思是该文件应该是可编辑的,并且我希望能够从我的代码中编辑它的节点。我已经在一个按钮的单击事件上尝试过了。

代码语言:javascript
复制
SiteMapNode smRoot = SiteMap.RootNode;
SiteMapNode smNode = new 
           SiteMapNode(smRoot.Provider, "Key", "~/Default.aspx", "Default");
smRoot.ChildNodes.Add(smNode);

但是在最后一行得到了错误的Collection is read-only.

我如何才能做到这一点?

EN

回答 1

Stack Overflow用户

发布于 2013-08-27 19:49:47

试试这个。

代码语言:javascript
复制
public void GetSiteMap(List<string> MyList)
{
    /// Add namespace:
    ///using System.Xml;
    ///using System.Data;
    ///using System.Xml.Serialization;

    String MyHome = "http://www.mysite.com" ;//Create Absolute url start.

    // create DataSet and DataTable if needed, or use List Array only
   /*
        DataTable DT = new DataTable();
        foreach (var array in MyList)
        {
            DT.Rows.Add(array);
        }

    */
    XmlDocument doc = new XmlDocument();// create XML Documents
    XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);// Create the head element
    doc.AppendChild(dec);

    XmlElement root = doc.CreateElement("urlset");// Create the root element with attributes

    root.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    root.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
    root.SetAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
    doc.AppendChild(root);

    //foreach (DataRow DR in DT.Rows)// if use DataTable or
    foreach (var array in MyList)// if use List only
    {
        XmlElement MyUrl = doc.CreateElement("url");//create child node for root node

        XmlElement Loc = doc.CreateElement("loc");//create child node for MyUrl node
        XmlElement Freq = doc.CreateElement("changefreq");//create child node for MyUrl node
        XmlElement Pri = doc.CreateElement("priority");//create child node for MyUrl node

        Loc.InnerText = MyHome + "/" + array;//set value for 1. child node  Loc node
        Freq.InnerText = "daily";//set value for 1. child node-Freq node
        Pri.InnerText = "0.85";//set value for 1. child node-Pri node

        MyUrl.AppendChild(Loc); //add child Loc node to MyUrl node
        MyUrl.AppendChild(Freq);//add child Freq node to MyUrl node
        MyUrl.AppendChild(Pri);//add child Pri node to MyUrl node


        root.AppendChild(MyUrl);//add child MyUrl node to root node
    }


    Response.Clear();
    XmlSerializer xs = new XmlSerializer(typeof(XmlDocument));
    Response.ContentType = "text/xml";
    xs.Serialize(Response.Output, doc);
    Response.End();
}

在PageLoad()上调用这个空函数

:)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12193736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档