?
Solved

Trying to indent XML data within a div

Posted on 2008-06-25
12
Medium Priority
?
624 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

Question has a verified solution.

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

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month11 days, 17 hours left to enroll

752 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