I have a problem about generating sitemap.xml
My creator code is like this:
XNamespace xmlns = XNamespace.Get("http://www.sitemaps.org/schemas/sitemap/0.9");
XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
XNamespace schemaLocation = XNamespace.Get("http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd");
XElement urlset = new XElement(xmlns+"urlset",
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XAttribute(xsi + "schemaLocation", schemaLocation));
urlset.Add(new XElement("url"));
This code generates the xml file but the generated sitemap.xml
url elements include an xmlns=""
attribute.
<urlset xmlns="..." ><url xmlns=""/> </urlset>
All of the <url>
element include that xmlns = ""
attribute.
How can I solve this?
I think the W3 Org can summize namespaces better than I, you have qualified namespaces for all nodes and attributes above the 'url' node. When you add urlset.Add(new XElement("url"));
how should it determine the namespace it is under?
I would recommend trying this out and see how it works for you:
static void Main ( string [] args )
{
XNamespace xmlns = XNamespace.Get("http://www.sitemaps.org/schemas/sitemap/0.9");
XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
XNamespace schemaLocation = XNamespace.Get("http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd");
XElement urlset = new XElement(xmlns+"urlset",
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XAttribute(xsi + "schemaLocation", schemaLocation));
urlset.Add(new XElement(xmlns+"url")); // NB> We are qualifying the node
var s = urlset.ToString( );
Console.ReadKey( );
}
Try this code
XmlDocument stripDocumentNamespace(XmlDocument oldDom)
{
XmlDocument newDom = new XmlDocument();
newDom.LoadXml(Regex.Replace(oldDom.OuterXml,
@"(xmlns:?[^=]*=[""][^""]*[""])", "",
RegexOptions.IgnoreCase | RegexOptions.Multiline));
return newDom;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.