我有一個問題,我嘗試使用Microsoft.BizTalk.Streaming.ValueMutator
更新XML文檔的重複部分中包含的固定值的一組屬性。使用Microsoft.BizTalk.Streaming.ValueMutator更新重複節中的XML屬性
比如我在嘗試更新XML文檔包含以下輸入:
<ns0:TestXML xmlns:ns0="http://Test.Schemas">
<ns0:NodeA>
<ns0:NodeB>
<ns0:alpha Id="1" Value="Apple" Type=""></ns0:alpha>
<ns0:alpha Id="2" Value="Banana" Type=""></ns0:alpha>
<ns0:alpha Id="3" Value="Car" Type=""></ns0:alpha>
<ns0:alpha Id="4" Value="Duck" Type=""></ns0:alpha>
</ns0:NodeB>
</ns0:NodeA>
</ns0:TestXML>
這我試圖用它來更新XML文檔中的代碼是:
XmlDocument xDocInput = new XmlDocument();
XmlDocument xDocOutput = new XmlDocument();
string inputFileName = @"C:\Input.xml";
string outputFileName = @"C:\Output.xml";
string newValue = "fruit";
string xpathToUpdate = "/*[namespace-uri()='http://Test.Schemas']/*[local-name()='NodeA']/*[local-name()='NodeB']/*[@Type]";
xDocInput.Load(inputFileName);
using (MemoryStream memstream = new MemoryStream())
{
xDocInput.Save(memstream);
memstream.Position = 0;
XPathCollection queries = new XPathCollection();
queries.Add(new XPathExpression(xpathToUpdate));
//ValueMutator xpathMatcher = new ValueMutator(this.XPathCallBack);
//Get resulting stream into response xml
xDocOutput.Load(new XPathMutatorStream(memstream, queries, delegate(int matchIdx, XPathExpression expr, string origValue, ref string finalValue) { finalValue = newValue; }));
//System.Diagnostics.Trace.WriteLine("Trace: " + memstream.Length.ToString());
}
xDocOutput.Save(outputFileName);
的此代碼的輸出結果是文件「Output.xml」。輸出文檔中包含的「的Output.xml」是下面的輸出:
<ns0:TestXML xmlns:ns0="http://Test.Schemas" >
<ns0:NodeA>
<ns0:NodeB>
<ns0:alpha Id="1" Value="Apple" Type="" >fruit</ns0:alpha>
<ns0:alpha Id="2" Value="Banana" Type="" >fruit</ns0:alpha>
<ns0:alpha Id="3" Value="Child" Type="" >fruit</ns0:alpha>
<ns0:alpha Id="4" Value="Duck" Type="" >fruit</ns0:alpha>
</ns0:NodeB>
</ns0:NodeA>
</ns0:TestXML>
正如你會發現「阿爾法」元素的文本值不正確地更新。所需的結果是使用值「Fruit」更新名爲「Type」的所有屬性。出了什麼問題,這個問題是如何解決的?
編輯答案 - XPath表達式中的星號未顯示,因爲它們在粗體(** ... **)字段內。請再試一次。 - Dimitre Novatchev(0秒前) – 2008-11-18 00:42:21
您修改了答案結果,未對輸入文檔進行任何更改。 – chinna 2008-11-18 01:29:28