indentation of input xml file after modification in custom pipeline in biztalk 2010

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
I NEED THE INDENTED XML SPECIALLY WHEN I OPEN IN NOTEPAD. THAT IS MY REQUIREMENT. XML SHOULD BE READABLE IN NOTEPAD FOR MANUAL EDITING.
I am trying to indent the contents of an input xml file but it is not working for me. I need to replace the head part of the xml as you can see in the code and then indent the contents of the entire xml to make the file readable. The below function receives
a XML message.
Please help me on this as soon as possible as I am new to c#.
Here is the code:
public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)<br/>
{<br/>
IBaseMessagePart bodyPart = pInMsg.BodyPart;
IBaseMessageContext context = pInMsg.Context;
string xmlNamespace = String.Empty;<br/>
if (bodyPart != null)<br/>
{<br/>
Stream originalStream = bodyPart.GetOriginalDataStream();
<br/>
if (originalStream != null)<br/>
{<br/>
XmlReaderSettings settings = new XmlReaderSettings();<br/>
settings.IgnoreComments = true;<br/>
settings.IgnoreProcessingInstructions = true;<br/>
settings.IgnoreWhitespace = true;<br/>

<br/>
XmlDocument xdoc = new XmlDocument();<br/>
XmlDocument Outputxdoc = new XmlDocument();
xdoc.Load(originalStream);
Outputxdoc.LoadXml("<POSLog xmlns=" http://www.nrf-arts.org/IXRetail/namespace/ http://www.nrf-arts.org/IXRetail/namespace/ "
xmlns:dtv=" http://www.datavantagecorp.com/xstore/ http://www.datavantagecorp.com/xstore/ " xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation=" http://www.nrf-arts.org/IXRetail/namespace/POSLog.xsd/ </POSLog http://www.nrf-arts.org/IXRetail/namespace/POSLog.xsd/ </POSLog >");<br/>
Outputxdoc.FirstChild.InnerXml = xdoc.FirstChild.InnerXml.Replace("ns0:", "");<br/>
Outputxdoc.FirstChild.InnerXml = Outputxdoc.FirstChild.InnerXml.Replace("xmlns:ns0=" http://www.nrf-arts.org/IXRetail/namespace/ http://www.nrf-arts.org/IXRetail/namespace/ "",
"");<br/>

<br/>

<br/>
byte[] outBytes = System.Text.Encoding.UTF8.GetBytes(Outputxdoc.OuterXml);<br/>
MemoryStream memStream_temp = new MemoryStream();
memStream_temp.Write(outBytes, 0, outBytes.Length);<br/>
memStream_temp.Position = 0;
MemoryStream memStream = new MemoryStream();<br/>
memStream = memStream_temp;
//originalStream = bodyPart.GetOriginalDataStream();
//for loop for all transaction nodes<br/>
//create one transaction node<br/>
//inner xml. Replace all nso<br/>
//append to Outputxdoc
<br/>
using (XmlReader reader = XmlReader.Create(memStream))<br/>
{<br/>
XmlWriterSettings ws = new XmlWriterSettings();<br/>
ws.Indent = true;<br/>
ws.Encoding = System.Text.Encoding.UTF8;<br/>

<br/>
MemoryStream outputStream = new MemoryStream();
using (XmlWriter xmlWriter = XmlWriter.Create(outputStream, ws))<br/>
{<br/>
reader.Read();<br/>
if (reader.NodeType == XmlNodeType.XmlDeclaration)<br/>
{<br/>
xmlWriter.WriteStartDocument();<br/>
reader.Read();<br/>
}

<br/>
if (reader.NodeType == XmlNodeType.Element)<br/>
{<br/>
xmlWriter.WriteRaw(reader.ReadOuterXml());<br/>
reader.Read();<br/>
}<br/>
}
outputStream.Position = 0;
bodyPart.Data = outputStream;
pContext.ResourceTracker.AddResource(memStream_temp);<br/>
pContext.ResourceTracker.AddResource(memStream);<br/>
}
<br/>
}<br/>
}
return pInMsg;<br/>
}
#endregion<br/>
}
<br/>

View the full article
 
Back
Top