sitemap.xml的代码如下
<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>http://www.abc.com/</loc>
<priority>0.5</priority>
<lastmod>2008-04-3T08:45:45+08:00</lastmod>
<changefreq>always</changefreq>
</url>
<url>
<loc>http://www.abc.com/aaa</loc>
<priority>1.0</priority>
<lastmod>2008-04-02T09:22+08:00</lastmod>
<changefreq>always</changefreq>
</url>
<url>
<loc>http://www.abc.com/bbb</loc>
<priority>1.0</priority>
<lastmod>2008-04-02T09:24+08:00</lastmod>
<changefreq>always</changefreq>
</url>
</urlset>我想在这个XML上自动添加节点。平时网上添加XML节点的那些代码都用不上,出错了。请各位大侠能给出能读写这段的代码!
<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>http://www.abc.com/</loc>
<priority>0.5</priority>
<lastmod>2008-04-3T08:45:45+08:00</lastmod>
<changefreq>always</changefreq>
</url>
<url>
<loc>http://www.abc.com/aaa</loc>
<priority>1.0</priority>
<lastmod>2008-04-02T09:22+08:00</lastmod>
<changefreq>always</changefreq>
</url>
<url>
<loc>http://www.abc.com/bbb</loc>
<priority>1.0</priority>
<lastmod>2008-04-02T09:24+08:00</lastmod>
<changefreq>always</changefreq>
</url>
</urlset>我想在这个XML上自动添加节点。平时网上添加XML节点的那些代码都用不上,出错了。请各位大侠能给出能读写这段的代码!
顶下楼主
先引用system.xml
再引用system.xml.xpath具体的写法,你google搜一下.
等等我去把我以前的源码找一下给你看
/// 增加一级目录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
string sitemap = Server.MapPath("../Web.sitemap"); XmlDataDocument doc = new XmlDataDocument();
doc.Load(sitemap);
XmlNode root = doc.GetElementsByTagName("siteMapNode").Item(0); SiteMapSection df = new SiteMapSection();
df.LockItem = false; XmlElement el = doc.CreateElement("siteMapNode");
el.SetAttribute("title", TextBox1.Text);
el.SetAttribute("roles", "");
el.SetAttribute("xmlns", "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0");
root.AppendChild(el);
el.InnerText = "";
doc.Save(sitemap);}恩 我做的是只有两级的目录树,这个是向sitemap中添加一级的
doc.Load(sitemap); SiteMapSection df = new SiteMapSection();
df.LockItem = false;
XmlNodeList nodeList = doc.GetElementsByTagName("siteMapNode"); XmlNode xn = doc.SelectSingleNode("//*[local-name()='siteMapNode' and @title='" + DropDownList1.SelectedItem.Text + "']");
XmlElement er = doc.CreateElement("siteMapNode");
er.SetAttribute("title", TextBox2.Text);
er.SetAttribute("url", TextBox3.Text);
er.SetAttribute("roles", "");
er.SetAttribute("xmlns", "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0");
xn.AppendChild(er); doc.Save(sitemap);
这个是二级的哦,你用的是1.1啊
我读写的是web.sitemap文件