小弟是新手请教以下问题。
txtSitemap.Text = "<url>
<loc>http://www.csdn.net</loc>
<lastmod>2008-09-13T16:02:40+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>";
XmlDocument xmldoc;
XmlNode xmlnode;
XmlElement xmlelem;
xmldoc = new XmlDocument();
//加入XML的声明段落
xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration,"","");
xmldoc.AppendChild(xmlnode);
//加入一个根元素
xmlelem = xmldoc.CreateElement("urlset");
xmlelem.SetAttribute("xmlns", "http://www.google.com/schemas/sitemap/0.84");
xmlelem.InnerXml = txtSitemap.Text;
xmldoc.AppendChild(xmlelem);
xmldoc.Save(@""+strfile+"");
以上代码运行保存下来的XML文件中结果如下:
<?xml version="1.0"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
<url xmlns="http://www.google.com/schemas/sitemap/0.84">
<loc>http://www.ibay24.de</loc>
<lastmod>2008-09-18T16:02:40+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
</urlset>为什么<url>节点里会多了xmlns="http://www.google.com/schemas/sitemap/0.84"元素?怎么才能去掉它?
还有怎么在<?xml version="1.0"?>加入encoding="UTF-8" 元素?
我刚学XML。遇到点问题。在网上找了半天没找到答案。哪位高手帮帮忙,先谢了
txtSitemap.Text = "<url>
<loc>http://www.csdn.net</loc>
<lastmod>2008-09-13T16:02:40+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>";
XmlDocument xmldoc;
XmlNode xmlnode;
XmlElement xmlelem;
xmldoc = new XmlDocument();
//加入XML的声明段落
xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration,"","");
xmldoc.AppendChild(xmlnode);
//加入一个根元素
xmlelem = xmldoc.CreateElement("urlset");
xmlelem.SetAttribute("xmlns", "http://www.google.com/schemas/sitemap/0.84");
xmlelem.InnerXml = txtSitemap.Text;
xmldoc.AppendChild(xmlelem);
xmldoc.Save(@""+strfile+"");
以上代码运行保存下来的XML文件中结果如下:
<?xml version="1.0"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
<url xmlns="http://www.google.com/schemas/sitemap/0.84">
<loc>http://www.ibay24.de</loc>
<lastmod>2008-09-18T16:02:40+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
</urlset>为什么<url>节点里会多了xmlns="http://www.google.com/schemas/sitemap/0.84"元素?怎么才能去掉它?
还有怎么在<?xml version="1.0"?>加入encoding="UTF-8" 元素?
我刚学XML。遇到点问题。在网上找了半天没找到答案。哪位高手帮帮忙,先谢了
string s = @" <url>
<loc>http://www.csdn.net</loc>
<lastmod>2008-09-13T16:02:40+00:00 </lastmod>
<changefreq>daily </changefreq>
<priority>1.0 </priority>
</url>";
XmlDocument xmldoc = new XmlDocument();
XmlDeclaration xd = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmldoc.AppendChild(xd);
XmlElement xmlelem = xmldoc.CreateElement("urlset");
xmlelem.InnerXml = s;//先赋值,再设置Attribute
xmlelem.SetAttribute("xmlns", "http://www.google.com/schemas/sitemap/0.84");
xmldoc.AppendChild(xmlelem);
xmldoc.Save("e:\\ttt.xml");