Answered by:
Merging two Windows.Data.Xml.Dom.XmlDocument objects in one - issue

Question
-
I am trying to merge two Windows.Data.Xml.Dom.XmlDocument objects into a single one, ie. i want to add all elements of 2nd object to the beginning of the 1st one.
To accomplish this I am using something like:
obj1.insertBefore(obj2Items[i], obj1Items[0]);
but I get "JavaScript runtime error: Validate failed because the node provided does not belong to this document."
Any ideas on this?
Thanx in advance
Monday, July 9, 2012 3:29 PM
Answers
-
The error message is telling you precisely what the problem is: "the node provided does not belong to this document"
You cannot add a node that is not a child of the document.
Check out the documentation for this method and you will see: http://msdn.microsoft.com/en-us/library/windows/apps/windows.data.xml.dom.xmldocument.insertbefore.aspx
"newChild
Type: IXmlNode
The address of the new node to be inserted. The node passed here must be a valid child of the current XML DOM document node.
Jeff Sanders (MSFT)
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Monday, July 9, 2012 6:55 PM
- Marked as answer by MingXu-MSFTMicrosoft employee, Moderator Thursday, July 19, 2012 11:13 AM
Monday, July 9, 2012 6:55 PMModerator -
Yes, iterate over the source XmlDocument and createElement on the target. Then you can insert the copy of the node where ever you wish.
Jeff Sanders (MSFT)
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Tuesday, July 10, 2012 12:16 PM
- Marked as answer by MingXu-MSFTMicrosoft employee, Moderator Thursday, July 19, 2012 11:13 AM
Tuesday, July 10, 2012 12:16 PMModerator
All replies
-
The error message is telling you precisely what the problem is: "the node provided does not belong to this document"
You cannot add a node that is not a child of the document.
Check out the documentation for this method and you will see: http://msdn.microsoft.com/en-us/library/windows/apps/windows.data.xml.dom.xmldocument.insertbefore.aspx
"newChild
Type: IXmlNode
The address of the new node to be inserted. The node passed here must be a valid child of the current XML DOM document node.
Jeff Sanders (MSFT)
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Monday, July 9, 2012 6:55 PM
- Marked as answer by MingXu-MSFTMicrosoft employee, Moderator Thursday, July 19, 2012 11:13 AM
Monday, July 9, 2012 6:55 PMModerator -
that makes sense.
but is there a way to merge 2 Windows.Data.Xml.Dom.XmlDocument objects?
Tuesday, July 10, 2012 8:34 AM -
Yes, iterate over the source XmlDocument and createElement on the target. Then you can insert the copy of the node where ever you wish.
Jeff Sanders (MSFT)
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Tuesday, July 10, 2012 12:16 PM
- Marked as answer by MingXu-MSFTMicrosoft employee, Moderator Thursday, July 19, 2012 11:13 AM
Tuesday, July 10, 2012 12:16 PMModerator