Can we use html tags in xml?

  • Remove From My Forums

  • Question

  • User-524191788 posted

    hi,

    Thanks for your time.

     i have created a small web application with XML. It is working fine but In this XML file, i want to add html tags in one of the column, named Content. And html tags  like: bold,italic,para and so....on.

     I hope this is clear.

    XML file is as follows:profile.xml

    xmlversion="1.0"encoding="utf-8" ?>

    <

    profiles>

    <

    useruser_Id="1" name="Name1"designation="Webdeveloper" content="Hi how are you doing? I want to make some text bold and some text italic and i'd want to insert image here in this content and vice versa. thanks for bearing">user>

    <

    useruser_Id="2"name="Name2" designation="Web Designer and webdeveloper"content="Hi how are you doing? I want to make some text bold and some text italic and i'd want to insert image here in this content and vice versa. thanks for bearing">user>

    <

    useruser_Id="3" name="Name3"designation="Webdeveloper" content="Hi how are you doing? I want to make some text bold and some text italic and i'd want to insert image here in this content and vice versa. thanks for bearing">user>

    <

    useruser_Id="4"name="Name4" designation="Graphic Designer"content="Hi how are you doing? I want to make some text bold and some text italic and i'd want to insert image here in this content and vice versa. thanks for bearing">user>

    profiles>

    and default.aspx as follows

    <%

    @ PageLanguage="VB"AutoEventWireup="false"CodeFile="Default.aspx.vb" Inherits="_Default" %>

    DOCTYPE htmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <

    htmlxmlns="http://www.w3.org/1999/xhtml">

    <

    head runat="server"> <title>Untitled Pagetitle>

    head>

    <

    body> <formid="form1"runat="server"> <div> <asp:XmlDataSourceID="XmlDataSource1" runat="server"DataFile="~/App_Data/Profile.xml" XPath='/profiles/user'>asp:XmlDataSource> <asp:DataListID="DataList1" runat="server"Style=""DataSourceID="XmlDatasource1"> <ItemTemplate> <tablewidth="780px"style="background:#999933; border:1px;"> <tr> <tdstyle="width:20%; background-color:White">User IDtd><tdstyle="width:20%;background-color:White">User Nametd><tdstyle="width:60%; background-color:White">Contenttd>tr> <tr><td><%# Eval("user_Id") %>td><td><%# Eval("name") %>td><td><%#Eval("content")%>td>tr> table> ItemTemplate> asp:DataList>div> form>

    body>

    html>

    thanks once again.

Answers

  • User-512755392 posted

    Thanks for your infomation.

    You have to decode html tag before write it to Response.

    Here is my tested code(I converted from C# to VB):

       
    Imports System
    Imports System.Data
    Imports System.Configuration
    Imports System.Collections
    Imports System.Web
    Imports System.Web.Security
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Web.UI.WebControls.WebParts
    Imports System.Web.UI.HtmlControls
    Imports System.Xml
    Imports System.Xml.Xsl
    Imports System.Xml.XPath
    Imports System.IO
    Public Class xml_xslt_display
        Inherits System.Web.UI.Page
        
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            Dim output As String = ""
            Dim msXml As MemoryStream = New MemoryStream
            Dim writer As XmlTextWriter = New XmlTextWriter(msXml, System.Text.Encoding.UTF8)
            Try 
                Response.Clear
                Dim xml_p As String = "test57.xml"  
                Dim xslt_p As String = "xslt57.xsl"
                Dim xmlPath As String = Server.MapPath(xml_p)
                Dim xslPath As String = Server.MapPath(xslt_p)
                'Instantiate the XPathDocument Class
                Dim doc As XPathDocument = New XPathDocument(xmlPath)
                'Instantiate the XslTransform Class
                Dim transform As XslCompiledTransform = New XslCompiledTransform
                transform.Load(xslPath)
                transform.Transform(doc, Nothing, writer)
                msXml.Position = 0
                Dim xmlReader As XmlReader = XmlReader.Create(msXml)
                
                While xmlReader.Read
                    output = xmlReader.ReadOuterXml
                    
                End While
                Response.Write(Server.HtmlDecode(output))
                Response.End
                xmlReader.Close
            Finally
                msXml.Close
                writer.Flush
                writer.Close
            End Try
        End Sub
    End Class
    
    
     

    I transform xml with xslt manually instead of using XML Control.

    • Marked as answer by Thursday, October 7, 2021 12:00 AM

  • User-524191788 posted

    HI,

    I appreciate your work, thanks.

    I've got another way also in very very short "one line code". I think you might be knowing this.

    in XSLT file we have to add this code:       disable-output-escaping="yes"/>      where we want to add xhtml source code in element of XML file.

    I hope it is clear.

    Thank you All

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM

What tags are valid in XML?

XML Naming Rules.
Element names are case-sensitive..
Element names must start with a letter or underscore..
Element names cannot start with the letters xml (or XML, or Xml, etc).
Element names can contain letters, digits, hyphens, underscores, and periods..
Element names cannot contain spaces..

Can you create tags in XML?

You can create tags from scratch or load them from another source, such as an InDesign document, InCopy document, or DTD file. Use any of the following methods to create or load XML tags for your document: Create a tag with the New Tag command. Load tags from an XML file or another document.

How do you escape HTML tags in XML?

The only option really is to escape the data, or use cdata. Otherwise the XML parser will crash when it finds the malformed markup.

How do I tag an XML file?

Start Tag. The beginning of every non-empty XML element is marked by a start-tag. ... .
End Tag. Every element that has a start tag should end with an end-tag. ... .
Empty Tag. The text that appears between start-tag and end-tag is called content. ... .
XML Tags Rules. Following are the rules that need to be followed to use XML tags −.