private void WriteXML() { XmlTextWriter writer = new XmlTextWriter(Server.MapPath("/File/grade.xml"),null); writer.Formatting = Formatting.Indented; //首行缩进 writer.Indentation = 4; //缩进位置 XmlDocument domDoc = new XmlDocument(); XmlDeclaration nodeDeclar = domDoc.CreateXmlDeclaration("1.0", System.Text.Encoding.UTF8.BodyName, "yes"); domDoc.AppendChild(nodeDeclar); //如果rss有样式表文件的话,加上这两句 XmlProcessingInstruction nodeStylesheet = domDoc.CreateProcessingInstruction("xml-stylesheet", "type=/"text/css/" href=/"rss.css/""); domDoc.AppendChild(nodeStylesheet); XmlElement root = domDoc.CreateElement("PageList"); root.SetAttribute("Date", "20090307"); //添加属性结点 domDoc.AppendChild(root); XmlElement chnode = domDoc.CreateElement("Page"); chnode.SetAttribute("File", "index.swf"); //添加属性结点 chnode.SetAttribute("Music", ""); root.AppendChild(chnode); XmlElement element = domDoc.CreateElement("title"); XmlNode textNode = domDoc.CreateTextNode("搜狐焦点新闻"); //文本结点 element.AppendChild(textNode); chnode.AppendChild(element); element = domDoc.CreateElement("link"); textNode = domDoc.CreateTextNode("http://www.sohu.com"); element.AppendChild(textNode); chnode.AppendChild(element); element = domDoc.CreateElement("description"); //引用结点 XmlNode cDataNode = domDoc.CreateCDataSection("即时报道国内外时政大事,解读环球焦点事件"); element.AppendChild(cDataNode); chnode.AppendChild(element); //DataTable dt = GetDataTab(); //访问数据库,获取要在rss中显示的记录 //foreach (DataRow dr in dt.Rows) //{ // element = domDoc.CreateElement("item"); // //... // //创建内容结点,常见的如title,description,link,pubDate,创建方法同上 // //... // chnode.AppendChild(element); //} //输出 domDoc.WriteTo(writer); writer.Flush(); writer.Close(); }