如何将一个XML文档中的元素添加到另一个XML文档中? [英] How do I add an element from one XML document into another XML document?

查看:82
本文介绍了如何将一个XML文档中的元素添加到另一个XML文档中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在添加此对象objXMLInputDoc中的CitationDocumentBatch元素时遇到问题。我想将它添加到此对象objXMLCopyOfOriginalInputDoc。它应该作为< soapenv:body>的孩子添加。在objXMLCopyOfOriginalInputDoc中。



objXMLInputDoc对象包含以下内容

I am having trouble adding CitationDocumentBatch element that is in this object objXMLInputDoc. I want to add it to this object objXMLCopyOfOriginalInputDoc. It should be added as a child of <soapenv:body> inside objXMLCopyOfOriginalInputDoc.

objXMLInputDoc object contains the following

<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" xsi:schemaLocation="http://www.courts.state.mn.us/CourtXML/3 file:///H:/Deve/Schemas/CourtXML/CitationDocument_3_5.xsd">
	<CitationDocument>
		<Citation>
	</CitationDocument>
</CitationDocumentBatch>





objXMLCopyOfOriginalInputDoc包含以下内容



objXMLCopyOfOriginalInputDoc contains the following

<?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 or insert CitationDocumentBatch element here-->
	</soapenv:Body>
</soapenv:Envelope>





我想要的结果是objXMLCopyOfOriginalInputDoc,在我从objXMLInputDoc插入/添加CitationDocumentBatch之后会看起来像这样



The result I want would be in objXMLCopyOfOriginalInputDoc which after I insert/add CitationDocumentBatch from objXMLInputDoc will look like this

<?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" xsi:schemaLocation="http://www.courts.state.mn.us/CourtXML/3 file:///H:/Deve/Schemas/CourtXML/CitationDocument_3_5.xsd">
	<CitationDocument>
		<Citation>
	</CitationDocument>
</CitationDocumentBatch>
	</soapenv:Body>
</soapenv:Envelope>





我正在尝试这个在vb.net但我得到一个异常要插入的节点来自不同的文档上下文。

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

What I have tried:

I have tired this but I am getting an exception

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

推荐答案

您需要导入节点。



You need to import the node.

XmlNode node = objXMLInputDoc.SelectSingleNode("//CitationDocumentBatch");

XmlNode copiedNode = objXMLCopyOfOriginalInputDoc.OwnerDocument.ImportNode(node, true); //needs to import node, true = if you want deep copy


objXMLCopyOfOriginalInputDoc.DocumentElement.SelectSingleNode("soap:Body", objXMLNameSpaceManager).AppendChild(copiedNode);


我不再需要帮助。我为XmlDocument使用了ImportNode方法

I no longer need help. I used "ImportNode" method for XmlDocument
objXMLCopyOfOriginalInputDoc.DocumentElement.SelectSingleNode("soap:Body", objXMLNameSpaceManager).AppendChild(objXMLCopyOfOriginalInputDoc.ImportNode(objXMLInputDoc.SelectSingleNode("//CitationDocumentBatch"), True))


这篇关于如何将一个XML文档中的元素添加到另一个XML文档中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆