Hi there,
I have a problem where I am attempting to update a set of attributes with a fixed value contained within a repeating section of an XML document using the Microsoft.BizTalk.Streaming.ValueMutator
.
For example the XML document which I am attempting to update contains the following input:
<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>
The code which I am attempting to use to update the XML document is:
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);
The resulting output of this code is the file "Output.xml". Contained within the output document "Output.xml" is the following output:
<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>
As you will notice the "alpha" element's text value is updated incorrectly. The desired result is to update all attributes named "Type" with the value "Fruit". What is going wrong and how is this problem solved?