Answered by:
XML Nodes development, any short of this method

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