locked
XML Nodes development, any short of this method RRS feed

  • Question

  • User-240513752 posted
    class Program
        {
            static void Main(string[] args)
            {
                XmlDocument xmlDoc = new XmlDocument();
                XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
                XNamespace xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9";
                XElement root = new XElement(xmlns +"urlset");
    
    
              
    
                
    
                XmlNode rootNode = xmlDoc.CreateElement("url");
    
                xmlDoc.AppendChild(rootNode);
                XmlNode userNode = xmlDoc.CreateElement("loc");
                userNode.InnerText = "http://localdevweb.rainsoftfn.com";
                rootNode.AppendChild(userNode);
    
                XmlNode changefreq = xmlDoc.CreateElement("changefreq");
                changefreq.InnerText = "yearly";
                rootNode.AppendChild(changefreq);
    
                XmlNode priority = xmlDoc.CreateElement("priority");
                priority.InnerText = "0.4";
                rootNode.AppendChild(priority);
                 
    
                
                XmlNode OurpRocesssuserNode = xmlDoc.CreateElement("loc");
                OurpRocesssuserNode.InnerText = "http://localdevweb.rainsoftfn.com/OurProcess";
                rootNode.AppendChild(OurpRocesssuserNode);
    
                XmlNode OurProcesschangefreq = xmlDoc.CreateElement("changefreq");
                OurProcesschangefreq.InnerText = "yearly1";
                rootNode.AppendChild(OurProcesschangefreq);
    
                XmlNode OurProcesspriority = xmlDoc.CreateElement("priority");
                OurProcesspriority.InnerText = "0.41";
                rootNode.AppendChild(OurProcesspriority);
    
    
                XmlNode AboutUsNode = xmlDoc.CreateElement("loc");
                AboutUsNode.InnerText = "http://localdevweb.rainsoftfn.com/l/AboutUs";
                rootNode.AppendChild(AboutUsNode);
    
                XmlNode AboutUschangefreq = xmlDoc.CreateElement("changefreq");
                AboutUschangefreq.InnerText = "monthly";
                rootNode.AppendChild(AboutUschangefreq);
    
                XmlNode AboutUspriority = xmlDoc.CreateElement("priority");
                AboutUspriority.InnerText = "0.2";
                rootNode.AppendChild(AboutUspriority);
    
                XmlNode CareersNode = xmlDoc.CreateElement("loc");
                CareersNode.InnerText = "http://localdevweb.rainsoftfn.com/l/Careers";
                rootNode.AppendChild(CareersNode);
    
                XmlNode Careerschangefreq = xmlDoc.CreateElement("changefreq");
                Careerschangefreq.InnerText = "daily";
                rootNode.AppendChild(Careerschangefreq);
    
                XmlNode CAreerspriority = xmlDoc.CreateElement("priority");
                CAreerspriority.InnerText = "0.3";
                rootNode.AppendChild(CAreerspriority);
    
    
               
    
                xmlDoc.Save("test-doc.xml");
            }
    
            
        }

    I want to make the code to short as much . a parameter or parameters will be passed & they generate XML like the above XML.?

    <url>
      <loc>http://localdevweb.rainsoftfn.com</loc>
      <changefreq>yearly</changefreq>
      <priority>0.4</priority>
      <loc>http://localdevweb.rainsoftfn.com/OurProcess</loc>
      <changefreq>yearly1</changefreq>
      <priority>0.41</priority>
      <loc>http://localdevweb.rainsoftfn.com/l/AboutUs</loc>
      <changefreq>monthly</changefreq>
      <priority>0.2</priority>
      <loc>http://localdevweb.rainsoftfn.com/l/Careers</loc>
      <changefreq>daily</changefreq>
      <priority>0.3</priority>
    </url>

    Friday, October 21, 2016 1:34 PM

Answers

  • User-654786183 posted

    I have updated the fiddle

    Because the Xml is having namespace at the root level, you have to add the same namespace for all the child elements

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

All replies

  • User-654786183 posted

    Try something like this.  You can convert this to an XmlDocument at the end.

    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());

    .NET Fiddle for this code

    How to change XElement to XmlDocument

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(myXElement.ToString());

    Friday, October 21, 2016 2:21 PM
  • User-240513752 posted
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
      <url xmlns="">
        <loc>http://www.example.com/</loc>
        <lastmod>2012-04-30</lastmod>
        <changefreq>monthly</changefreq>
        <priority>1.0</priority>
      </url>
      <url xmlns="">
        <loc>http://www.example.com/about-us.html</loc>
        <changefreq>yearly</changefreq>
        <priority>0.4</priority>
      </url>
    </urlset>

    you generated ,,,how to remove xmlns="" from this <url xmlns=""> ?

    Friday, October 21, 2016 2:26 PM
  • User-654786183 posted

    I have updated the fiddle

    Because the Xml is having namespace at the root level, you have to add the same namespace for all the child elements

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