1

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&#xD;&#xA;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&#xD;&#xA;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?

1 Answer 1

3

Once you have created the new element with your variable URL, you need to insert with e.g. originalXml.DocumentElement.AppendChild(URL);.

However, take note that in your original XML you use a namespace while your C# code creates the new XmlElements in no namespace, so you need to fix the element creation as well, e.g.

XmlElement url = originalXml.CreateElement("url", originalXml.DocumentElement.NamespaceURI);
XmlElement loc = originalXml.CreateElement("loc", originalXml.DocumentElement.NamespaceURI);
loc.InnerText = pageurl;
url.AppendChild(loc);
originalXml.DocumentElement.AppendChild(url);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.