简体   繁体   中英

How to open a dynamically created a xml data in Internet Explorer?

I read XML file:

string strFileName = @"D:\BigFile.xml";
XmlDocument xDoc = new XmlDocument();
xDoc.Load(strFileName);

I inserted a xml node into this xml file:

XmlNode xElt = xDoc.SelectSingleNode("pagina");
XmlElement xNewChild = xDoc.CreateElement("postPaginaXMLXMLXMLXMLXMLXMLXMLXML");
xDoc.DocumentElement.InsertBefore(xNewChild, xElt);

However, I couldn't open this xDoc in Internet Explorer. I can open a created file:

Process proc = new Process();
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.FileName = strFileName;
proc.Start();

But, this code opens a real xml file by address(strFileName). How to open a dynamically created xml file in Internet Explorer without saving/creating this file on HDD?

This is an answer to a similar question.... The code essentially downloads an XML file, transforms the XML to HTML by applying xsl stylesheet (Layout\\Main.xsl) (which you might not want) then displys the resulting HTML in the WebBrowser control.... it's VB.NET but is simple enough to convert to C#


You can download the XML using WebClient..... something like this (replace url with your actual url)

Dim xml = New WebClient().DownloadString(url)

Now if you want to display the XML in the WebBrowser you can do all that using MemoryStream and an *.xsl file, no need to save the XML to the HD at all....

Using the WebBrowser1.DocumentStream property you can create HTML MemoryStream from applying an xsl Stylesheet to your XML. Here is an example function....

Friend Function CreateHTMLStream(xml As String, xslPath As String) As MemoryStream
Dim HTMLTransformStream As New MemoryStream
Dim HTMLTransformWriter As New XmlTextWriter(HTMLTransformStream, System.Text.Encoding.ASCII)

'Dim XMLTransformStream As New MemoryStream
Dim XMLTransformReader As New XmlTextReader(New StringReader(xml))

Dim xsltrans = New XslCompiledTransform()
xsltrans.Load(xslPath)
xsltrans.Transform(XMLTransformReader, Nothing, HTMLTransformWriter)
HTMLTransformStream.Position = 0
Return HTMLTransformStream
End Function

that function takes ans XML string (downloaded with the WebClient) and the path to you XSL StyleSheet, it transforms and returns the HTML as a MemoryStream.... SO you would call that function like so.....

WebBrowser1.DocumentStream = CreateHTMLStream( WebClient().DownloadString(url), "Layout\\Main.xsl")

This might give you some ideas..... I have not tested the code so you might have to play around with it....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM