2008-11-17 30 views
1

我有一個問題,我嘗試使用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」的所有屬性。出了什麼問題,這個問題是如何解決的?

回答

2

您需要在您的XPath表達式阿爾法元素。

我用下面的表達跑代碼:

string xpathToUpdate = "/*[namespace-uri()='http://Test.Schemas']/*[local-name()='NodeA']/*[local-name()='NodeB']/*[local-name()='alpha']/@Type"; 

,得到了下面的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="Car" Type="fruit"> 
     </ns0:alpha> 
     <ns0:alpha Id="4" Value="Duck" Type="fruit"> 
     </ns0:alpha> 
    </ns0:NodeB> 
    </ns0:NodeA> 
</ns0:TestXML> 

看着你發佈的代碼,你可能已經看到在使用ValueMutator這些文章,但只是在有好的信息hereherehere

heh - 剛纔意識到最後一個是我的同事之一。小世界。

0

使用的XPath表達式:

      //namespace-uri()='http://Test.Schemas']/*[local-name()='NodeA']/*[local-name()='NodeB']/*[@Type]

僅選擇具有 「類型」 屬性的lements。

最有可能需要的是:

      /*[namespace-uri()='http://Test.Schemas']/*[local-name()='NodeA']/*[local-name()='NodeB']/@Type

希望這有助於。

乾杯,

Dimitre Novatchev

+0

編輯答案 - XPath表達式中的星號未顯示,因爲它們在粗體(** ... **)字段內。請再試一次。 - Dimitre Novatchev(0秒前) – 2008-11-18 00:42:21

+0

您修改了答案結果,未對輸入文檔進行任何更改。 – chinna 2008-11-18 01:29:28