Skip to content

XamlReader2 – helpful wrapper to XamlReader

March 26, 2007

I think XamlReader needs some more methods to make a few tasks easier.


Things I’d like to have be easier:

1) I hate having to cast the root that I am expecting:

RootType root = (RootType)XamlReader.Load(s);

I’d prefer:

RootType root = XamlReader<RootType>.Load(s);

2) I hate having to only pass a stream in

I’d like to be able to pass a filepath, FileInfo or a string:


            string strXaml = "<String xmlns=\"clr-namespace:System;assembly=mscorlib\">Hello world</String>";
            string str = XamlReader2<string>.Load(strXaml);


        FileInfo dataFile = new FileInfo("c:\\temp\\topics.xml");
        if (dataFile.Exists)
            Topics = XamlReader2<TopicCollection>.Load(dataFile);
            Topics = new TopicCollection();




Prototype of XamlReader2: (could have major bugs – please review before using…)

[Update: 3/27] Updated the Load(string) override implementation to use StringReader/XmlReader instead of MemoryStream, etc…  Thanks for the comments!

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Windows.Markup;

namespace XamlHelper
    //This code is just a prototype of some helpers
    //to make XamlReading more convenient...let me know
    //what you think.
    public static class XamlReader2<T> where T : class
        public static T Load(Stream stream)
            object o = System.Windows.Markup.XamlReader.Load(stream);
            return CheckRootType(o);

        public static T Load(XmlReader xmlReader)
            object o = XamlReader.Load(xmlReader);
            return (CheckRootType(o));

        private static T CheckRootType(object o)
            T root = o as T;
            if (root != null)
                return root;
                throw new Exception("Expected <" + typeof(T).Name + "> as root tag.");

        public static T Load(FileInfo fileInfo)
            if (!fileInfo.Exists)
                throw new FileNotFoundException(fileInfo.FullName + " doesn't exist.");
            StreamReader sr = new StreamReader(fileInfo.FullName);
            return XamlReader2<T>.Load(sr.BaseStream);

        public static T Load(string xamlContainingString)
            StringReader stringReader = new StringReader(xamlContainingString);
            XmlReader xmlReader = XmlTextReader.Create(stringReader, new XmlReaderSettings());
            return Load(xmlReader);


From → XAML (non-UI)

  1. Douglas Stockwell permalink

    Here\’s a simplification on the Load(string) ->
    XamlReader.Load(new XmlTextReader(new StringReader(xamlString)));

  2. Rob permalink

    You are suggesting I replace the MemoryStream code with a TextReader and StringReader.
    I agree your code is simpler, but as an implementation detail of the Load method, I don\’t care how ugly that code is.

    For the real version of this override, we should compare the perf costs of the two methods.
    My big problem is that we (v3 of wpf) haven\’t made it simple for somebody with a string…while yours is easier than MemoryStream (!), it isn\’t simple enough to replace the need for a string override of Load.
    Thanks, Rob

  3. Simon permalink

    The ASCIIEncoding stuff seems wrong to me, I\’d search-replace ASCIIEncoding with just Encoding. (Functionally they are probably the same in Load(string xamlContainingString), but the latter looks better IMO). ASCIIEncoding as the parameter type in
    public static T Load(string xamlContainingString, ASCIIEncoding asciiEncoding) its plain wrong as ASCIIEncoding is a subclass of Encoding, and if you need to specify an encoding you most certainly do *not* need to specify ascii.
    As XML defaults to utf-8 unless you tell it othervise one could argue that utf-8 should be the default encoding, on the other hand as string literals are "unicode" i.e. utf-16 one could argue that should be the default (as you have it..)

  4. Douglas Stockwell permalink

    Rob, I agree we definately need a Load(string). But for now I think using the StringReader almost certainly has better perf.
    The Load(string,encoding) isn\’t really necessary. Since we start out with a unicode CLR string, converting through some other encoding like ASCII here is only going to result in a loss of information.

  5. Rob permalink

    Thanks for the feedback on the implementation of the Load(string) override.
    I\’ve updated the post to have the improved technique that you recommended.
    I\’m not sure where I ran into the AsciiEncoding/MemortStream technique…likely searching the web for "string" to "stream".  XmlTextReader feels better!
    Thanks, Rob

  6. Jens Peter Kleinau permalink

    Hi Rob,
    I did some very exiting work with the XAML Reader for the Deutsche Telecom and I can tell: just reading XAML is not enough. The complecations begin with merging ResourceDictionary.MergedDictionaries, relative or absolute Image Paths and exchanging unwanted classes (e.g. root class). But if you are finished with loading XAML, the problems of binding dynamic Data and events to the freshly loaded GUI are waiting for you;-)
    IMHO XamlReader needs a lot of improvement, if you want more than an additional loaded smily on your GUI.
    I made some suggestings to address this problems in my blog – but alas its in german.

  7. Unknown permalink

    yoipkj09 xulna909 peons4hire peons4hire peons4hire peons4hire wow gold world of warcraft gold wow gold 加拿大留学 留学加拿大 自吸泵 自吸泵 转子泵 转子泵 循环泵 循环泵 泵 泵 齿轮输油泵 齿轮输油泵 螺杆泵 螺杆泵 化工泵 化工泵 进口轴承 进口轴承 中高年 転職 中高年 転職 賃貸 賃貸 FX FX 阀门 阀门 切换器 切换器 上海展览公司 上海展览公司 阳光房 阳光房 上海阳光房 上海阳光房 wow gold wow gold wow gold world of warcraft power leveling world of warcraft power leveling world of warcraft power leveling world of warcraft power leveling WoW power leveling WoW power leveling WoW power leveling WoW power leveling thsale thsale thsale

  8. Unknown permalink

    japan, real estate, buy, rent, 部屋探し, 不動産,住宅,工場,倉庫,売地,相場, 賃貸,売買,駐車場,地域,路線,家賃,情報,部屋探し,借りる,買う, 賃貸物件,売買物件,一戸建て,マンション,アパート,店舗,事務所,土地,売買ゲット東京都 不動産 不動産 東京,東京 不動産,不動産 東京都,賃貸,不動産投資,東京 賃貸,賃貸 東京,新築マンション,賃貸マンション,住宅,土地,戸建,店舗,事務所,新築分譲マンション,不動産GET,不動産ゲット

  9. Unknown permalink

    有料老人ホーム 副業 清掃 データ復旧 アダルトグッズ パテックフィリップ  マンションリフォーム 住宅リフォーム   マッスルトレーナー  カラーコーディネーター  痔   アメリカ ビザ マンスリーマンション 東京   アンチエイジング 化粧品 永代供養 特許事務所 会社設立 永代供養 納骨堂 ウォーキングシューズ  ウォーキング  サウナスーツ イメージコンサルタント  美容学校  ペット 求人永代供養 広告業界  就職ナビローン

  10. Unknown permalink

    カラコン  メル友 ウィークリーマンション 自動車ガラス  不動産
     葬儀 東京 基礎化粧品ウィークリーマンション 治験 結婚指輪 太陽光発電
    結婚式 演出
    吉原 ソープ 債務整理
    法律事務所 求人 看護師  キャッシング
    転職 バイアグラ
    ショッピング枠 現金化
    価格   賃貸 
     介護 看護師   会社設立

  11. John permalink

    @Rob Relyea@"Thoughts?"Your approach could also be useful for traversing a XAML hierarchy to search for an element by name.What do you think of removing the boilerplate from FrameworkElement.FindName()?

  12. John permalink

    @Rob Relyea@"Thoughts?"Your approach could also be useful for traversing a XAML hierarchy to search for an element by name.What do you think of removing the boilerplate from FrameworkElement.FindName()?

  13. Unknown permalink

    収益物件 プレジデント 札幌 函館 岐阜 青森県 インテリアコーディネータ リフォーム 東京 リフォーム 大阪 不動産 査定 不動産 買取 不動産 売買 不動産 鑑定 不動産 売却  広島 不動産 岩手県 投資 秋田県 山梨県 福井県 石川県 福島県 宮城県 長野県 岐阜県 群馬県 奈良県 栃木県

  14. jigar permalink

    Thanks for this information. But i have one doubt.
    How to load xaml content ? consider below scenario.
    I have this xaml content.

    Dim xamlContent = _
    BasedOn="{StaticResource {x:Type TextBlock}}"
    TargetType="{x:Type TextBlock}">
    <Setter Property="Margin"
    BasedOn="{StaticResource {x:Type TextBox}}"
    TargetType="{x:Type TextBox}">
    <Setter Property="Margin"
    <RowDefinition Height="Auto"></RowDefinition>
    <RowDefinition Height="Auto"></RowDefinition>
    <ColumnDefinition Width="0.20*"/>
    <ColumnDefinition Width="0.80*"/>
    <TextBlock Name="tbkName"
    <TextBox Name="txtName"
    <TextBlock Name="tbkAddress"
    <TextBox Name="txtAddress"
    How do I load this into stackpanel or in grid ?

  15. Unknown permalink

    Welcome to enter (wow gold) and (wow power leveling) trading site, (wow gold) are cheap, (wow power leveling) credibility Very good! Quickly into the next single! Key words directly to the website click on transactions! -144422520534157

  16. Unknown permalink

    RMT 株式投資 結婚式 演出 太陽光発電 転職 看護師 釣具 会社設立 データ復旧 フローリング データ復元 投資 横浜 賃貸 ショッピング枠 現金化 データ復旧 テレマーケティング  パソコン自作
    アフィリエイト ブログアフィリエイト

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: