Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Copying XML Fragments >Thread Next - Re: Copying XML Fragments Re: Copying XML FragmentsTo: 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/ | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
