0

我正在编写 C# TBB,它从组件源获取输入,XmlElement然后将其转换XPathDocument为便于遍历。然后使用 . 将获取的内容写入内存流XmlWriter。最后,我将它推到 output 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())));                                

        }

请检查代码片段并建议我解决问题的方法。

4

4 回答 4

5

您可以简单地使用另一个 TBB 将所有内容解码回来,您应该将其作为模板中的最后一个 TBB 放置。

另请注意,如果您的模板中有 Dreamweaver TBB,它将创建所有编码的“输出”项目(与 Nuno 的答案内联,它将 ContentType 设为 HTML)。

只需将其解码回来。不要担心对不需要未编码的字符(例如百分号)进行未编码,因为它们将被双重编码,并且当您转换回来时,您会得到完全正确的那些。

这是您可以使用的 C# 片段 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);
于 2012-05-06T19:40:04.827 回答
2

您是否将此内容推送到包中?如果是,请在package.CreateStringItem方法中使用不同的 ContentType 选项。

其中一种内容类型将“按原样”保留任何特殊字符,无论是 HTML 还是文本,现在还不能 100% 确定是哪一种。

于 2012-05-06T18:26:24.457 回答
1
var a = HttpUtility.HtmlDecode(XmlizedString);

这避免了特殊字符的编码。解决了这个问题。

于 2012-05-07T09:06:53.110 回答
0

如果您尝试编写 xml而不编码 '<'、'>'、'&' 字符,则可以使用 WriteRaw() 方法。

例如

  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();
于 2012-11-13T00:09:59.130 回答