Write only InnerText of an Xml node with XmlTranslatorStream

Hi,

how can I write only the InnerText of an existing XML-Node in a message with XmlTranslatorStream? I tried to modify this example (http://biztalkmessages.vansplunteren.net/articles/dealing-with-base64):

 protected override int ProcessXmlNodes(int count)
        {
            bool readSucceeded = true;

            while (base.m_outputStream.Length < count && readSucceeded)
            {
                switch (streamStatus)
                {
                    case StreamStatus.Normal:
                        readSucceeded = base.m_reader.Read();
                        base.TranslateXmlNode();
                        break;

                    case StreamStatus.ElementFound:
                        //base.m_writer.WriteStartElement(elementToCreatePrefix, elementToCreateName, elementToCreateNamespace);
                        streamStatus = StreamStatus.StartElementWritten;
                        break;


                    case StreamStatus.StartElementWritten:
                        streamStatus = StreamStatus.InnerTextWritten;
                        break;

                    case StreamStatus.InnerTextWritten:
                        //base.m_writer.WriteEndElement();
                        
                        if (binaryReader == null)
                        {
                            // Open binary file.
                            FileStream inputFile = new FileStream(documentFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                            binaryReader = new BinaryReader(inputFile);
                        }                        

                        int bytesToRead = count - System.Convert.ToInt32(base.m_outputStream.Length);
                        byte[] buffer = new byte[bytesToRead];

                        int bytesRead = binaryReader.Read(buffer, 0, bytesToRead);

                        base.m_writer.WriteBase64(buffer, 0, bytesRead);

                        if (bytesRead < bytesToRead)
                        {
                            binaryReader.Close();
                            binaryReader = null;
                            streamStatus = StreamStatus.Normal;
                        }
                        break;
                }

                base.m_writer.Flush();
            }
            return (int)base.m_outputStream.Length;
        }

        protected override void TranslateEndElement(bool full)
        {
            base.TranslateEndElement(full);

            if (base.m_reader.LocalName == base64BinaryElementName)
            {
                
                streamStatus = StreamStatus.ElementFound;

            }
        }

but it throws an System.Xml.XmlException: Data at the root level is invalid. 

So what I' m missing? 

Thanks in advance,

Jrgen



August 28th, 2015 5:50am

Hi,

You can create a class XElementToXElementTransformStream,  to help with this kind of your requirement It reads XML elements from an input stream, then calls a developer-provided (context-specific) class that implements the IXElementToXElementTransformer interface.

Your sample Pipeline code will be like below

public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
{
  
    Stream originalStream = pInMsg.BodyPart.GetOriginalDataStream();
  
 
    IXElementToXElementTransformer transformer = new GroupCustomerReservations();
    Stream transformStream = new XElementToXElementTransformStream(
        originalStream, 
        transformer
    );
  
    pInMsg.BodyPart.Data = transformStream;
  
    pContext.ResourceTracker.AddResource(transformer);
  
    return pInMsg;
  
}

For detailed description you can refer :http://matricis.com/biztalk/streaming-xml-transformations-f/

Thanks

Abhishek

Free Windows Admin Tool Kit Click here and download it now
August 28th, 2015 5:03pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics