linq to xml 操作sitemap

刚开始用xml去做没有弄好,折腾了好久成功了,先仓促的记一下

protected void btnAdd_Click(object sender, EventArgs e)
    {
        XElement els = XElement.Load(Server.MapPath(@"~\Student\stu.sitemap"));
        XNamespace siteNM = "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0";
        XElement eTemp = els.Element(siteNM + "siteMapNode");
        var elements = from el in eTemp.Elements(siteNM + "siteMapNode")
                       where (string)el.Attribute("title") == "课堂学习"
                       select el;
        XElement keTang = null;
        foreach (var el in elements)
        {
            keTang = el;
        }
        keTang.Add(new XElement(siteNM + "siteMapNode", new XAttribute("url", txtUrl.Text), new XAttribute("title", txtName.Text), new XAttribute("description", ""), new XAttribute("roles", "0")));
        els.Save(Server.MapPath(@"~\Student\stu.sitemap"));
    }

 

XElement els = XElement.Load(Server.MapPath(@"~\Student\stu.sitemap"));
        XNamespace siteNM = "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0";
        XElement eTemp = els.Element(siteNM + "siteMapNode");
        var elements = from el in eTemp.Elements(siteNM + "siteMapNode")
                       where (string)el.Attribute("title") == "课堂学习"
                       select el;
        XElement keTang = null;
        foreach (var el in elements)
        {
            keTang = el;
        }
        var del = from el in keTang.Elements(siteNM + "siteMapNode")
                  where (string)el.Attribute("title") == txtName.Text
                  select el;
        XElement delXElement = null;
        foreach (var ell in del)
            delXElement = ell;

        if (delXElement != null)
            delXElement.Remove();

        els.Save(Server.MapPath(@"~\Student\stu.sitemap"));

你可能感兴趣的:(LINQ)