Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Copying XML Fragments

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 3/11/2008 5:11:00 PM

news.microsoft.com wrote:
> I need to copy the data/structure from one XML file to another.
> 
> I found I can get to the data I need, it resides under a node I can navigate 
> to:
> 
> 
> 
> XPathDocument _docSrc = new XPathDocument(_pathSrc);
> 
> XPathNavigator navSrc = ((IXPathNavigable)_docSrc).CreateNavigator();
> 
> XPathNavigator grpSrc = navSrc.SelectSingleNode("/A/B/C/D");
> 
> XPathDocument _docDst = new XPathDocument(_pathDst);
> 
> XPathNavigator navDst = ((IXPathNavigable)_docDst).CreateNavigator();
> 
> XPathNavigator grpDst = navDst.SelectSingleNode("/A/B/C/D");
> 
> 
> 
> 
> Essentially, I need everything under the D node in Dst to be erased.  How do 
> I do that, first of all?
> 
> Second, I need to copy everything from under the D node in Src to under the 
> D node in Dst.  What is the simples way to do that?

If you want to manipulate XML then XPathDocument is not the right 
choice, use XmlDocument instead or XDocument in .NET 3.5.
As for your manipulation you could use e.g.
   XmlDocument dest = new XmlDocument();
   dest.Load(_pathDst);
   XmlNode D = dest.SelectSingleNode("/A/B/C/D");
   while (D.HasChildNodes)
   {
     D.RemoveChild(D.LastChild);
   }
to remove the children of D.
To copy nodes from one document to another you could use e.g.
   XmlDocument source = new XmlDocument();
   source.Load(_pathSrc);
   foreach (XmlNode child in source.SelectSingleNode("A/B/C/D/node()"))
   {
     D.AppendChild(D.OwnerDocument.ImportNode(child, true))
   }


-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent