简体   繁体   English

防止创建空的xmlns属性

[英]prevent empty xmlns attribute being created

I am having a little issue. 我有一个小问题。 when i uise the following code to add to my xml file there is an empty xmlns="" beinmg added to it. 当我将以下代码添加到我的xml文件时,将添加一个空的xmlns =“” beinmg。 How do i stop that from happening? 我如何阻止这种情况发生?

XmlDocument doc = new XmlDocument();        
    doc.Load(HttpContext.Current.Server.MapPath(@"~/Sitemap.xml"));
    XmlElement root = doc.DocumentElement;
    XmlElement ele = doc.CreateElement("url");      
    ele.Attributes.RemoveNamedItem("xmlns");
    XmlElement locele = doc.CreateElement("loc");
    locele.InnerText = urlstring;
    XmlElement lastmodele = doc.CreateElement("lastmod");
    lastmodele.InnerText = DateTime.Now.ToString();
    XmlElement chgfrqele = doc.CreateElement("changefreq");
    chgfrqele.InnerText = "weekly";
    ele.AppendChild(locele);
    ele.AppendChild(lastmodele);
    ele.AppendChild(chgfrqele);
    root.AppendChild(ele);
    doc.Save(HttpContext.Current.Server.MapPath(@"~/Sitemap.xml"));

My outputted xml should look like this: 我输出的xml应该如下所示:

    <?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>www.url.com/test</loc>
    <lastmod>03/10/2018 10:01:43</lastmod>
    <changefreq>weekly</changefreq>
  </url> 
  <url>
    <loc>www.url.com/test</loc>
    <lastmod>05/10/2018 09:31:12</lastmod>
    <changefreq>weekly</changefreq>
  </url>


</urlset>

Unfortunately, it ends up looking like this: 不幸的是,它最终看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
     <url xmlns="">
    <loc>www.url.com/test</loc>
    <lastmod>05/10/2018 09:15:40</lastmod>
    <changefreq>weekly</changefreq>
  </url>
  <url xmlns="">
    <loc>www.url.com/test</loc>
    <lastmod>05/10/2018 09:21:40</lastmod>
    <changefreq>weekly</changefreq>
  </url>
</urlset>

How do I stop it adding the following to my URL element?: 如何停止将以下内容添加到我的URL元素?:

xmlns=""

Try this: 尝试这个:

private static void RemoveEmptyNamespace(XElement element)
{
    XAttribute attr = element.Attribute("xmlns");
    if (attr != null && string.IsNullOrEmpty(attr.Value))
        attr.Remove();
    foreach (XElement el in element.Elements())
        RemoveEmptyNamespace(el);
}

xmlns without prefix is known as default element . 没有前缀的xmlns被称为默认元素 Notice that descendant element without prefix inherit default namespace from ancestor implicitly. 注意,没有前缀的后代元素会隐式继承祖先的默认名称空间。 When you create element without specifying any namespace it will be created in empty namespace instead of in the default namespace, hence the xmlns="" . 当您创建元素而未指定任何名称空间时,它将在空名称空间而不是默认名称空间中创建,因此将创建xmlns="" So to avoid that you need to specify the namespace on creating new element, for example: 因此,为避免这种情况,您需要在创建新元素时指定名称空间,例如:

XmlElement locele = doc.CreateElement("loc", "http://www.sitemaps.org/schemas/sitemap/0.9");
locele.InnerText = urlstring;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM