views:

140

answers:

5

This may be simple, although i'm having some trouble finding a solution.

When you look a .xml file in your folder, you could double click it so your main browser will display the code content of it.

I have the complete path of the xml, and I'd like to create a link in aspx (with either c# or vb.net) that redirects to the XML in the browser.

response.redirect("<path>")

--- update

it redirects to:

http://img37.imageshack.us/img37/7227/89684913.jpg

when i put my mouse over "here" there's the localhost port with / in the end (it looks for the path in the localhost)

that's why i made this question.. it has to be a different approach.

A: 

You could get the InnerXml from the root node and replace < with &lt; and > with &gt; in your page.

Something simple to start with:

Response.Write(doc.InnerXml.Replace("<", "&lt;").Replace(">", "&gt;"));
Mark Ursino
same here.. i actually want it idented, colored and with the "collapse" and "expand" functionalities.. i actually want the browser to read it.. not just display the content (that i already do in other way)
MarceloRamires
A: 

Once you read the contents of the file into a string, you can do something like this

<pre>
<%= myXmlString.Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;") %>
</pre>

The "pre" tags will make the browser maintain the file's layout. Yeah, you could do something more sophisticated with regular expressions, but meh, sometimes something simple is all it takes ;-)

Joel Martinez
i actually want it idented, colored and with the "collapse" and "expand" functionalities.. i actually want the browser to read it.. not just display the content
MarceloRamires
A: 

if you want to maintain the structure and whitespace of the xml document, try using the output from LINQ's XDocument. This example shows how to load from a file

public string XmlFile
{
    get
    {
        return Server.MapPath("output.xml");
    }
}

then, in your xml output method:

            string xmlFromFile = string.Empty;

            XmlTextReader reader = null;
            XDocument xmlDoc = null;

            try
            {
                reader = new XmlTextReader(XmlFile);
                xmlDoc = XDocument.Load(reader);
                reader.Close();
            }
            catch
            {
                if(reader != null)
                    reader.Close();
            }

            lblXMLoutput.Text = String.Format("<pre>{0}</pre>",
                xmlDoc.ToString().Replace("<", "&lt;").Replace(">", "&gt;"));

            this.xmlOutput.InnerHtml = xmlDoc.ToString();

and in your form have this code:

<asp:Label ID="lblXMLoutput" runat="server" />
<div id="xmlOutput" runat="server" style="display:none;"></div>

Notice that the xmlOutput div isn't necessary. It's just there so you can check to make sure all of your xml is output correctly.

Jim Schubert
as far as your comment about coloring and collapse/expand, you can use manoli's c# formatter (http://www.manoli.net/csharpformat/) He provides the code and supports XML coloring. That shouldn't be too hard to implement. The collapse/expand should be interesting.
Jim Schubert
well, thank you very much, but i was looking for something more like FILE.DOUBLECLICK("XMLFILE.XML") hahah, joking.. i don't want to simulate it, and i'd like to learn it also for sometime i use it with other things (like mp3 or mp4 that the filefox and chrome have their ways of opening it) or any other thing i might need, i'm not really in need of displaying the code (but if it works the way i describe, it's gonna be added to the release, indeed, because it would be useful) it's more about learning.
MarceloRamires
+1  A: 

As i said in the question, it would be easy:

Process.Start(<path>)

It worked perfectly, thank to all of you guys who have helped me!

MarceloRamires
+1  A: 
 Dim xmlDoc As New XmlDocument
    xmlDoc.Load(Server.MapPath("QuinnDirectRequest.xml"))
    Response.Clear()
    Response.AddHeader("Content-Disposition", "inline; filename=file.xml")

    If Request.QueryString("type") = "s" Then
        Response.ContentType = "text/xml"
        Response.Write(xmlDoc.InnerXml)
    Else
        Response.ContentType = "application/xml"
        Response.Write(xmlDoc.InnerXml)
    End If

    Response.Flush()
    Response.End()
plz check this example