Displaying an XML code in the browser
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 dif开发者_开发百科ferent approach.
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!
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()
You could get the InnerXml
from the root node and replace <
with <
and >
with >
in your page.
Something simple to start with:
Response.Write(doc.InnerXml.Replace("<", "<").Replace(">", ">"));
Once you read the contents of the file into a string, you can do something like this
<pre>
<%= myXmlString.Replace("<", "<").Replace(">", ">").Replace("\"", """) %>
</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 ;-)
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("<", "<").Replace(">", ">"));
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.
精彩评论