Monday, January 24, 2011

Debatching with xlinq in Receive Pipeline

sample Xml:


<_x0032_100>
name
providerid
loc

<_x0032_200>

2010
66
dec
a
100


2010
76
Dec
item
100


2011
2011
Dec
ab
100


2011
2011
Sep
ab
100


2011
2011
Dec
ab
100


2011
2011
Dec
ab
100


<_x0032_300>
sc
dcdc

<_x0032_400>
lc
25



use the following.

1.Create a disassembler pipeline component.
2.Use the following code in the disassemble stage

public void Disassemble(IPipelineContext pc, IBaseMessage inmsg)
{
try
{
// Clone the message context so we can associate it with the new message later
originalMsgContext = PipelineUtil.CloneMessageContext(inmsg.Context);
// create the xml doc
incomingXML.Load(inmsg.BodyPart.GetOriginalDataStream());

splittedNode = incomingXML.SelectSingleNode(strxpath);


}
catch (Exception ex)
{

throw (ex);
}
}

/// Input message
Then in the GetNext use something like this....


public IBaseMessage GetNext(IPipelineContext context)
{
XmlDocument currentMessage = new XmlDocument();
IBaseMessage newMsg = null;
List strdocs = new List();
StringReader strreadRead = new StringReader(splittedNode.OuterXml);


//XLINQ query with year,renderingid and month
var grouped = from c in System.Xml.Linq.XElement.Load(strreadRead).Elements("serviceline") group c by new { year = c.Element("year").Value, renderingid = c.Element("renderingid").Value, month = c.Element("month").Value } into c select c;

foreach (var serviceline in grouped)
{
string strgroup = "<_x0032_200>";
foreach (var temps in serviceline)
{
strgroup += temps.ToString();
strgroup += "\n";
}
strgroup = strgroup + "";

strdocs.Add(strgroup);
strgroup = string.Empty;

}

if ((strdocs.Count > 0) && (splitCounter < editnode =" incomingXML.SelectSingleNode(strxpath);" fragment =" incomingXML.CreateDocumentFragment();" innerxml =" strdocs[splitCounter];" splitcounter =" splitCounter" memstream =" new" newmsg =" context.GetMessageFactory().CreateMessage();" newmsgpart =" context.GetMessageFactory().CreateMessagePart();" data =" memStream;" iprop =" 0;" val =" originalMsgContext.ReadAt(iProp,">

No comments: