//Lets say you want to have output as below. Defile the XMl template with Place holder, here indicated with ##
string myXmlTemplate = "<URL>" +
"<loc>##phLoc##</loc>" +
"<lastmod>##phLastMod##</lastmod>" +
"<changefreq>##phchangefreq##</changefreq>" +
"<priority>##phPriority##</priority>" +
"</URL>";
//Now retrive the tokens that you would like to replce with these place holders
string locToken = "http://www.xxx.com/blogview.aspx?blogno=13046";
string lastmod = "now()";
string changefreq = "weekly";
string priority = "0.8";
//I have written this code repeated to make it reader friednly.
myXmlTemplate = myXmlTemplate.Replace("##phLoc##", locToken);
myXmlTemplate = myXmlTemplate.Replace("##phLastMod##", lastmod);
myXmlTemplate = myXmlTemplate.Replace("##phchangefreq##", changefreq);
myXmlTemplate = myXmlTemplate.Replace("##phPriority##", priority);
//Now if you see the string myXmlTemplate, you will get your desired out put.
//all you have to do is to covert in in to XML document using any xml parser
XDocument xDoc = XDocument.Parse(myXmlTemplate);
xDoc.Save("sitemap.xml");
Please mark this post as Answer if it is of help to you!
I would love to change the world, but they wont give me the source code.
As you can see there are no xmlns="" attributes in the inner tags. I can't use replace functions for the whole file as there are 12000 records. I just want it to insert a clean record.
As you can see there are no xmlns="" attributes in the inner tags. I can't use replace functions for the whole file as there are 12000 records. I just want it to insert a clean record.
Creating XML nodes are not faster then replacing a flat string... I process 50k records by string replace only. All ou have to do is to write a loop of the MyXmlTemplate varibale.....if you are able to identify the details that should be looped throug...
Anyways, if we go with the approach accepted by you..i think tou have to do is
Dim xmlEl As XmlElement = XMLd.CreateElement("URL", "http://www.sitemaps.org/schemas/sitemap/0.")
You have to tell the xml node creator that what is the parents default name space here...try this..
Please mark this post as Answer if it is of help to you!
I would love to change the world, but they wont give me the source code.
I am not sure I have understood this but it didn't like that method
Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 59:
Line 60: xmlEl.InnerXml = "<loc></loc><lastmod></lastmod><changefreq></changefreq><priority></priority>"
Line 61: xmlEl.Item("loc", "http://www.sitemaps.org/schemas/sitemap/0").InnerText = "http://www.eureferendum.com/blogview.aspx?blogno=" + System.Convert.ToString(newblog) Line 62: xmlEl.Item("lastmod", "http://www.sitemaps.org/schemas/sitemap/0").InnerText = "now()"
Line 63: xmlEl.Item("changefreq", "http://www.sitemaps.org/schemas/sitemap/0").InnerText = "weekly"
My point is that the namespace of a node is determined when you create it,
not when you insert it.
So when you create your new nodes you need to make sure you create
them in the proper namespace. If you already have a node and that is in
no namespace and you insert it somewhere into a node that is in a namespace then
the serializer has to add the xmlns="" to ensure the node remains in no namespace.
That is how the DOM works, there is no way to change that. So as already suggested, make sure you create nodes in the namespace you want them in.
For this same reason I prefer using XML string and then simply parse it desired system.xml api object
acidtrash
Member
124 Points
158 Posts
Unwanted xmlns attributes on sitemap
Apr 03, 2012 12:36 PM|LINK
I am using the following cide whihc as you can see gives me xmlns attributes that I dont want.
Dim newblog As Integer = Convert.ToInt32(e.Command.Parameters("@newblog").Value) Dim XMLd As New XmlDocument XMLd.Load("\\bristol-sql\wwwroot\sitemap.xml") Dim ns As String = XMLd.DocumentElement.NamespaceURI Dim xmlEl As XmlElement = XMLd.CreateElement("URL", ns) xmlEl.InnerXml = "<loc></loc><lastmod></lastmod><changefreq></changefreq><priority></priority>" xmlEl.Item("loc").InnerText = "http://www.xxx.com/blogview.aspx?blogno=" + System.Convert.ToString(newblog) xmlEl.Item("lastmod").InnerText = "now()" xmlEl.Item("changefreq").InnerText = "weekly" xmlEl.Item("priority").InnerText = "0.8" XMLd.DocumentElement.AppendChild(xmlEl) XMLd.Save(Server.MapPath("sitemap.xml")) Response.Redirect("blogview.aspx?blogno=" + System.Convert.ToString(newblog))It outputs this...
<URL>
<loc xmlns="">http://www.xxx.com/blogview.aspx?blogno=13046</loc>
<lastmod xmlns="">now()</lastmod>
<changefreq xmlns="">weekly</changefreq>
<priority xmlns="">0.8</priority>
</URL>
kuber.manral
Contributor
3051 Points
714 Posts
Re: Unwanted xmlns attributes on sitemap
Apr 04, 2012 05:13 AM|LINK
Hi acidtrash,
I have check your code and it is working fine for me. I have used raw XML for this purpose as an input something like :
Visit My Blog
kavita_khand...
Star
9767 Points
1931 Posts
Re: Unwanted xmlns attributes on sitemap
Apr 04, 2012 05:28 AM|LINK
//Lets say you want to have output as below. Defile the XMl template with Place holder, here indicated with ## string myXmlTemplate = "<URL>" + "<loc>##phLoc##</loc>" + "<lastmod>##phLastMod##</lastmod>" + "<changefreq>##phchangefreq##</changefreq>" + "<priority>##phPriority##</priority>" + "</URL>"; //Now retrive the tokens that you would like to replce with these place holders string locToken = "http://www.xxx.com/blogview.aspx?blogno=13046"; string lastmod = "now()"; string changefreq = "weekly"; string priority = "0.8"; //I have written this code repeated to make it reader friednly. myXmlTemplate = myXmlTemplate.Replace("##phLoc##", locToken); myXmlTemplate = myXmlTemplate.Replace("##phLastMod##", lastmod); myXmlTemplate = myXmlTemplate.Replace("##phchangefreq##", changefreq); myXmlTemplate = myXmlTemplate.Replace("##phPriority##", priority); //Now if you see the string myXmlTemplate, you will get your desired out put. //all you have to do is to covert in in to XML document using any xml parser XDocument xDoc = XDocument.Parse(myXmlTemplate); xDoc.Save("sitemap.xml");I would love to change the world, but they wont give me the source code.
acidtrash
Member
124 Points
158 Posts
Re: Unwanted xmlns attributes on sitemap
Apr 04, 2012 12:47 PM|LINK
My XML is as thus...
<?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 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> <url> <loc>http://www.eureferendum.com/blogview.aspx?blogno=11</loc> <lastmod>2012-03-21</lastmod> <changefreq>weekly</changefreq> <priority>0.8</priority> </url> <url>As you can see there are no xmlns="" attributes in the inner tags. I can't use replace functions for the whole file as there are 12000 records. I just want it to insert a clean record.
kuber.manral
Contributor
3051 Points
714 Posts
Re: Unwanted xmlns attributes on sitemap
Apr 04, 2012 12:57 PM|LINK
Hi acidtrash,
You should pass an Empty string to namespace in this case then. set "ns" to Empty, Try Something like this :
Dim xmlEl As XmlElement = XMLd.CreateElement("URL", "")
Hope it helps...
Visit My Blog
acidtrash
Member
124 Points
158 Posts
Re: Unwanted xmlns attributes on sitemap
Apr 04, 2012 12:59 PM|LINK
Yah, tried that. IT just puts the empty xmlns tags in anyway. Grrr. :)
kavita_khand...
Star
9767 Points
1931 Posts
Re: Unwanted xmlns attributes on sitemap
Apr 04, 2012 01:22 PM|LINK
Creating XML nodes are not faster then replacing a flat string... I process 50k records by string replace only. All ou have to do is to write a loop of the MyXmlTemplate varibale.....if you are able to identify the details that should be looped throug...
Anyways, if we go with the approach accepted by you..i think tou have to do is
Dim xmlEl As XmlElement = XMLd.CreateElement("URL", "http://www.sitemaps.org/schemas/sitemap/0.")
You have to tell the xml node creator that what is the parents default name space here...try this..
I would love to change the world, but they wont give me the source code.
acidtrash
Member
124 Points
158 Posts
Re: Unwanted xmlns attributes on sitemap
Apr 04, 2012 01:28 PM|LINK
I am not sure I have understood this but it didn't like that method
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 59: Line 60: xmlEl.InnerXml = "<loc></loc><lastmod></lastmod><changefreq></changefreq><priority></priority>" Line 61: xmlEl.Item("loc", "http://www.sitemaps.org/schemas/sitemap/0").InnerText = "http://www.eureferendum.com/blogview.aspx?blogno=" + System.Convert.ToString(newblog) Line 62: xmlEl.Item("lastmod", "http://www.sitemaps.org/schemas/sitemap/0").InnerText = "now()" Line 63: xmlEl.Item("changefreq", "http://www.sitemaps.org/schemas/sitemap/0").InnerText = "weekly"Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Unwanted xmlns attributes on sitemap
Apr 05, 2012 01:58 AM|LINK
Hello acid:)
Just UseXmlDocuement and use XmlElement to deal with the problem,use the first default parameter function without any other parameters of namespaces……
XmlDocument doc = new XmlDocument(); XmlElement root = doc.CreateElement("Root"); XmlElement subnode = doc.CreateElement("SubItem1"); root.AppendChild(subnode); doc.AppendChild(root); doc.Save("c:\\try.xml");kavita_khand...
Star
9767 Points
1931 Posts
Re: Unwanted xmlns attributes on sitemap
Apr 05, 2012 07:34 AM|LINK
@acidtrash
My point is that the namespace of a node is determined when you create it,
not when you insert it.
So when you create your new nodes you need to make sure you create
them in the proper namespace. If you already have a node and that is in
no namespace and you insert it somewhere into a node that is in a namespace then
the serializer has to add the xmlns="" to ensure the node remains in no namespace.
That is how the DOM works, there is no way to change that. So as already suggested, make sure you create nodes in the namespace you want them in.
For this same reason I prefer using XML string and then simply parse it desired system.xml api object
Check in details how I have used
XNamespace xN = "http://www.sitemaps.org/schemas/sitemap/0.9";
Try this 100% working code :) XDocument xDoc = XDocument.Load(Server.MapPath("sitemap.xml")); XNamespace xN = "http://www.sitemaps.org/schemas/sitemap/0.9"; XElement xUrlNode = new XElement(xN + "url"); XElement xLocNode = new XElement(xN + "loc","my Location"); XElement xlastmod = new XElement(xN + "lastmod", "new()"); XElement xchangefreq = new XElement(xN + "changefreq", "weekly"); XElement xpriority = new XElement(xN + "priority", "0.8"); //Lets insert the nodes in reverse case xUrlNode.Add(xLocNode); //Add the child node in the URL node xUrlNode.Add(xlastmod); xUrlNode.Add(xchangefreq); xUrlNode.Add(xpriority); //Add the URL Node xDoc.Root.Add(xUrlNode); string seeOutPutHere = xDoc.ToString();xDoc.Save(Server.MapPath("sitemap.xml"));This will give you output as below.
<?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 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> <url> <loc>http://www.eureferendum.com/blogview.aspx?blogno=11</loc> <lastmod>2012-03-21</lastmod> <changefreq>weekly</changefreq> <priority>0.8</priority> </url> <url> <loc>my Location</loc> <lastmod>new()</lastmod> <changefreq>weekly</changefreq> <priority>0.8</priority> </url> </urlset>I would love to change the world, but they wont give me the source code.