0

我的 XML 是:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <sitemap>
    <loc>http://localhost:2511/SF/sitemap_1.xml</loc>
    <lastmod>2013-11-11T04:17:57+00:00</lastmod>
  </sitemap>
  <sitemap>
    <loc>http://localhost:2511/SF/sitemap_2.xml</loc>
    <lastmod>2013-11-11T04:17:57+00:00</lastmod>
  </sitemap>
</urlset>

我想<sitemap>在这个 xml 中添加新节点。

我尝试:

public void LoadXML()
{
    string sSiteMapFilePath = HttpRuntime.AppDomainAppPath + "sitemap_index.xml";
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(HttpRuntime.AppDomainAppPath + "sitemap_index.xml");
    //XmlDocumentFragment docFrag = xmlDoc.CreateDocumentFragment();
    XmlNode node=GenerateIndexNode(Page.Request.Url.Scheme + "://" + Request.Url.Authority + "Test" + "/sitemap_" + "4" + ".xml");
    //docFrag.InnerXml = node.ToString();
    XmlNode childNode = xmlDoc.DocumentElement;
    childNode.InsertAfter(node, childNode.LastChild);
    xmlDoc.Save(sSiteMapFilePath);
}
public XmlNode GenerateIndexNode(string Loc)
{
    XmlDocument xd = new XmlDocument();
    xd.Load(HttpRuntime.AppDomainAppPath + "sitemap_index.xml");
    XmlElement nodeSite = xd.CreateElement("sitemap");
    XmlElement nodeLoc = xd.CreateElement("loc");
    nodeLoc.InnerText = Loc;
    XmlElement nodeMode = xd.CreateElement("lastmod");
    nodeMode.InnerText = DateTime.Now.ToString("yyyy-MM-ddThh:mm:ss+00:00");
    nodeSite.AppendChild(nodeLoc);
    nodeSite.AppendChild(nodeMode);
    return nodeSite;

}

愿望输出是:

 <?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
      <sitemap>
        <loc>http://localhost:2511/SF/sitemap_1.xml</loc>
        <lastmod>2013-11-11T04:17:57+00:00</lastmod>
      </sitemap>
      <sitemap>
        <loc>http://localhost:2511/SF/sitemap_2.xml</loc>
        <lastmod>2013-11-11T04:17:57+00:00</lastmod>
      </sitemap>
      <sitemap>
        <loc>New URL</loc>
        <lastmod>2013-11-11T04:17:57+00:00</lastmod>
      </sitemap>
    </urlset>

但我得到了错误:

The node to be inserted is from a different document context.

出了点问题。我错过了一些要理解的东西。谢谢你的帮助。

4

1 回答 1

0

您已经在 中加载了站点地图 xml 文件LoadXML(),无需在 中重新加载此文件GenerateIndexNode(),而是可以简单地将加载的文件的引用传递给此函数:

public XmlNode GenerateIndexNode(XmlDocument xd)
{
    XmlElement nodeSite = xd.CreateElement("sitemap");
    XmlElement nodeLoc = xd.CreateElement("loc");
    nodeLoc.InnerText = Loc;
    XmlElement nodeMode = xd.CreateElement("lastmod");
    nodeMode.InnerText = DateTime.Now.ToString("yyyy-MM-ddThh:mm:ss+00:00");
    nodeSite.AppendChild(nodeLoc);
    nodeSite.AppendChild(nodeMode);
    return nodeSite;
}

并且可以这样调用:

public void LoadXML()
{
    string sSiteMapFilePath = HttpRuntime.AppDomainAppPath + "sitemap_index.xml";
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(HttpRuntime.AppDomainAppPath + "sitemap_index.xml");

    XmlNode node=GenerateIndexNode(xmlDoc);

    XmlNode childNode = xmlDoc.DocumentElement;
    childNode.InsertAfter(node, childNode.LastChild);
    xmlDoc.Save(sSiteMapFilePath);
}
于 2013-11-12T07:16:01.287 回答