locked
how to make this XML RRS feed

  • Question

  • User-240513752 posted
    <?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
      <url>
        <loc>http://www.example.com/</loc>
        <lastmod>2012-04-30</lastmod>
        <changefreq>monthly</changefreq>
        <priority>1.0</priority>
      </url>
      <url>
        <loc>http://www.example.com/about-us.html</loc>
        <changefreq>yearly</changefreq>
        <priority>0.4</priority>
      </url>
    </urlset>

    How to make this XML?

    Friday, October 21, 2016 1:55 PM

Answers

  • User-654786183 posted

    Try this

    XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
    		XElement myXml = new XElement(ns+"urlset", 
    									  new XElement("url",
    												   new XElement("loc", "http://www.example.com/"),
    												   new XElement("lastmod", "2012-04-30"),
    												   new XElement("changefreq", "monthly"),
    												   new XElement("priority", "1.0")),
    									  new XElement("url", 
    												   new XElement("loc", "http://www.example.com/about-us.html"),
    												   new XElement("changefreq", "yearly"),
    												   new XElement("priority", "0.4")));
    		Console.WriteLine(myXml.ToString());

    I have created a fiddle for the same

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 21, 2016 2:17 PM

All replies

  • User-654786183 posted

    Try this

    XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
    		XElement myXml = new XElement(ns+"urlset", 
    									  new XElement("url",
    												   new XElement("loc", "http://www.example.com/"),
    												   new XElement("lastmod", "2012-04-30"),
    												   new XElement("changefreq", "monthly"),
    												   new XElement("priority", "1.0")),
    									  new XElement("url", 
    												   new XElement("loc", "http://www.example.com/about-us.html"),
    												   new XElement("changefreq", "yearly"),
    												   new XElement("priority", "0.4")));
    		Console.WriteLine(myXml.ToString());

    I have created a fiddle for the same

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 21, 2016 2:17 PM
  • User-707554951 posted

    Hi khan_1,

    Senthilwaits provide a good solution for your problem. Here is an another way for you:

    protected void Page_Load(object sender, EventArgs e)
            {
                XmlDocument document = new XmlDocument();
                document.AppendChild(document.CreateXmlDeclaration("1.0", "UTF-8", ""));
                XmlElement root = document.CreateElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
                document.AppendChild(root);
                XmlNode url = document.CreateElement("url", "http://www.sitemaps.org/schemas/sitemap/0.9");
                CreateNode(document, url, "loc", "http://www.example.com/");
                CreateNode(document, url, "lastmod", "2012-04-30");
                CreateNode(document, url, "changefreq", "monthly");
                CreateNode(document, url, "priority", "1.0");
                root.AppendChild(url);
                XmlNode url2 = document.CreateElement("url", "http://www.sitemaps.org/schemas/sitemap/0.9");
                CreateNode(document, url2, "loc", "http://www.example.com/about-us.html");
                CreateNode(document, url2, "changefreq", "yearly");
                CreateNode(document, url2, "priority", "0.4");
                root.AppendChild(url2);
                document.Save(@"C:\Users\v-guzou\Desktop\Test Document\text2.xml");
    
            }
            public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
            {
                XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, "http://www.sitemaps.org/schemas/sitemap/0.9");
                node.InnerText = value;
                parentNode.AppendChild(node);
            }

    The screenshot as below:



    Best regards

    Cathy

    Sunday, October 23, 2016 5:41 AM