How to display image using xslt and xml? - asp.net

I have been trying to display image for quite sometimes now and some of the solutions in this website does not work for me...
This is my xslt code
<xsl:template match="/">
<div class="main">
<h2>Product Catalogue</h2>
<xsl:for-each select="productdetails/product">
<div class="box">
<img src="<xsl:value-of select="product_image"/>"> </img>
<xsl:value-of select="format-number(product_Price,'0.00')" />
<h3>
<xsl:value-of select="product_Name"/>
</h3>
RM<xsl:value-of select="format-number(product_Price,'0.00')" />
<br/>
<p style="font-size:9pt; font-style: italic;">
<xsl:value-of select="product_description"/>
</p>
<br/>
Add to Cart
</div>
</xsl:for-each>
</div>
</xsl:template>
Just so you know product_image in the xml is already the imagepath thats why I did not need to write the path in the xslt code.

In XML, a tag cannot contain another tag - so this is invalid code:
<img src="<xsl:value-of select="product_image"/>"> </img>
Use either:
<img>
<xsl:attribute name="src">
<xsl:value-of select="product_image"/>
</xsl:attribute>
</img>
or:
<img src="{product_image}"/>
To understand the 2nd syntax, read about Attribute Value Templates.

Related

Read more/less button for multiple content

I've been trying to display several sheets thanks to the more/less button with pure CSS based on this example, one of the solution suggested for Pure CSS - Read more/Read Less images. I have no problem displaying the first sheet. But for the others, it doesn't work. Each time I click on the "read more" button, only the first sheet appears. I guess that I need to change the id input ref. As you will see <xsl:for-each select="./#xml:id"> is a different sheet.
This is a xsl section:
<xsl:template match="Name of the template">
<xsl:for-each select=".">
<xsl:if test="count(./key('whatAction-subCat', #xml:id)) != 0">
<li>
<!-- some data -->
<br />
<xsl:call-template name="whatResult"/>
</li>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template name="whatResult">
<input type="checkbox" class="read-more-state" id="result" />
<div class="read-more-wrap">
<xsl:text>Read</xsl:text>
<span class="read-more-target">
<table class="table-2">
<caption>What Result</caption>
<tr>
<th>Occur. in</th>
<th>What Role</th>
<th>What Context</th>
<th>What Sphere</th>
</tr>
<xsl:for-each select="./#xml:id">
<!-- verb competition -->
<xsl:call-template name="contend"/>
<!-- verb motion -->
<xsl:call-template name="self_motion"/>
<xsl:call-template name="arriving"/>
<!-- etc -->
...
</xsl:for-each>
</table>
</span>
</div>
<label for="result" class="read-more-trigger"></label>
</xsl:template>
A few print screen for a better understanding: show less btn, show more btn, and show more of several btn.
As you will see, another concern is the space between the hidden content and the more button. I need to look to the CSS--one step at a time...
In advance, thanks so very much for your help!
Vanessa
I found the following solution:
My main concern is to display a long list of data at the request of a user. Thus, I choose the modal solution based on this example. In order to avoid the display of the same sheet, I added the modal solution to each <xsl:template name="A NAME>.
<!-- I call the templates where I add the modal solution -->
<xsl:template match="category[#corresp]">
<xsl:for-each select="./#xml:id">
<!--to display content in each sub-category, ex. 'contend', 'self_motion' -->
<xsl:if test=".!contains(., 'contend') and //ref[#corresp='#contend'] ">
<xsl:call-template name="contend"/>
</xsl:if>
<xsl:if test=".!contains(., 'self_motion') and //ref[#corresp='#self_motion'] ">
<xsl:call-template name="self_motion"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
<!-- some templates of data with the modal solution -->```
<xsl:template name="contend">
<div id="contend">
</div>
<a href="#modal-container-contend" aria-label="Open Navigation">
<xsl:text>Read the detailed structural unit for this sub-category</xsl:text>
</a>
<div id="modal-container-contend">
<div class="modal">
<table class="table-2">
<caption>What Result</caption>
<tr>
<th>Occur. in</th>
<th>What Role</th>
<th>What Context</th>
<th>What Sphere</th>
<th style="text-align: center">What Behavior</th>
</tr>
<xsl:for-each
select="//ref[#corresp = '#contend']/ancestor::node()/../following-sibling::ref[#n = '2']">
<xsl:call-template name="following-subCatVerb"/>
</xsl:for-each>
</table>
close
</div>
</div>
</xsl:template>
<xsl:template name="self_motion">
<div id="self_motion">
</div>
<a href="#modal-container-self-motion" aria-label="Open Navigation">
<xsl:text>Read the detailed structural unit for this sub-category</xsl:text>
</a>
<div id="modal-container-self-motion">
<div class="modal">
<table class="table-2">
<caption>What Result</caption>
<tr>
<th>Occur. in</th>
<th>What Role</th>
<th>What Context</th>
<th>What Sphere</th>
<th style="text-align: center">What Behavior</th>
</tr>
<xsl:for-each
select="//ref[#corresp = '#self_motion']/ancestor::node()/../following-sibling::ref[#n = '2']">
<xsl:call-template name="following-subCatVerb"/>
</xsl:for-each>
</table>
close
</div>
</div>
</xsl:template>
<!-- the same logic for same sheets of data-->
And the CSS for the modal solution:
#modal-container-contend,
#modal-container-self-motion,
#modal-container-arriving,
#modal-container-satisfying {position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.8); justify-content: center; align-items: center; display: flex;}
.modal {width: 70%; background: #fff; padding: 20px; text-align: center;}
#modal-container-contend:not(:target),
#modal-container-self-motion:not(:target),
#modal-container-arriving:not(:target),
#modal-container-satisfying:not(:target) {opacity: 0; visibility: hidden; transition: opacity 1s, visibility 1s;}
#modal-container-contend:target,
#modal-container-self-motion:target,
#modal-container-arriving:target,
#modal-container-satisfying:target {opacity: 1; visibility: visible; transition: opacity 1s, visibility 1s;}

Get last item in xslt

I have this piece of XSLT
<xsl:template match="/">
<xsl:variable name="boxes" select="$currentPage/boxes/descendant::nodeId [text() != '']" />
<xsl:if test="count($boxes) > 0">
<div id="boxContainer">
<ul class="noList" id="frontBoxes">
<xsl:for-each select="$boxes">
<xsl:variable name="node" select="umbraco.library:GetXmlNodeById(.)" />
<xsl:variable name="imageNodes" select="umbraco.library:Split($node/descendant::boxImage, ',')" />
<xsl:for-each select="$imageNodes/descendant::value">
<xsl:variable name="image" select="umbraco.library:GetMedia(.,0)" />
<li>
<a class="boxLink">
<xsl:attribute name="href">
<xsl:value-of select="umbraco.library:NiceUrl($node/descendant::boxLink)" />
</xsl:attribute>
<xsl:value-of select="$node/#nodeName" />
</a>
<img src="{Eksponent.CropUp:Url($image/umbracoFile, 'frontBoxes')}" width="{$image/umbracoWidth}" height="{$image/umbracoHeight}" alt="" />
<xsl:value-of select="$node/descendant::bodyText" />
</li>
</xsl:for-each>
</xsl:for-each>
</ul>
</div>
</xsl:if>
</xsl:template>
If runs through a maximum and minimum of for front page boxes with an image, text and a link.
How can I get the last item so that I can output class="last" in the last <li>
I have tried with <xsl:if test="position()"> inside the last for-each but that doesn't work as intended.
use
<xsl:choose>
<xsl:when test="position()=last()">
<!-- set class="list" -->
</xsl:when>
<xsl:otherwise>
<!-- without class="list" -->
</xsl:otherwise>
</xsl:choose>

XSLT enumerate each child element

I am trying to create a super generic XSLT that basically can only count on the table & row elements being there. I have the following sample XML I am trying to make an XSLT for:
<?xml version="1.0" encoding="UTF-8"?>
<table>
<row>
<id>5311</id>
<status>Active</status>
<id_vendor>Verizon</id_vendor>
<mobile_number>555123456</mobile_number>
</row>
<row>
<id>5312</id>
<status>Inactive</status>
<id_vendor>Sprint</id_vendor>
<mobile_number>555123457</mobile_number>
</row>
<row>
<id>5313</id>
<status>Active</status>
<id_vendor>ATT</id_vendor>
<mobile_number>555123458</mobile_number>
</row>
</table>
The problem is that I do not know what the fields are going to end up being (id, status, id_vendor, mobile_number, etc. are all subject to change.
I want to make pleasant to look at view such as:
<div style="background-color:#E3CA87;padding:.2em">
<div style="background-color:#F1E2BB;padding:.2em">
<div>id: x<div>
<div>status: active<div>
<div>id_vendor: Verizon<div>
<div>mobile_number: 555123456<div>
<div>
<div>
Here is what I have so far.. but I can't get the anonymous elements going right:
<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<body style="background-color:#eee;font-size:12pt;font-family:Arial;">
<xsl:for-each select="table/row">
<div style="background-color:#eee;padding:.5em">
<xsl:for-each select="/*">
<div style="background-color:#E3CA87;padding:.2em;border: 1px solid black">
<xsl:for-each select="./*">
<div style="background-color:#F1E2BB;padding:.2em">
<xsl:value-of select ="name(./*)"/>:
<xsl:value-of select="*" />
</div>
</xsl:for-each>
</div>
</xsl:for-each>
</div>
</xsl:for-each>
</body>
</html>
Close... but it was actually a little differnt.. just figured it out for anyone that want's to know:
<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<body style="background-color:#eee;font-size:12pt;font-family:Arial;">
<xsl:for-each select="table/row">
<div style="background-color:#eee;padding:.5em">
<xsl:for-each select=".">
<div style="background-color:#E3CA87;padding:.2em;border: 1px solid black">
<xsl:for-each select=".">
<xsl:for-each select="./*">
<div style="background-color:#F1E2BB;padding:.2em">
<xsl:value-of select="concat(local-name(.), ': ', .)" />
</div>
</xsl:for-each>
</xsl:for-each>
</div>
</xsl:for-each>
</div>
</xsl:for-each>
</body>
</html>
Use relative paths e.g. <xsl:for-each select="*"> instead of <xsl:for-each select="/*">. Then I think you want to drop the inner for-each and simply replace it with
<div style="background-color:#F1E2BB;padding:.2em">
<xsl:value-of select="concat(local-name(), ': ', .)" />
</div>

How to include template in all pages

i want to include design in all pages, for this i had set a master page layout, it works perfect.Then i want to add some portions to each page of my site,i am also done this by creating a utility page, but problem is repeats all contents. here is my code
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/data">
<h1><xsl:value-of select="$page-title"/></h1>
<ul><xsl:apply-templates select="/categories/entry"/></ul>
</xsl:template>
<xsl:template match="categories/entry">
<div class="left_wrap">
<div class="large_video"><img src="{$workspace}/images/video.jpg" style="border:#393939 solid 1px;" /></div>
<div class="title1">Categories<img src="{$workspace}/images/arrow.jpg" /></div>
<div style="float:left; width:680px;">
<div class="category_block">
<div class="category_title"><xsl:value-of select="title"/></div>
<div class="category_image"><img src="{$workspace}/images/politics.jpg" /></div>
<div class="category_info"><xsl:value-of select="description"/></div>
<div class="date"><img src="{$workspace}/images/time.png" style="float:left;"/><p style="float:left; width:120px; margin-left:6px;">2 days ago</p></div>
</div><!--category_block END-->
</div>
</div>
</xsl:template>
</xsl:stylesheet>
I want to repeat only the "class='category_block'" div ,but i need all other in this page. How is it possible in symphony?. Also i want to limit category description character count to 100 character in my home page, how can i limit it.
Firstly, is your <div class="left-wrap"> meant to be repeated for each category entry, or should it contain all categories? I'm going to assume the latter, as that seems to make most sense given what I'm seeing here.
If that's so, your data template should look like this:
<xsl:template match="data">
<h1><xsl:value-of select="$page-title"/></h1>
<xsl:apply-templates select="categories"/>
</xsl:template>
That will match the categories node and allow you to set up the container for all your category entries:
<xsl:template match="categories">
<div class="left_wrap">
<div class="large_video">
<img src="{$workspace}/images/video.jpg" style="border:#393939 solid 1px;" />
</div>
<div class="title1">Categories<img src="{$workspace}/images/arrow.jpg" /></div>
<div style="float:left; width:680px;">
<ul>
<!-- This will be your repeating block -->
<xsl:apply-templates select="entry"/>
<ul>
</div>
</div>
</xsl:template>
Now, when you've got more than one entry, whatever's in the entry template below will be repeated for each:
<xsl:template match="categories/entry">
<li>
<div class="category_block">
<div class="category_title">
<xsl:value-of select="title"/>
</div>
<div class="category_image">
<img src="{$workspace}/images/politics.jpg" />
</div>
<div class="category_info">
<xsl:value-of select="description"/>
</div>
<div class="date">
<img src="{$workspace}/images/time.png" style="float:left;"/>
<p style="float:left; width:120px; margin-left:6px;">2 days ago</p>
</div>
</div><!--category_block END-->
</li>
</xsl:template>
Obviously, I've had to make some assumptions here, so let me know if this isn't exactly what you're after. Also, bear in mind that with XSLT there's always more than one way to do something.
Finally, you should consider cleaning up your markup a bit. Lots of extraneous, non-semantic divs and so on. Your <div class="title1"> should probably be a heading, for instance. Ditto the <div class="category_title">.
To answer your last question about truncating the description, try this utility from the Symphony site.

Display XML on an ASP.NET page

I have a string containing XML document using LinqtoXML
What is the best way of displaying it on an asp.net page as is.
I would have liked to do it the way Dennis has mentioned (using an <asp:Xml> control). But that necessitates the use of an XSL stylesheet to format the XML. The Xml control does not allow an HTML encoded string to be passed as the DocumentContent property and does not expose any method to encode the content.
As I have mentioned in the comments to Dennis' post, the defaultss.xsl contained within msxml.dll is not available publicly (and is not XSL 1.0 compliant). However, a public converted version was posted here: http://www.dpawson.co.uk/xsl/sect2/microsoft.html#d7615e227. I have tested it and it works, though a bit buggy.
Therefore, I think the simplest way would be to use an <asp:Literal> control to output pre-encoded XML to the page. The following sample demonstrates this method:
<%# Page Language="C#" %>
<%# Import Namespace="System.IO" %>
<%# Import Namespace="System.Xml" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string theXML = Server.HtmlEncode(File.ReadAllText(Server.MapPath("~/XML/myxmlfile.xml")));
lit1.Text = theXML;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<pre>
<asp:Literal ID="lit1" runat="server" />
</pre>
</div>
</form>
</body>
</html>
Use the asp:Xml control together with this Default Stylesheet as the TransformSource. This will give you a look very similar to what you see when you open an xml file in Internet Explorer complete with syntax highlighting and coloration.
I removed the expanding/collapsing node functionality because I couldn't get that to work. This file is xsl 1.0 and works great without bugs for my purposes.
To avoid any future problems with my linked file, here are the contents:
<!--
|
| XSLT REC Compliant Version of IE5 Default Stylesheet
|
| Original version by Jonathan Marsh (jmarsh#xxxxxxxxxxxxx)
| http://msdn.microsoft.com/xml/samples/defaultss/defaultss.xsl
|
| Conversion to XSLT 1.0 REC Syntax by Steve Muench (smuench#xxxxxxxxxx)
|
+-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="no" method="html"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="processing-instruction()">
<DIV class="e">
<SPAN class="b">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">nbsp</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN class="m">
<xsl:text><?</xsl:text>
</SPAN>
<SPAN class="pi">
<xsl:value-of select="name(.)"/>
<xsl:value-of select="."/>
</SPAN>
<SPAN class="m">
<xsl:text>?></xsl:text>
</SPAN>
</DIV>
</xsl:template>
<xsl:template match="processing-instruction('xml')">
<DIV class="e">
<SPAN class="b">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">nbsp</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN class="m">
<xsl:text><?</xsl:text>
</SPAN>
<SPAN class="pi">
<xsl:text>xml </xsl:text>
<xsl:for-each select="#*">
<xsl:value-of select="name(.)"/>
<xsl:text>="</xsl:text>
<xsl:value-of select="."/>
<xsl:text>" </xsl:text>
</xsl:for-each>
</SPAN>
<SPAN class="m">
<xsl:text>?></xsl:text>
</SPAN>
</DIV>
</xsl:template>
<xsl:template match="#*">
<SPAN>
<xsl:attribute name="class"><xsl:if test="xsl:*/#*"><xsl:text>x</xsl:text></xsl:if><xsl:text>t</xsl:text></xsl:attribute>
<xsl:value-of select="name(.)"/>
</SPAN>
<SPAN class="m">="</SPAN>
<B>
<xsl:value-of select="."/>
</B>
<SPAN class="m">"</SPAN>
</xsl:template>
<xsl:template match="text()">
<DIV class="e">
<SPAN class="b"> </SPAN>
<SPAN class="tx">
<xsl:value-of select="."/>
</SPAN>
</DIV>
</xsl:template>
<xsl:template match="comment()">
<DIV class="k">
<SPAN>
<!--<A STYLE="visibility:hidden" class="b" onclick="return false" onfocus="h()">-</A>-->
<SPAN class="m">
<xsl:text><!--</xsl:text>
</SPAN>
</SPAN>
<SPAN class="ci" id="clean">
<PRE>
<xsl:value-of select="."/>
</PRE>
</SPAN>
<SPAN class="b">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">nbsp</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN class="m">
<xsl:text>--></xsl:text>
</SPAN>
<SCRIPT>f(clean);</SCRIPT>
</DIV>
</xsl:template>
<xsl:template match="*">
<DIV class="e">
<DIV STYLE="margin-left:1em;text-indent:-2em">
<SPAN class="b">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">nbsp</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN class="m"><</SPAN>
<SPAN>
<xsl:attribute name="class"><xsl:if test="xsl:*"><xsl:text>x</xsl:text></xsl:if><xsl:text>t</xsl:text></xsl:attribute>
<xsl:value-of select="name(.)"/>
<xsl:if test="#*">
<xsl:text> </xsl:text>
</xsl:if>
</SPAN>
<xsl:apply-templates select="#*"/>
<SPAN class="m">
<xsl:text>/></xsl:text>
</SPAN>
</DIV>
</DIV>
</xsl:template>
<xsl:template match="*[node()]">
<DIV class="e">
<DIV class="c">
<!--<A class="b" href="#" onclick="return false" onfocus="h()">-</A>-->
<SPAN class="m"><</SPAN>
<SPAN>
<xsl:attribute name="class"><xsl:if test="xsl:*"><xsl:text>x</xsl:text></xsl:if><xsl:text>t</xsl:text></xsl:attribute>
<xsl:value-of select="name(.)"/>
<xsl:if test="#*">
<xsl:text> </xsl:text>
</xsl:if>
</SPAN>
<xsl:apply-templates select="#*"/>
<SPAN class="m">
<xsl:text>></xsl:text>
</SPAN>
</DIV>
<DIV>
<xsl:apply-templates/>
<DIV>
<SPAN class="b">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">nbsp</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN class="m">
<xsl:text></</xsl:text>
</SPAN>
<SPAN>
<xsl:attribute name="class"><xsl:if test="xsl:*"><xsl:text>x</xsl:text></xsl:if><xsl:text>t</xsl:text></xsl:attribute>
<xsl:value-of select="name(.)"/>
</SPAN>
<SPAN class="m">
<xsl:text>></xsl:text>
</SPAN>
</DIV>
</DIV>
</DIV>
</xsl:template>
<xsl:template match="*[text() and not (comment() or processing-instruction())]">
<DIV class="e">
<DIV STYLE="margin-left:1em;text-indent:-2em">
<SPAN class="b">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">nbsp</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN class="m">
<xsl:text><</xsl:text>
</SPAN>
<SPAN>
<xsl:attribute name="class"><xsl:if test="xsl:*"><xsl:text>x</xsl:text></xsl:if><xsl:text>t</xsl:text></xsl:attribute>
<xsl:value-of select="name(.)"/>
<xsl:if test="#*">
<xsl:text> </xsl:text>
</xsl:if>
</SPAN>
<xsl:apply-templates select="#*"/>
<SPAN class="m">
<xsl:text>></xsl:text>
</SPAN>
<SPAN class="tx">
<xsl:value-of select="."/>
</SPAN>
<SPAN class="m"></</SPAN>
<SPAN>
<xsl:attribute name="class"><xsl:if test="xsl:*"><xsl:text>x</xsl:text></xsl:if><xsl:text>t</xsl:text></xsl:attribute>
<xsl:value-of select="name(.)"/>
</SPAN>
<SPAN class="m">
<xsl:text>></xsl:text>
</SPAN>
</DIV>
</DIV>
</xsl:template>
<xsl:template match="*[*]" priority="20">
<DIV class="e">
<DIV STYLE="margin-left:1em;text-indent:-2em" class="c">
<!--<A class="b" href="#" onclick="return false" onfocus="h()">-</A>-->
<SPAN class="m"><</SPAN>
<SPAN>
<xsl:attribute name="class"><xsl:if test="xsl:*"><xsl:text>x</xsl:text></xsl:if><xsl:text>t</xsl:text></xsl:attribute>
<xsl:value-of select="name(.)"/>
<xsl:if test="#*">
<xsl:text> </xsl:text>
</xsl:if>
</SPAN>
<xsl:apply-templates select="#*"/>
<SPAN class="m">
<xsl:text>></xsl:text>
</SPAN>
</DIV>
<DIV>
<xsl:apply-templates/>
<DIV>
<SPAN class="b">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">nbsp</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN class="m">
<xsl:text></</xsl:text>
</SPAN>
<SPAN>
<xsl:attribute name="class"><xsl:if test="xsl:*"><xsl:text>x</xsl:text></xsl:if><xsl:text>t</xsl:text></xsl:attribute>
<xsl:value-of select="name(.)"/>
</SPAN>
<SPAN class="m">
<xsl:text>></xsl:text>
</SPAN>
</DIV>
</DIV>
</DIV>
</xsl:template>
<xsl:template name="entity-ref">
<xsl:param name="name"/>
<xsl:text disable-output-escaping="yes">&</xsl:text>
<xsl:value-of select="$name"/>
<xsl:text>;</xsl:text>
</xsl:template>
</xsl:stylesheet>
c# solution
Thats worked for me:
string encodedXml = String.Format("<pre>{0}</pre>", HttpUtility.HtmlEncode(xmlStr));
testLb.Text = encodedXml;
Displaying XML Formatted Code Without Using an XML Control
Here is a VB.NET solution:
Public Shared Function FormatXml(ByVal xmlDoc As XmlDocument) As String
Dim sb As New StringBuilder()
`'We will use stringWriter to push the formated xml into our StringBuilder sb.`
Using stringWriter As New StringWriter(sb)
`'We will use the Formatting of our xmlTextWriter to provide our indentation.`
Using xmlTextWriter As New XmlTextWriter(stringWriter)
xmlTextWriter.Formatting = Formatting.Indented
xmlDoc.WriteTo(xmlTextWriter)
End Using
End Using
Return sb.ToString()
End Function
Use this Function to set the Text of a multiline TextBox or a Label:
XmlViewTextBox.Text = FormatXml(xmlDoc)
If you are trying to use an XML string, convert it to an XmlDocument first:
Dim xmlDoc As New XmlDocument
xmlDoc.LoadXml(xmlString)
This solution will render the XML code on the HTML page while preserving line-breaks and indents.
HTML PRE tags in a div, and just echo out the string properly escaped?
Div gives you alignment / formatting, while the PRE should preserve whitespace

Resources