Solved

Trying to indent XML data within a div

Posted on 2008-06-25
12
622 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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