今天用c#程序生成百度站點地圖文件sitemap.xml的時候,創(chuàng)建url節(jié)點多了莫名的多了xmlns屬性。原來是上級節(jié)點有這個屬性,而子節(jié)點也要指定,不然就會多這個。問題原因:
當父節(jié)點具有XMLNS屬性時,子節(jié)點必須指定XMLNS屬性,但是當子節(jié)點的XMLNS屬性與父節(jié)點命名空間相同時,子節(jié)點不顯示XMLNS屬性,上面問題的根本所在就是我們理解上的錯誤,認為我沒有為子節(jié)點指定命名空間,就不應該出現(xiàn)此屬性,恰恰相反,當我們?yōu)槠渲付ㄅc父節(jié)點相同的命名空間時,此屬性才不會出現(xiàn)。
原始xml文件內容
添加節(jié)點的c#代碼
- <?xml version="1.0" encoding="UTF-8"?>
- <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">
- <url>
- <loc>http://m.example.com/index.html</loc>
- <mobile:mobile type="mobile" />
- <lastmod>2009-12-14</lastmod>
- <changefreq>daily</changefreq>
- <priority>0.8</priority>
- </url>
- </urlset>
新加的節(jié)點
- public static bool AddUrl2(SitemapUrl model)
- {
- XmlDocument doc = new XmlDocument();
- doc.Load(@"c:\test.xml");
- var rootNode = doc.GetElementsByTagName("urlset");
- if (rootNode.Count == 1)
- {
- var loc = doc.CreateElement("loc");
- loc.InnerText = model.loc;
- var lastmod = doc.CreateElement("lastmod");
- lastmod.InnerText = model.lastmod;
- var changefreq = doc.CreateElement("changefreq");
- changefreq.InnerText = model.changefreq;
- var priority = doc.CreateElement("priority");
- priority.InnerText = model.priority;
- var url = doc.CreateElement("url");
- url.AppendChild(loc);
- url.AppendChild(lastmod);
- url.AppendChild(changefreq);
- url.AppendChild(priority);
- var root = rootNode[0];
- root.AppendChild(url);
- doc.Save(@"c:\test.xml");
- return true;
- }
- else
- return false;
- }
新節(jié)點多了一個xmlns屬性,這個屬性是表示命名空間的意思
- <url xmlns="">
- <loc>http://www.baidu.com/</loc>
- <lastmod>2014-12-02</lastmod>
- <changefreq>daily</changefreq>
- <priority>1</priority>
- </url>
原來是url的父節(jié)點urlset有這個屬性,在用c#插入xml節(jié)點的時候沒有傳入這個參數(shù),所以要修復這個問題,不想要這個屬性就優(yōu)傳入就可以了。調用CreateElement一個重載方法
doc.CreateElement("url", doc.DocumentElement.NamespaceURI);
完整代碼:注意:url的子節(jié)點(如:loc,lastmod)創(chuàng)建的也得傳入doc.DocumentElement.NamespaceURI這個參數(shù),不然也會多了一個xmlns=""。當然從開始就沒有指定xmlns,就不用這們傳入doc.DocumentElement.NamespaceURI了。
- public static bool AddUrl(SitemapUrl model)
- {
- XmlDocument doc = new XmlDocument();
- doc.Load(@"c:\test.xml");
- var rootNode = doc.GetElementsByTagName("urlset", doc.DocumentElement.NamespaceURI);
- if (rootNode.Count == 1)
- {
- var loc = doc.CreateElement("loc", doc.DocumentElement.NamespaceURI);
- loc.InnerText = model.loc;
- var lastmod = doc.CreateElement("lastmod", doc.DocumentElement.NamespaceURI);
- lastmod.InnerText = model.lastmod;
- var changefreq = doc.CreateElement("changefreq", doc.DocumentElement.NamespaceURI);
- changefreq.InnerText = model.changefreq;
- var priority = doc.CreateElement("priority", doc.DocumentElement.NamespaceURI);
- priority.InnerText = model.priority;
- var url = doc.CreateElement("url", doc.DocumentElement.NamespaceURI);
- url.AppendChild(loc);
- url.AppendChild(lastmod);
- url.AppendChild(changefreq);
- url.AppendChild(priority);
- var root = rootNode[0];
- root.AppendChild(url);
- doc.Save(@"c:\test.xml");
- return true;
- }
- else
- return false;
- }