# Reading files in Dynamic Silverlight

**URL:** <https://www.ruby-forum.com/t/reading-files-in-dynamic-silverlight/133980>\
**Category:** IronRuby\
**Created:** [April 6, 2008, 1:01am UTC](https://www.ruby-forum.com/t/reading-files-in-dynamic-silverlight/133980 "2008-04-06T01:01:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![barby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@barby](https://www.ruby-forum.com/u/barby)\
**Post date:** [April 6, 2008, 1:01am UTC](https://www.ruby-forum.com/t/reading-files-in-dynamic-silverlight/133980/1 "2008-04-06T01:01:54Z")

</div>

Hi

I can’t work out how I can read external xaml files from Silverlight  
with  
IronRuby.  
I’ve tried to use System::IO::File and StreamReader but both give me a  
MethodAccessException.

How would I accomplish reading a xaml file in the xap with IronRuby and  
Silverlight.

I would like to create a user control and load that into my main app.

System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream(  
“story\_details\_view.xaml”)

\_root = this.InitializeFromXaml(new  
System.IO.StreamReader(s).ReadToEnd())  
as UserControl;

I would also like to try to read an external xaml file for skinning  
purposes.  
Is that possible at this moment?

Cheers  
Ivan

---

<div class="post-metadata">

**Author:** ![barby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@barby](https://www.ruby-forum.com/u/barby)\
**Post date:** [April 6, 2008, 1:12am UTC](https://www.ruby-forum.com/t/reading-files-in-dynamic-silverlight/133980/2 "2008-04-06T01:12:28Z")

</div>

Ivan Porto C. wrote:

> I would like to create a user control and load that into my main app.  
> purposes.  
> Is that possible at this moment?

In IronPython you just use the ordinary ‘file’ type to open a file that  
is in the ‘xap’ file as if it was a local resource. You may find that  
something similar is available in IronRuby.

Michael F.  
[http://www.ironpythoninaction.com](http://www.ironpythoninaction.com)

---

<div class="post-metadata">

**Author:** ![barby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@barby](https://www.ruby-forum.com/u/barby)\
**Post date:** [April 6, 2008, 3:39am UTC](https://www.ruby-forum.com/t/reading-files-in-dynamic-silverlight/133980/3 "2008-04-06T03:39:16Z")

</div>

Michael F.:

> In IronPython you just use the ordinary ‘file’ type to open a file that  
> is in the ‘xap’ file as if it was a local resource. You may find that  
> something similar is available in IronRuby.

I added this feature to IronPython to support the Peter Norvig spell  
check demo that we used at MIX. I haven’t gotten around to adding that  
functionality to IronRuby yet - it’s on the list of things to do.

Thanks,  
-John

---

<div class="post-metadata">

**Author:** ![barby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@barby](https://www.ruby-forum.com/u/barby)\
**Post date:** [April 6, 2008, 3:42am UTC](https://www.ruby-forum.com/t/reading-files-in-dynamic-silverlight/133980/4 "2008-04-06T03:42:48Z")

</div>

It’s not currently supported in IronRuby file IO, but here’s a  
workaround (I haven’t tried this code in real, so it may not work :).

include Microsoft::Scripting::Silverlight

runtime = DynamicApplication::Environment  
stream =  
runtime.host.platform\_adaptation\_layer.open\_input\_file\_stream(“story\_details\_view.xaml”)

stream is instance of the Stream class.

Tomas

---

<div class="post-metadata">

**Author:** ![barby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@barby](https://www.ruby-forum.com/u/barby)\
**Post date:** [April 6, 2008, 11:17pm UTC](https://www.ruby-forum.com/t/reading-files-in-dynamic-silverlight/133980/5 "2008-04-06T23:17:03Z")

</div>

Ivan Porto C.:

> System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream(“story\_details\_view.xaml”)
> 
> \_root = this.InitializeFromXaml(new  
> System.IO.StreamReader(s).ReadToEnd()) as UserControl;

I’m not sure why the above C# code wouldn’t “just work”. Which method  
call throws MethodAccessException?  
The only thing that looks odd is the GetManifestResourceStream. The way  
I’ve done it is:

s = System.Windows.Application.Current.GetResourceStream(new  
Uri(“story\_details\_view.xaml”, UriKind.Relative));

> I would also like to try to read an external xaml file for skinning  
> purposes.  
> Is that possible at this moment?

Sure, just use the normal Silverlight downloading APIs. Check out the  
WebClient and HttpWebRequest classes.

- John
