Skip to main content

Microsoft Silverlight

Answered Question Export xml to html or xml viewerRSS Feed

(0)

thomasgoes
thomasgoes

Member

Member

31 points

40 Posts

Export xml to html or xml viewer

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
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Answered Question

Re: Export xml to html or xml viewer

 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 ../>


sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

Lipoly
Lipoly

Member

Member

2 points

1 Posts

Re: Export xml to html or xml viewer

 Could you elaborate on the "BLOCKED SCRIPT" technique to display xml string data from the Silverlight app in the iFrame?

sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Re: Export xml to html or xml viewer

"BLOCKED SCRIPT" is "JavaScript" .  I typed "JavaScript", but somehow, it changed to "BLOCKED SCRIPT" when it was posted.

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

anilgv
anilgv

Member

Member

10 points

12 Posts

Re: Re: Export xml to html or xml viewer

Can you please explain as how to read xml string data in C# using JavaScript?

sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Re: Re: Export xml to html or xml viewer

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

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

anilgv
anilgv

Member

Member

10 points

12 Posts

Re: Re: Re: Export xml to html or xml viewer

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?

sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Re: Re: Re: Export xml to html or xml viewer

 What do you mean by "pretty print format"?

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

anilgv
anilgv

Member

Member

10 points

12 Posts

Re: Re: Re: Re: Export xml to html or xml viewer

For example,

<root><node/></root>

should look like

<root>
 
<node/>
</root>

sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Re: Re: Re: Re: Export xml to html or xml viewer

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());

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

anilgv
anilgv

Member

Member

10 points

12 Posts

Re: Re: Re: Re: Re: Export xml to html or xml viewer

As you mentioned it doesn't work on IE.

I am looking for a way to implement this on IE. Any thoughts??

Thank you.

sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Re: Re: Re: Re: Re: Export xml to html or xml viewer

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.

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

anilgv
anilgv

Member

Member

10 points

12 Posts

Re: Re: Re: Re: Re: Re: Export xml to html or xml viewer

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.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities