添加新节点

时间:2017-05-01 16:51:11

标签: asp.net-mvc

我的xml文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <sitemap>
    <loc>abcdefg </loc>
    <lastmod>2017-12-10</lastmod>
  </sitemap>
</sitemapindex>

运行我的程序以添加新节点后,它看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <sitemap>
    <loc>laksdjfs </loc>
    <lastmod> 453w43e5</lastmod>
  </sitemap>
  <sitemap xmlns="">
    <loc>abcdefghoh_20170501181500715.xml</loc>
    <lastmod>2017-05-01</lastmod>
  </sitemap>
</sitemapindex>

xmlns =“”添加到节点中 - 我不想在那里拥有它。我该如何删除它?

我的Asp.Net Core 2.0程序如下所示:

   public void AddIndexFileToSiteMap(string IndexFileName)
    {
        string filenavn = GetSiteMapDir();
        DateTime time = DateTime.Now;
        string lastmoddate = time.Year.ToString("0000") + "-" + time.Month.ToString("00") + "-" + time.Day.ToString("00");

        try
            { 
            FileStream fileStream = new FileStream(filenavn, FileMode.Open);

            XmlDocument doc = new XmlDocument();
            doc.Load(fileStream);

            // Create the child nodes. This code demonstrates various ways to add them.
            XmlNode newElem = doc.CreateNode(XmlNodeType.Element,"sitemap", "");

            newElem.InnerXml = "<loc></loc><lastmod></lastmod>";
            newElem["loc"].InnerText = IndexFileName;
            newElem["lastmod"].InnerText = lastmoddate;
            newElem.Attributes.RemoveAll();

            // 2.  Add the new element to the end of the item list.
            doc.DocumentElement.AppendChild(newElem);
            fileStream.Position = 0;
            doc.Save(fileStream);
            fileStream.Flush();
            }
        catch 
            {

            }
        //doc.Save(fileStream);
    }

0 个答案:

没有答案