Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 3rd, 2018, 11:10 AM
Friend of Wrox
 
Join Date: Apr 2013
Posts: 101
Thanks: 14
Thanked 0 Times in 0 Posts
Default How do I add/import xml document into another xml document?

I am having trouble adding xml document (CitationDocumentBatch) that is in this object objXMLInputDoc into another object objXMLCopyOfOriginalInputDoc. It should be added as a child of <soapenv:Body>

objXMLInputDoc object contains the following
Code:
CitationDocumentBatch schemaVersion="3:5" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<CitationDocument>
		<Citation>
	</CitationDocument>
</CitationDocumentBatch>
objXMLCopyOfOriginalInputDoc contains the following
Code:
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
	<soap:Header xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	</soap:Header>
	<soapenv:Body>
	<!--Add CitationDocumentBatch here-->
	</soapenv:Body>
</soapenv:Envelope>
The result I want would be in objXMLCopyOfOriginalInputDoc which after I insert/add CitationDocumentBatch from objXMLInputDoc will look like this
Code:
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
	<soap:Header xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	</soap:Header>
	<soapenv:Body>
	<CitationDocumentBatch schemaVersion="3:5" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<CitationDocument>
		<Citation>
	</CitationDocument>
</CitationDocumentBatch>
	</soapenv:Body>
</soapenv:Envelope>
Here is what I have tried.
Code:
objXMLCopyOfOriginalInputDoc.DocumentElement.SelectSingleNode("soap:Body", objXMLNameSpaceManager).AppendChild(objXMLInputDoc.SelectSingleNode("//CitationDocumentBatch"))
I am trying this in vb.net but I am getting an exception "The node to be inserted is from a different document context."
Code:
objXMLCopyOfOriginalInputDoc.DocumentElement.SelectSingleNode("soap:Body", objXMLNameSpaceManager).AppendChild(objXMLInputDoc.SelectSingleNode("//CitationDocumentBatch"))
Can someone help me with this? It might be very easy but I am not able to figure it out.
 
Old August 3rd, 2018, 11:22 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

There is an "ImportNode" method for "XmlDocument" you can use e.g.

Code:
objXMLCopyOfOriginalInputDoc.DocumentElement.SelectSingleNode("soap:Body", objXMLNameSpaceManager).AppendChild(objXMLCopyOfOriginalInputDoc.ImportNode(objXMLInputDoc.SelectSingleNode("//CitationDocumentBatch"), True))
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
winkimjr2 (August 3rd, 2018)
 
Old August 3rd, 2018, 11:47 AM
Friend of Wrox
 
Join Date: Apr 2013
Posts: 101
Thanks: 14
Thanked 0 Times in 0 Posts
Default

Thank you so much Martin. I spent time stuck but now it is working!
 
Old August 3rd, 2018, 03:30 PM
Friend of Wrox
 
Join Date: Apr 2013
Posts: 101
Thanks: 14
Thanked 0 Times in 0 Posts
Default How do I include all child elements for inserted document?

My mistake. All I needed to do was use a different object (objXMLParkingCitationsDoc) I do not need help.
The following code inserts xml document but the inserted document does not have all children inside CitationDocumentBatch node.
What do I need to do so all the child elements inside CitationDocumentBatch node are inserted also.

Code:
objXMLCopyOfOriginalInputDoc.DocumentElement.SelectSingleNode("soap:Body", objXMLNameSpaceManager).AppendChild(objXMLCopyOfOriginalInputDoc.ImportNode(objXMLInputDoc.SelectSingleNode("//CitationDocumentBatch"), True))

Last edited by winkimjr2; August 3rd, 2018 at 03:58 PM..
 
Old August 3rd, 2018, 03:53 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

The second argument to "ImportNode" being "True" should ensure a deep copy is imported (https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx) so I can't really see why you say "does not have all children". Can you show a minimal but complete sample of the .NET code and the XMLs to demonstrate the issue, allowing us to reproduce the problem?
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old August 6th, 2018, 08:22 AM
Friend of Wrox
 
Join Date: Apr 2013
Posts: 101
Thanks: 14
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Martin Honnen View Post
The second argument to "ImportNode" being "True" should ensure a deep copy is imported (https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx) so I can't really see why you say "does not have all children". Can you show a minimal but complete sample of the .NET code and the XMLs to demonstrate the issue, allowing us to reproduce the problem?
What I meant was, using the object objXMLParkingCitationsDoc worked because that is the object with the information I needed. The original object objXMLInputDoc I used as ImportNode did not have child elements. Thank you again for your help.





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to I add xml document to another xml document as a child element? winkimjr2 Visual Studio 2008 0 August 3rd, 2018 09:10 AM
Create an XML document using JAXP and XML fragments. WilliamYou XML 2 October 29th, 2012 03:44 PM
Split xml file with result document and javax.xml.transform.Transformer. nisargmca XSLT 3 January 12th, 2010 06:26 AM
add 5 years to date in three nodes in xml document Boris1 XSLT 4 November 25th, 2009 11:18 AM
Add and Remove Nodes in XML document. tutul128 XML 3 March 1st, 2004 10:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.