I Want add some url to xml file on my website using C#.
I have already create a XML file on website Root.
The content of xml file 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" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>http://www.structure.com/Structure.aspx?id=1</loc>
</url>
</urlset>
Now I want add new <url> Node with <loc> node to xml file and I want xml content changes like
<?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" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>http://www.structure.com/Structure.aspx?id=1</loc>
</url>
<url>
<loc>http://www.structure.com/Structure.aspx?id=2</loc>
</url>
</urlset>
I try made a function that get url string from web form and it's triggers on asp:Button click
protected void Button1_Click(object sender, EventArgs e)
{
insertSiteMap("http://www.structure.com/Structure.aspx?id=2");
}
And the function is:
private void insertSiteMap(string pageurl)
{
//Load XML Schema
System.Xml.XmlDocument originalXml = new System.Xml.XmlDocument();
originalXml.Load(Server.MapPath("../sitemap.xml"));
XmlElement URL = originalXml.CreateElement("url");
XmlElement LOC = originalXml.CreateElement("loc");
XmlText LOCText = originalXml.CreateTextNode(pageurl);
LOC.AppendChild(LOCText);
URL.AppendChild(LOC);
XmlNode newUrl = originalXml.GetElementsByTagName("url")[0];
originalXml.DocumentElement.PrependChild(newUrl);
originalXml.Save(Server.MapPath("../sitemap.xml"));
}
I don't have any error and visual studio message me xml file has been modified but when I open the file there is no any changes on xml file :(.
am I did wrong any where?