Update the XML attribute in the repeating section with Microsoft.BizTalk.Streaming.ValueMutator
I have a problem when I try to update a set of fixed value attributes contained in a repeating section of an XML document using Microsoft.BizTalk.Streaming.ValueMutator
.
For example, the XML document I'm trying 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 I'm trying to use to update the XML document:
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 "Output.xml" file. The output document "Output.xml" contains 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 text value of the alpha element is not being updated correctly. The desired result is to update all attributes named "Type" with the value "Fruit". What is going wrong and how is this problem solved?
source to share
You need to include the alpha element in the XPath expression.
I ran your code using the following expression:
string xpathToUpdate = "/*[namespace-uri()='http://Test.Schemas']/*[local-name()='NodeA']/*[local-name()='NodeB']/*[local-name()='alpha']/@Type";
and got the following 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>
Looking at the code you've posted, you've probably seen these articles on using ValueMutator, but just in case there is good information here , here, and here .
Heh - I just realized that the last of them is one of my employees. Small world.
source to share
XPath expression used:
//namespace-uri()='http://Test.Schemas']/*[local-name()='NodeA']/*[local-name()='NodeB']/*[@Type]
selects only items that have the "Type" attribute.
Most likely you need:
/*[namespace-uri()='http://Test.Schemas']/*[local-name()='NodeA']/*[local-name()='NodeB']/@Type
Hope it helped.
Greetings,
Dimitar Novachev
source to share