在Message上添加新节点

//Msg_XmlDocReceive是Receive Shape接收到的XmlDocument类型的消息
//Msg_XmlDoc4是XmlDocument类型消息,在其Message Assign Shape中

Case 1:
Msg_XmlDoc4=Msg_XmlDocReceive;
Msg_XmlDoc4.DocumentElement.AppendChild(Msg_XmlDoc4.CreateNode(System.Xml.XmlNodeType.Element,"Jackson","http://www.jackson.com"));

Send(Msg_XmlDoc4)后,接收到的文件内容和Msg_XmlDocReceive内容一致。

Case 2:
Msg_XmlDoc4=Msg_XmlDocReceive;
Msg_XmlDoc4=(System.Xml.XmlDocument)(Msg_XmlDoc4.CloneNode(true));
Msg_XmlDoc4.DocumentElement.AppendChild(Msg_XmlDoc4.CreateNode(System.Xml.XmlNodeType.Element,"Jackson","http://www.jackson.com"));

Send(Msg_XmlDoc4),接收到的文件比Case 1中接收到的内容多一个新添加的节点:<Jackson xmlns="http://www.jackson.com" /> 。

分析:Case 1中,Msg_XmlDoc4只是保存了对Msg_XmlDocReceive的引用,AppendChild方法的操作实际上是针对Msg_XmlDocReceive内容的。但是消息是不可变的,显然对构造过程已完成的Msg_XmlDocReceive的消息体的改动会被ignore。
        Case 2中,第二行代码克隆了消息体,因此改动是针对构造过程中的消息,所以添加节点是有效的。

PS:XmlDocument是XmlNode的子类。AppendChild方法调用不当容易出现“The node to be inserted is from a different document context. ”的异常问题。原因在于每个XmlNode节点都有一个Context,不同Context之间的节点拷贝需要使用ImportNode方法。节点Context的存在说明了为什么CreateNode方式被设计成非static方法。

Reference:http://msdn.microsoft.com/en-us/library/aa560493.aspx

你可能感兴趣的:(message)