Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Export xml to html or xml viewer
12 replies. Latest Post by anilgv on November 13, 2009.
(0)
thomasgoes
Member
31 points
40 Posts
08-07-2008 9:17 PM |
Is there a way to have xml viewer in Silverlight? If we Bind XML data to TextBox ..its not same as XML Viewer.
Can XML data be exported to HTML browser window within Silverlight using Iframe?
sladapter
All-Star
17445 points
3,173 Posts
08-07-2008 11:32 PM |
I don't think Silverlight has a built-in control to display XML data. Using Iframe is a way to go.
But that Iframe is still a HTML IFrame, not a Iframe inside Sivlerlight. That Iframe can display on top of the Silverlight control if you give it correct position.
You can put a IFrame tag on your TextPage.HTML/ASPX page.
<IFRAme id="MyIFrame" style="position:absolute; width:someWidth; height:someHeight; visibility:hidden" />
From your silverlight code you can access to the IFrame and set property:
HtmlElement m = HtmlPage.Document.GetElementById("MyIFrame"); if (m != null) {
m.SetAttribute("src", "URL of your xml file") // If you have a xml file. m.SetStyleAttribute("left", x); m.SetStyleAttribute("top", y); m.SetStyleAttribute("visibility", "visible"); }
If the Xml is not from a file, but from a xml string data in your Silverlight app, you can still write out that xml to the Iframe just like you do in BLOCKED SCRIPT
In order for your IFrame to display on top of the Silverlight control, you need to set windowless=true in your Silverlight control Tag in the html page:
<object data="data:application/x-silverlight," type="application/x-silverlight-2-b1" width="100%" height="100%">
<param name="windowless" value="true" />
<param ../>
Lipoly
2 points
1 Posts
09-03-2009 5:46 PM |
Could you elaborate on the "BLOCKED SCRIPT" technique to display xml string data from the Silverlight app in the iFrame?
09-03-2009 5:55 PM |
"BLOCKED SCRIPT" is "JavaScript" . I typed "JavaScript", but somehow, it changed to "BLOCKED SCRIPT" when it was posted.
anilgv
10 points
12 Posts
11-04-2009 1:54 PM |
Can you please explain as how to read xml string data in C# using JavaScript?
11-04-2009 3:01 PM |
anilgv:Can you please explain as how to read xml string data in C# using JavaScript?
Do you mean to write the xml string data to the IFrame?
You can use a JavaScript to write anything to the IFrame:
Put the following JavaScript function in your testpage.html
function WriteToIFrame(xml) { var f = document.getElementById('myframe'); f = (f.contentWindow) ? f.contentWindow : (f.contentDocument.document) ? f.contentDocument.document : f.contentDocument; f.document.open(); f.document.write(xml); f.document.close(); }
Then in the Silverlight code you can call
HtmlPage.Window.Invoke("WriteToIFrame", YourXML); // Pass the XML to the JavaScript Function
11-04-2009 4:39 PM |
Thank you, that works fine in showing the XML data from the string into IFrame.
However, this XML is not in pretty-print format. How would I achieve pretty-print in IFrame?
11-04-2009 5:07 PM |
What do you mean by "pretty print format"?
11-04-2009 5:17 PM |
For example,
<root><node/></root>
should look like
<root> <node/></root>
11-04-2009 8:32 PM |
I found the following should work. But only works on Firefox, not on IE. On IE, xml won't even display.
BLOCKED SCRIPT
function WriteToIFrame(xml) { var f = document.getElementById('myFrame'); if (f == null) return; f = (f.contentWindow) ? f.contentWindow : (f.contentDocument.document) ? f.contentDocument.document : f.contentDocument; f.document.open("text/xml"); f.document.write(xml); f.document.close(); }
Server Side:
XDocument xdoc = XDocument.Parse("<root><node/></root>"); HtmlPage.Window.Invoke("WriteToIFrame", xdoc.Document.ToString());
11-05-2009 1:43 PM |
As you mentioned it doesn't work on IE.
I am looking for a way to implement this on IE. Any thoughts??
Thank you.
11-05-2009 1:51 PM |
I haven't found out a solution for IE. But this is not Silverlight problem, really a problem on how to output xml string to a IFrame in IE. I'll keep looking. I remember IE was supposed to support xml data island (with <xml/> tag). But I didn't seem to make it work either. I don't know if IE 8 took that capability away or not. I'm on IE8.
11-13-2009 3:19 PM |
I am able to display formatted (consistent indentation) XML string in TextBox instead of using IFrame and JavaScript.
This approach can be followed for the XML data that comes in string form.
Steps:
1. Pass the XML string to the C Sharp code that returns the XML in formatted structure.
// Attractively format the XML with consistent indentation.
public static String PrettyPrint(String XML) { String Result = "";
// Create an XmlWriterSettings object with the correct options. XmlWriterSettings writerSettings = new XmlWriterSettings(); writerSettings.Indent = true; writerSettings.OmitXmlDeclaration = true;
// Create the XmlWriter object and write some content MemoryStream memoryStream = new MemoryStream(); XmlWriter writer = XmlWriter.Create(memoryStream, writerSettings); try {
// Create an XML document and write the XML into a formatting XmlWriter XDocument doc = XDocument.Parse(XML); doc.WriteTo(writer); writer.Flush(); memoryStream.Flush();
// Have to rewind the MemoryStream in order to read its contents. memoryStream.Position = 0;
// Read MemoryStream contents into a StreamReader. StreamReader reader = new StreamReader(memoryStream);
// Extract the text from the StreamReader. String formattedXML = reader.ReadToEnd();
Result = formattedXML; } catch (XmlException) { //in case of any exception return the input string return XML; } finally { if (writer != null) writer.Close();
if (memoryStream != null) memoryStream.Close(); } return Result; }
2. Set the formatted XML String to the TextBox.Text.