简体   繁体   中英

Append new Node with Child Node in XML

My XML is:

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

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:

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

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:

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

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.

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