Solved

Trying to indent XML data within a div

Posted on 2008-06-25
12
619 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 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