1. Here is demo, draw on others to generate XML
Create an XML file method protected void Btn1_onclick (object sender, EventArgs e) {XmlText XmlText; XmlDocument xmldoc = new XmlDocument (); Add the Declaration of the XML paragraph XmlNode XmlNode = xmldoc. Createxmldeclaration ("1.0", "gb2312", null); XmlDoc. AppendChild (XmlNode); Add a root element XmlElement Xmlelem = xmldoc. CreateElement ("", "bookstore", ""); XmlText = xmldoc. createTextNode (""); Xmlelem. AppendChild (xmltext); XmlDoc. AppendChild (Xmlelem); Add a child element XmlElement xmlelem1 = xmldoc. CreateElement ("", "book", ""); XmlText = xmldoc. createTextNode (""); Xmlelem1. AppendChild (xmltext); Adds two attribute xmlelem1 to the child element "book". SetAttribute ("Genre", "" "," Fantasy "); Xmlelem1. SetAttribute ("ISBN", "2-3631-4"); XmlDoc. Childnodes.item (1). AppendChild (XMLELEM1); Creates a child element of three child elements XmlElement xmlelem2 = xmldoc. CreateElement ("", "title", ""); XmlText = xmldoc. CreateTextNode ("Oberon ' s Legacy"); Xmlelem2. AppendChild (xmltext); XmlDoc. Childnodes.item (1). AppendChild (XMLELEM1). AppendChild (XMLELEM2); XmlElement xmlelem3 = xmldoc. CreateElement ("", "Author", ""); XmlText = xmldoc. createTextNode ("Corets, Eva"); Xmlelem3. AppendChild (xmltext); XmlDoc. Childnodes.item (1). AppendChild (XMLELEM1). AppendChild (XMLELEM3); XmlElement xmlelem4 = xmldoc. CreateElement ("", "Price", ""); XmlText = xmldoc. createTextNode ("5.95"); Xmlelem4. AppendChild (xmltext); XmlDoc. Childnodes.item (1). AppendChild (XMLELEM1). AppendChild (XMLELEM4); XmlDoc. Save (Server.MapPath ("Bookstore.xml")); Save}//Create XML file method two protected void Btn2_onclick (object sender, EventArgs e) {XmlDocument xmldoc = new XmlDocument (); Creates an empty XML document XMLDOC. LOADXML ("<?xml version= ' 1.0 ' encoding= ' gb2312 '?>" + "<bookstore>" + "<book genre= ' fantasy ' isbn= ' 2-3631-4 ' ≫ "+" <title>oberon ' s legacy</title> "+" <author>corets, eva</author> "+ "<price>5.95</price>" + "</book>" + "</bookstore>"); XmlDoc. Save (Server.MapPath ("Bookstore2.xml")); Save
If this is the case:
<?xml version= "1.0" encoding= "gb2312"?> <bookstore> <book genre= "Fantasy" isbn= "2-3631-4" > <title>oberon ' s legacy</title> <author>corets, eva</author> <price >5.95</price> </book> </bookstore>
Not the site map XML file I want.
2. The following is my own according to the actual situation written
Using system;using system.collections.generic;using system.io;using system.threading;using System.Xml;namespace helper{public class Sitemapxml {Private Const string Xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9"; Private Const string xmlnsxsi = "Http://www.w3.org/2001/XMLSchema-instance"; Private Const string xsischemalocation = "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/ Schemas/sitemap/0.9/sitemap.xsd "; <summary>//Generate sitemap Map///</summary>//<param name= "siteMaps" > List of objects to be generated & lt;/param>//<param name= "savefilename" > Settings file Save name </param>//<param name= "Changefreq" ; Update cycle </param>///<param name= "Savepath" >xml File Save path </param>//<returns></returns > public static bool Createsitemapxml (list<sitemap> siteMaps, string savepath = "/", String savefilename = "Sitemap", String changefreq = "WEekly ") {//Save the created XML document string filename = Savefilename +". xml "; String path = System.Web.HttpContext.Current.Server.MapPath (savepath) + filename; Create XML first, return path var xmldoc = new XmlDocument (); Join the declaration paragraph of XML, <?xml version= "1.0" encoding= "gb2312"?> xmldeclaration xmldecl = xmldoc. Createxmldeclaration ("1.0", "UTF-8", null); XmlDoc. AppendChild (XMLDECL); Add a root element XmlNode Xmlelem = xmldoc. CreateElement ("", "Urlset", ""); Add Property XmlAttribute attr = xmldoc. CreateAttribute ("xmlns"); attr. Value = Xmlns; if (Xmlelem. Attributes = null) Xmlelem. Attributes.setnameditem (attr); attr = xmldoc. CreateAttribute ("Xmlns:xsi"); attr. Value = Xmlnsxsi; if (Xmlelem. Attributes = null) Xmlelem. Attributes.setnameditem (attr); attr = xmldoc. CreateAttribute ("Xsi:schemalocation"); attr. Value = XsisChemalocation; if (Xmlelem. Attributes = null) Xmlelem. Attributes.setnameditem (attr); XmlDoc. AppendChild (Xmlelem); String lastmod = DateTime.Now.ToString ("Yyyy-mm-dd"); for (int i = 0; i < Sitemaps.count; i++) {XmlNode root = xmldoc. selectSingleNode ("Urlset");//Find <urlset> if (root = null) {//Join a root element Xmlelem = xmldoc. CreateElement ("", "Urlset", ""); Add Property attr = xmldoc. CreateAttribute ("xmlns"); attr. Value = Xmlns; if (Xmlelem. Attributes = null) Xmlelem. Attributes.setnameditem (attr); attr = xmldoc. CreateAttribute ("Xmlns:xsi"); attr. Value = Xmlnsxsi; if (Xmlelem. Attributes = null) Xmlelem. Attributes.setnameditem (attr); attr = xmldoc. CreateAttribute ("Xsi:schemalocation"); attr. Value = xsischemalocation; if (Xmlelem. Attributes = null) Xmlelem. Attributes.setnameditem (attr); XmlDoc. AppendChild (Xmlelem); i = 0; Continue } XmlElement xe1 = xmldoc. createelement ("url");//Create a <url> node XmlElement xmlelem1 = xmldoc. CreateElement ("", "Loc", ""); XmlText XmlText = xmldoc. createTextNode (Sitemaps[i]. LOC); Xmlelem1. AppendChild (xmltext); Xe1. AppendChild (XMLELEM1); Xmlelem1 = xmldoc. CreateElement ("", "priority", ""); XmlText = xmldoc. createTextNode (Sitemaps[i]. priority); Xmlelem1. AppendChild (xmltext); Xe1. AppendChild (XMLELEM1); Xmlelem1 = xmldoc. CreateElement ("", "Lastmod", ""); XmlText = xmldoc. createTextNode (LASTMOD); Xmlelem1. AppendChild (xmltext); Xe1. AppendChild (XMLELEM1); Xmlelem1 = xmldoc. CreateElement ("", "Changefreq", ""); XmlText = xmldoc. createTextNode (Changefreq); Xmlelem1. AppendChild (xmltext); Xe1. AppendChild (XMLELEM1); Root. AppendChild (XE1);//Add to <urlset> node} try {//and then save to source location XmlDoc. AppendChild (Xmlelem); XmlDoc. Save (path); return true; } catch (Exception) {return false; }}}//<summary>////<url>/<loc>http://news.chinahbnet.com/201 4/8/19/15352.html</loc>//<priority>0.5</priority>//<lastmod>2014-08-19</lastmod& Gt <changefreq>weekly</changefreq>//</url>///</summary> public class Sitema p {///<summary>///link address//such as: Http://news.chinahbnet.com//</summary> public string Loc {get; set;} <summary>///page weights///0.1-1///</summary> public string Priority {get; SE T }//<summary>//Build Date//2014-08-19///</summary> public string Lastmo d {get; set;} <summary>//renewal cycle//always often//hourly per hour//daily daily///Weekly Weekly///monthly monthly//yearly//Never never//</summary> public string Chan gefreq {get; set;} }}
The resulting result is:
<?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" schemalocation= "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd "> <url> <loc>http:// www.baidu.com</loc> <priority>1.00</priority> <lastmod>2014-08-19</lastmod > <changefreq>weekly</changefreq> </url></urlset>
Hope can help you!