locked
replacing text from node. RRS feed

  • Question

  • User-240513752 posted
     private static XmlDocument GenerateSiteMapXML(int businessUnitId)
            {
                IBusinessUnitService _businessService = new BusinessUnitService();
                ICmsPageService _cmsService = new CmsPageService();
                var BusinessUnitEntity = _businessService.GetByIdWithDetail(businessUnitId);
                var WebURL = BusinessUnitEntity.WebURL;
                var cmspages = _cmsService.GetCmsPagebyIncludeSiteAndBusinessUnitId(businessUnitId);
                //XElement xml = new XElement("");
                XmlDocument xmlDoc = new XmlDocument();
    
    
                if (cmspages.Any())
                {
    
                    XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
                    XmlElement rootNode = xmlDoc.CreateElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
                    xmlDoc.AppendChild(rootNode);
                    XmlNode UrlNode = xmlDoc.CreateElement("url");
                    HomePageDetail(BusinessUnitEntity, UrlNode, xmlDoc, rootNode);
    
                    foreach (var cmspage in cmspages)
                    {
                        rootNode.AppendChild(UrlNode);
                        XmlNode loc = xmlDoc.CreateElement("loc");
                        if (cmspage.IsSystemPage == true)
                            loc.InnerText = BusinessUnitEntity.WebURL + RainMaker.Common.Constants.SystemCmsPages.Where(x => x.SystemPageId == cmspage.SystemPageId.ToInt()).Select(z => z.Url).SingleOrDefault();
                        else
                            loc.InnerText = BusinessUnitEntity.WebURL + "/l/" + cmspage.SystemName;
                        UrlNode.AppendChild(loc);
    
                        XmlNode lastmod = xmlDoc.CreateElement("lastmod");
                        lastmod.InnerText = DateTime.UtcNow.ToString("yyyy-MM-ddThh:mm:ss");
                        UrlNode.AppendChild(lastmod);
    
                        if (cmspage.ChangeFreqId != null && cmspage.ChangeFreqId != 0)
                        {
                            XmlNode changefreq = xmlDoc.CreateElement("changefreq");
                            changefreq.InnerText = Enum.GetName(typeof(CmsChangeFrequency), cmspage.ChangeFreqId);
                            UrlNode.AppendChild(changefreq);
                        }
    
                        if (cmspage.Priority != null)
                        {
                            XmlNode priority = xmlDoc.CreateElement("priority");
                            priority.InnerText = cmspage.Priority.ToString();
                            UrlNode.AppendChild(priority);
                        }
    
                    }
    
    
    
                }
    
    
                return xmlDoc;
    
            }
    
            private static void HomePageDetail(BusinessUnit BusinessUnitEntity, XmlNode UrlNode, XmlDocument xmlDoc, XmlElement rootNode)
            {
                rootNode.AppendChild(UrlNode);
                XmlNode loc = xmlDoc.CreateElement("loc");
                loc.InnerText = BusinessUnitEntity.WebURL;
                UrlNode.AppendChild(loc);
    
                XmlNode lastmod = xmlDoc.CreateElement("lastmod");
                lastmod.InnerText = DateTime.UtcNow.ToString("yyyy-MM-ddThh:mm:ss");
                UrlNode.AppendChild(lastmod);
    
    
                XmlNode changefreq = xmlDoc.CreateElement("changefreq");
                changefreq.InnerText = "Hourly";
                UrlNode.AppendChild(changefreq);
    
    
                XmlNode priority = xmlDoc.CreateElement("priority");
                priority.InnerText = "0.4";
                UrlNode.AppendChild(priority);
            }
    

    output of this file is

    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
      <url xmlns="">
        <loc>http://localdevweb.rainsoftfn.com</loc>
        <lastmod>2016-10-28T11:31:50</lastmod>
        <changefreq>Hourly</changefreq>
        <priority>0.4</priority>
        <loc>http://localdevweb.rainsoftfn.com/OurProcess</loc>
        <lastmod>2016-10-28T11:31:50</lastmod>
        <changefreq>Daily</changefreq>
        <priority>0.50</priority>
        <loc>http://localdevweb.rainsoftfn.com/ContactUs</loc>
        <lastmod>2016-10-28T11:31:50</lastmod>
        <changefreq>Always</changefreq>
        <priority>0.00</priority>
        <loc>http://localdevweb.rainsoftfn.com/l/AboutUs</loc>
        <lastmod>2016-10-28T11:31:50</lastmod>
        <changefreq>Daily</changefreq>
        <priority>0.30</priority>
        <loc>http://localdevweb.rainsoftfn.com/l/Careers</loc>
        <lastmod>2016-10-28T11:31:50</lastmod>
        <changefreq>Always</changefreq>
        <priority>0.70</priority>
      </url>
    </urlset>

    How can i remove "xmlns=""" from this line in the output <url xmlns="">

    simple replace <url xmlns=""> to <url> 

    Friday, October 28, 2016 11:58 AM

All replies

  • User-707554951 posted

    Hi khan_1,

    khan_1

    How can i remove "xmlns=""" from this line in the output <url xmlns="">

    For your question above, I suggest you could add some namespace ("http://www.sitemaps.org/schemas/sitemap/0.9") when your create "url" element.

    Just do as below:

    XmlNode UrlNode = xmlDoc.CreateElement("url", "http://www.sitemaps.org/schemas/sitemap/0.9");

    The following is an example I made based on your needs, you could refer to it:

     protected void Page_Load(object sender, EventArgs e)
            {
                XmlDocument xmlDoc = new XmlDocument();
                XmlDeclaration xmldeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
                XmlElement rootNode = xmlDoc.CreateElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
                xmlDoc.AppendChild(rootNode);
              
                XmlNode UrlNode = xmlDoc.CreateElement("url", "http://www.sitemaps.org/schemas/sitemap/0.9");
                rootNode.AppendChild(UrlNode);
                CreateNode(xmlDoc, UrlNode, "loc", "http://localdevweb.rainsoftfn.com");
                CreateNode(xmlDoc, UrlNode, "lastmod", "2016 - 10 - 28T11: 31:50");
                xmlDoc.Save(@"C:\Users\v-guzou\Desktop\Test Document\text3.xml");   
            }
            protected void CreateNode(XmlDocument xmldoc, XmlNode parentNode, string name, string value)
            {
                XmlNode node = xmldoc.CreateElement(name, "http://www.sitemaps.org/schemas/sitemap/0.9");
                node.InnerText = value;
                parentNode.AppendChild(node);
            }

    The output screenshot as below:

    Best regards

    Cathy

    Sunday, October 30, 2016 4:50 AM