Solved

Trying to indent XML data within a div

Posted on 2008-06-25
12
612 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
  • 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now