我正在编写C#TBB,它将来自组件源的输入作为XmlElement
,然后将其转换为XPathDocument
以便于遍历。然后使用XmlWriter
将获取的内容写入内存流。最后,我将其推送到输出XmlDocument
。在输出xml文档中的字符如&lt; &GT;以<
>
等编码格式显示。如何防止对这些字符进行编码?
<%@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())));
}
请检查代码段并建议我解决问题的方法。
答案 0 :(得分:5)
您可以使用另一个TBB将所有内容解码回来,您应将其作为模板中最后一个TBB放置。
另请注意,如果您的模板中有Dreamweaver TBB,它将创建包含所有内容的“输出”项(与Nuno的答案内联,使ContentType为HTML)。
简单地将其解码回来。不要担心对不需要编码的字符(例如百分号)进行编码,因为它们将被双重编码,当您转换回来时,您将完全得到这些字符。
以下是您可以使用的C#Fragment TBB:
<%@ 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);
答案 1 :(得分:2)
您是否将此内容推送到套餐中?如果是,请使用package.CreateStringItem
方法中的不同ContentType选项。
其中一种内容类型将保留任何特殊字符&#34;格式为#34; HTML或文本,而不是100%确定现在哪一个。
答案 2 :(得分:1)
var a = HttpUtility.HtmlDecode(XmlizedString);
避免了特殊字符的编码。 解决了这个问题。
答案 3 :(得分:0)
你试图写xml 而不用编码'&lt;','&gt;','&amp;'您可以使用WriteRaw()方法的字符。
e.g。
var xmlFile = "C:\Temp\Test.xml";
var xmlstring = "<Nodes><Node>1</Node><Node>2</Node>><Node>3</Node></Nodes>";
XmlTextWriter writer;
writer = new XmlTextWriter(xmlFile, Encoding.UTF8);
writer.WriteRaw(xmlstring);
writer.Flush();
writer.Close();