在XML中添加带有子节点的新节点

时间:2022-10-25 14:05:36

My XML is:

我的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>

I want to add new <sitemap> node in this xml.

我想在此xml中添加新的 节点。

I try :

我试试:

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;

}

Desire Out put is:

Desire Out put是:

 <?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>

But I got error:

但是我得到了错误:

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

要插入的节点来自不同的文档上下文。

Some thing going wrong.I am missing some thing to understand.Thanks for help.

有些事情出错了。我错过了一些要理解的东西。谢谢你的帮助。

1 个解决方案

#1


0  

You already load sitemap xml file in LoadXML(), no need to reload this file in GenerateIndexNode(), instead you can simply pass loaded file's reference to this function:

您已经在LoadXML()中加载了sitemap xml文件,无需在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;
}

and can call this way:

并可以这样调用:

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);
}

#1


0  

You already load sitemap xml file in LoadXML(), no need to reload this file in GenerateIndexNode(), instead you can simply pass loaded file's reference to this function:

您已经在LoadXML()中加载了sitemap xml文件,无需在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;
}

and can call this way:

并可以这样调用:

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);
}