I am writing C# TBB that takes the input from component source as XmlElement
then converts it to XPathDocument
for easy traversing. And then writing the fetched content to the memory stream using XmlWriter
. At the end I am pushing it to a output XmlDocument
. In the output xml document characters like < > are visible in the encoded format like <
>
. How can I prevent encoding of those characters?
<%@Import NameSpace="System.IO"%>
<%@Import Namespace="System.Xml.XPath"%>
<%@Import nameSpace="Tridion.ContentManager.CommunicationManagement"%>
using (MemoryStream ms = new MemoryStream())
{
TemplatingLogger log = TemplatingLogger.GetLogger(typeof(test));
XmlTextWriter securboxXmlWriter = new XmlTextWriter(ms, new System.Text.UTF8Encoding(true));
securboxXmlWriter.Indentation = 4;
securboxXmlWriter.Formatting = Formatting.Indented;
securboxXmlWriter.WriteStartDocument();
securboxXmlWriter.WriteStartElement("comp");
securboxXmlWriter.WriteAttributeString("xmlns", "http://www.w3.org/1999/xhtml");
securboxXmlWriter.WriteStartElement("con");
securboxXmlWriter.WriteStartElement("wf:wf");
securboxXmlWriter.WriteStartElement("wf:sI");
Component component=null;
string componentId = string.Empty;
try
{
component = engine.GetObject(package.GetByName(Package.ComponentName)) as Component;
log.Debug("componentId:"+component.Id);
}
catch (Exception e)
{
throw new InvalidOperationException("Component not found:" + e);
}
XmlElement componentxml = component.Content; // - this will give the xml Source of the component.
XmlDocument compXMLDoc = new XmlDocument();//- Creating the xml document
StringBuilder xmlTextBuilder = new StringBuilder();
XmlWriter xmlWriter = new XmlTextWriter(new StringWriter(xmlTextBuilder));
componentxml.WriteTo(xmlWriter);
compXMLDoc.Load(new StringReader(xmlTextBuilder.ToString()));
MemoryStream memStream = new MemoryStream();
compXMLDoc.Save(memStream);
memStream.Position = 0;
XPathDocument xpathDoc = new XPathDocument(memStream);//creating xpath document
String groupBodyComponent = String.Empty;
String gBCValue = String.Empty;
XPathNavigator nav = xpathDoc.CreateNavigator();
String value = string.Empty;
test t = new test();
// move to first element
nav.MoveToFirstChild();
nav.MoveToFirstChild();
//begin looping through the nodes
do
{
if (nav.Name.Equals("aH"))
{
securboxXmlWriter.WriteStartElement("wg:" + nav.Name);
if (nav.MoveToFirstChild())
{
gBC = nav.InnerXml;
switch(gBC){
case "bPTt":
while (nav.MoveToNext())
{
log.Debug(nav.Name);
if (nav.Name.Equals("value"))
{
gBCValue = nav.InnerXml;
}
securboxXmlWriter.WriteStartElement("wg:" + gBC);
value = t.replaceDynamicVariable(gBCValue);//will have some html tags in it
securboxXmlWriter.WriteElementString("text", value);
securboxXmlWriter.WriteEndElement();
}
break;
}
}
value = t.replaceDynamicVariable(nav.InnerXml);
securboxXmlWriter.WriteElementString("text", value);
securboxXmlWriter.WriteEndElement();
}
} while (nav.MoveToNext());
securboxXmlWriter.WriteEndElement();
securboxXmlWriter.WriteEndElement();
securboxXmlWriter.WriteEndElement();
securboxXmlWriter.WriteEndElement();
securboxXmlWriter.WriteEndDocument();
securboxXmlWriter.Flush();
securboxXmlWriter.Close();
Item output = package.GetByName("Output");
if (output != null)
{
package.Remove(output);
}
package.PushItem("Output", package.CreateStringItem(ContentType.Xml, Encoding.UTF8.GetString(ms.ToArray())));
}
Please check the code snippet and suggest me a way to solve the issue.
You can simply decode everything back with another TBB which you should place as the very last TBB in the template.
Also note, if you have a Dreamweaver TBB in you template, it will create the "Output" item with everything encoded (inline with Nuno's answer which makes the ContentType as HTML).
Simply decode it back. Don't worry about unencoding the characters that don't need to be unencoded (such as the percent sign) since they will be double-encoded, and when you convert back, you'll get exactly those back.
Here is a C# Fragment TBB you can use:
<%@ Import Namespace="System.Web"%>
Item outputItem = package.GetByName("Output");
string outputText = outputItem.GetAsString();
outputText = HttpUtility.HtmlDecode(outputText);
outputItem.SetAsString(outputText);
package.Remove(outputItem);
package.PushItem("Output", outputItem);