关于xml 多出xmlns=""
XML文件:<histNews xmlns="http://www.163.com/rss/0.9">
<news>
<title>什么什么什么 </title>
<time>2008-12-11 </time>
</news>
<histNews>
读取方式:
string url = Server.MapPath("/sitemap/Sitemaps.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(url);
string news_title="12311";
string news_time="2008-12-05";
XmlNode root = xmlDoc.DocumentElement;
XmlElement news = xmlDoc.CreateElement("news");
XmlElement title = xmlDoc.CreateElement("title");
title.InnerText = news_title;
news.AppendChild(title);
XmlElement time = xmlDoc.CreateElement("time");
time.InnerText = news_time ;
news.AppendChild(time);
root.AppendChild(news);
xmlDoc.Save(url);
结果:
<news xmlns="">
<title>什么什么</title>
<time>2008-12-14</time>
</news>
要求:红色部份去掉!
--------------------编程问答-------------------- XmlNode root = xmlDoc.SelectSingleNode("news");
参考
http://www.cnblogs.com/ziyan22/archive/2007/08/23/866456.html
--------------------编程问答-------------------- 搂主上贴中我就猜到搂主是想创建XML,而不是读取,所以说根本不用调用load了
搂主完全改成下面的,就行了:
string url = Server.MapPath("/sitemap/Sitemaps.xml");
XmlDocument xmlDoc = new XmlDocument();
string news_title = "12311";
string news_time = "2008-12-05";
XmlNode root = xmlDoc.CreateElement("histNews");
XmlElement news = xmlDoc.CreateElement("news");
XmlElement title = xmlDoc.CreateElement("title");
title.InnerText = news_title;
news.AppendChild(title);
XmlElement time = xmlDoc.CreateElement("time");
time.InnerText = news_time;
news.AppendChild(time);
root.AppendChild(news);
xmlDoc.AppendChild(root);
xmlDoc.Save(url); --------------------编程问答-------------------- 噢,没标出我改动的地方,标示一下吧:
string url = Server.MapPath("/sitemap/Sitemaps.xml");
XmlDocument xmlDoc = new XmlDocument();
//xmlDoc.Load(url);
string news_title = "12311";
string news_time = "2008-12-05";
XmlNode root = xmlDoc.CreateElement("histNews");
XmlElement news = xmlDoc.CreateElement("news");
XmlElement title = xmlDoc.CreateElement("title");
title.InnerText = news_title;
news.AppendChild(title);
XmlElement time = xmlDoc.CreateElement("time");
time.InnerText = news_time;
news.AppendChild(time);
root.AppendChild(news);
xmlDoc.AppendChild(root);
xmlDoc.Save(url);
注意:搂主还需确信sitemap文件夹是存在的,否则得先创建好 --------------------编程问答-------------------- 另外,上面代码还删掉了一句:XmlNode root = xmlDoc.DocumentElement;
刚才测试了一下上面代码是对的 --------------------编程问答--------------------
XML文件:结尾的时候要关闭</histNews> --------------------编程问答-------------------- 楼主还在问这个问题啊,注意加一个参数。还有你的<histNews>... </histNews> 结束标记
<histNews xmlns="http://www.163.com/rss/0.9">
<news>
<title>什么什么什么 </title>
<time>2008-12-11 </time>
</news>
<histNews>
xmlDoc.CreateElement("news", "http://www.163.com/rss/0.9");
--------------------编程问答-------------------- 纠正一下,呵呵,搂主帖子已经说明了:要去掉XML名称空间的声明
string url = Server.MapPath("/sitemap/Sitemaps.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(url);
string news_title = "12311";
string news_time = "2008-12-05";
XmlNode root = xmlDoc.DocumentElement;
XmlElement news = xmlDoc.CreateElement("news", "http://www.163.com/rss/0.9");
XmlElement title = xmlDoc.CreateElement("title", "http://www.163.com/rss/0.9");
title.InnerText = news_title;
news.AppendChild(title);
XmlElement time = xmlDoc.CreateElement("time", "http://www.163.com/rss/0.9");
time.InnerText = news_time;
news.AppendChild(time);
root.AppendChild(news);
xmlDoc.Save(url);
搂主是要创建这个XML,所以不需要这个XML文件事先已经存在的,直接使用3楼代码就可以啦
补充:.NET技术 , C#