Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Trying to indent XML data within a div

Posted on 2008-06-25
12
Medium Priority
?
625 Views
Last Modified: 2008-09-29
Hello,

I have a long XML string which I put in a <div> (in a <xmp> tag)  the following way:

<div>
        <xmp><%= xmlStr%></xmp>
</div>

(xmlStr is taken from the server side)
The string looks like XML but it appears as 1 long row with no indentation.
How can I make it look like a XML with indentation?

Thanks...
0
Comment
Question by:Relegence
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
12 Comments
 
LVL 21

Expert Comment

by:naspinski
ID: 21864676
Depending on what your structure is, you could use some .Replace() methods to add in breaks and spaces:

For example, if your structure was

<root>
  <person>
    <age />
  </person>
  <person>
    <age />
  </person>
</root>

You could use:
xmlStr.Replace("&lt;person", "<br />&nbsp;&nbsp;&lt;person:).Replace("<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;age", "&lt;age")

Notice that that adds a <br /> at the beginning of each line, knocking it down one line, and adds how many spaces in relation to what the element is.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 21864688
The browser will not format XML text just because it is XML text.  It thinks it is just a stream of characters--nothing more.  You would have to do some magic to make the browser treat the text as an XML stream.  I don't specifically know of a way to achieve that effect, so let me think about this.
0
 

Author Comment

by:Relegence
ID: 21867880
Thank you both - the first suggestion is not good for me since I don't know the XML format in advance.

Maybe I can achieve what I need in a different way - I have a XML string in the server side and I want to put it within a div and show it as a XML document.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 96

Expert Comment

by:Bob Learned
ID: 21868252
Well, another way would be to use XSLT to transform the XML to HTML so that you can display it in the browser, but generic XSLT is pretty difficult.

Another way would be to use an iframe that points to an .aspx file that adds the XML file to the response stream:

    <iframe src="XmlReader.aspx">
    </iframe>

XmlReader.aspx:

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/xml";
        Response.WriteFile(Server.MapPath("Test.xml"));
        Response.End();
    }
0
 

Author Comment

by:Relegence
ID: 21868404
The second suggestion which sounds better is problematic because I don't have a XML
file. I only have a very long XML string....
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 21868425
Where does the XML come from?
0
 

Author Comment

by:Relegence
ID: 21871990
It comes from a servlet. I call a url with some parameters and get the XML in return
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 21874117
Can you show me how that is done?
0
 

Author Comment

by:Relegence
ID: 21893091
Sorry for the delay..... Here it is:

The 'resp' is a XML string so I try to show it formatted as XML.
I put it in a <div> but it is not formatted...

HttpWebRequest weR = (HttpWebRequest)HttpWebRequest.Create(theURL);
webRes = (HttpWebResponse)weR.GetResponse();
string resp;
using (StreamReader sr = new StreamReader(webRes.GetResponseStream()))
{
   resp = sr.ReadToEnd();
}

Open in new window

0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 21945363
Suggested course of action:


    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/xml";
        string theURL = Request.QueryString["url"];
        HttpWebRequest weR = (HttpWebRequest)HttpWebRequest.Create(theURL);
        using (HttpWebResponse webRes = (HttpWebResponse)weR.GetResponse())
        {
            using (StreamReader sr = new StreamReader(webRes.GetResponseStream()))
            {
               Response.Write(sr.ReadToEnd());
               Response.End();
            }
        }
    }

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question