Click here to Skip to main content
14,580,612 members
Rate this:
Please Sign up or sign in to vote.
See more:
I need to display xml data as it is in a table.

Example:



<table><tbody><tr><td>Heading1</td> <td>Heading2</td> <td>Heading 3</td></tr><tr><td>value 1</td>  <td>value 2</td><td>
<arrayofmenu_bo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<menu_bo>
    <id>2
    <createdby>0
    <createdon>0001-01-01T00:00:00
    <modifiedby>0


</td></tr></tbody></table>


What I have tried:

when i try to display it is displaying in below format, but I need to show as it is in xml format (with nodes). How can I do this, please advise

Heading 1 Heading 2 Heading 3 

value 1  value 2    2   0    0001-01-01T00:00:00    0
Posted
Updated 21-Apr-20 4:29am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

To display XML text in a ML text file, you should use the CDATA tag:
CDATA - Wikipedia[^]
XML - CDATA Sections - Tutorialspoint[^]
<table><tbody><tr><td>Heading1</td> <td>Heading2</td> <td>Heading 3</td></tr><tr><td>value 1</td>  <td>value 2</td><td>
<![CDATA[<arrayofmenu_bo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<menu_bo>
    <id>2</id>
    <createdby>0</createdby>
    <createdon>0001-01-01T00:00:00</createdon>
    <modifiedby>0</modifiedby>
</menu_bo>
]]>
</td></tr></tbody></table>

Note that I added in bold closing tags which were missing from the XML text you provided.
   
Comments
Richard Deeming 21-Apr-20 10:22am
   
That doesn't seem to work in an HTML document - the output appears as:
Heading1   Heading2   Heading 3
value 1    value 2    2 0 0001-01-01T00:00:00 0 ]]>
Rate this:
Please Sign up or sign in to vote.

Solution 2

You'll need to HTML-encode the XML if you want it to appear "as-is" in your HTML document. In particular:
  • <&lt;
  • >&gt;
  • &&amp;

You'll also want to wrap it in a <pre> element to preserve the white-space.
<pre>
&lt;arrayofmenu_bo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
&lt;menu_bo&gt;
    &lt;id&gt;2&lt;/id&gt;
    &lt;createdby&gt;0&lt;/createdby&gt;
    &lt;createdon&gt;0001-01-01T00:00:00&lt;/createdon&gt;
    &lt;modifiedby&gt;0&lt;/modifiedby&gt;
&lt;/menu_bo&gt;
&lt;/arrayofmenu_bo&gt;
</pre>
Demo[^]
<pre>: The Preformatted Text element - HTML: Hypertext Markup Language | MDN[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100