locked
Enable HTML tag in XML Text using C# RRS feed

  • Question

  • User-1256377279 posted

    Hi,

    How to enable HTML tag when we serialized XML string 

    Current Output

    <ScopeContent>&lt;p&gt;Genre: Architectural Photography&lt;/p&gt;&lt;p&gt;View looking across river towards temple.&lt;/p&gt;&lt;p&gt; Subjects 
    Indian architecture; Indian architecture (Hindu); Stein Collection; temples&lt;/p&gt;</ScopeContent>

    Expected Output

    <ScopeContent>
    
    <p>Genre: Architectural Photography</p><p>View looking across river towards temple.</p><p> Subjects = Indian architecture; Indian architecture (Hindu); Stein Collection; temples</p>
    
    </ScopeContent>

    I Used below to method to serialised Object to XML

            public string getXmlString(object o, string level)
            {
                StringWriter l_StringWriter = new StringWriter();
                XmlTextWriter l_TextWriter = new XmlTextWriter(l_StringWriter);
                XmlDocument l_Doc = getXmlDocument(o, level);
    
               
    
                l_Doc.WriteTo(l_TextWriter);
    
                return l_StringWriter.ToString();
    
            }
    
            public virtual XmlDocument getXmlDocument(object o, string Level)
            {
                MemoryStream l_Stream = new MemoryStream();
                XmlDocument l_Doc = new XmlDocument();
    
                XmlSerializer l_Serializer = new XmlSerializer(o.GetType(), new XmlRootAttribute(Level));
                XmlSerializerNamespaces l_SerializerNamespaces
                                              = new XmlSerializerNamespaces();           
    
                // Remove the default namespaces.
                l_SerializerNamespaces.Add("", "");
                l_Serializer.Serialize(l_Stream, o, l_SerializerNamespaces);
                l_Stream.Position = 0;            
                l_Doc.Load(l_Stream);          
                l_Stream.Close();
                return l_Doc;
            }
    

    Many Thanks

    Thursday, February 21, 2019 2:51 PM

All replies

  • User475983607 posted

    How to enable HTML tag when we serialized XML string 

    Use CDATA

    <![CDATA[An in-depth look at creating applications with XML, using <, >,]]>

    https://msdn.microsoft.com/en-us/library/ms256076%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Thursday, February 21, 2019 3:55 PM
  • User-893317190 posted

    Hi shabbir_215,

    Don't know how you write your code and what you want to do, but Html tags are legal xml tag, if you haven't encoded your html , it will truly show in xml.

    Below is my test code.

    string result = getXmlString(  @"<p>Genre: Architectural Photography</p><p>View looking across river towards temple.</p><p> Subjects = Indian architecture; Indian architecture (Hindu); Stein Collection; temples</p>",
    "ScopeContent");
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(result);
                string xml  = HttpUtility.HtmlDecode(xmlDocument.InnerText);  // get the encoded html, will have  >&lt;p&gt
                xml = "<ScopeContent>" + xml + "</ScopeContent>";  // decode the text to html  (will not have &lt;p&gt)
               
                File.WriteAllText(Server.MapPath("/xmlDemo/access.xml"), xml); // write to a file

    The result should be

    <ScopeContent><p>Genre: Architectural Photography</p><p>View looking across river towards temple.</p><p> Subjects = Indian architecture; Indian architecture (Hindu); Stein Collection; temples</p></ScopeContent>

    Best regards,

    Ackerly Xu

    Friday, February 22, 2019 3:13 AM