XamlWriter, MarkupObject How to ?<p>I'm trying to serialize a tree of object in xaml in some designer app.</p> <p>some node of this tree refers to the same reference that i added to a resource dictionnary.</p> <p>XamlWriter won't serialize the reference to the dictionnary resources as a StaticResource + ResourceKey</p> <p>but i understand reading this post :</p> <p><a title="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=286265&amp;SiteID=1" href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=286265&amp;SiteID=1">http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=286265&amp;SiteID=1</a></p> <p>that this should be possible using MarkupObject and modifying the ouput for some node.</p> <p> let say :</p> <div style="font-size:10pt;background:white;color:black;font-family:Courier New"> <p style="margin:0px"> </p> <p style="margin:0px">            <span style="color:teal">Transform3DGroup</span> TG1 = <span style="color:blue">new</span> <span style="color:teal">Transform3DGroup</span>();</p> <p style="margin:0px">            <span style="color:teal">Transform3DGroup</span> TG2 = <span style="color:blue">new</span> <span style="color:teal">Transform3DGroup</span>();</p> <p style="margin:0px"> </p> <p style="margin:0px">            <span style="color:teal">TranslateTransform3D</span> ts2 = <span style="color:blue">new</span> <span style="color:teal">TranslateTransform3D</span>();    </p> <p style="margin:0px"> </p> <p style="margin:0px">            TG1.Children.Add(<span style="color:blue">new</span> <span style="color:teal">TranslateTransform3D</span>());</p> <p style="margin:0px">            TG1.Children.Add(ts2);</p> <p style="margin:0px"> </p> <p style="margin:0px">            TG2.Children.Add(<span style="color:blue">new</span> <span style="color:teal">TranslateTransform3D</span>());</p> <p style="margin:0px">            TG2.Children.Add(ts2);</p> <p style="margin:0px"> </p></div> <p> here's basically the output i'm looking for :</p> <div style="font-size:10pt;background:white;color:black;font-family:Courier New"> <p style="margin:0px"><span style="color:blue">    &lt;</span><span style="color:maroon">Transform3DGroup</span><span style="color:blue"> </span><span style="color:red">x:Name</span><span style="color:blue">=</span>&quot;<span style="color:blue">TG1</span>&quot;<span style="color:blue">&gt;</span></p> <p style="margin:0px"><span style="color:blue">      &lt;</span><span style="color:maroon">TranslateTransform3D</span><span style="color:blue">/&gt;</span></p> <p style="margin:0px"><span style="color:blue">      &lt;</span><span style="color:maroon">StaticResource</span><span style="color:blue"> </span><span style="color:red">ResourceKey</span><span style="color:blue">=</span>&quot;<span style="color:blue">ts2</span>&quot;<span style="color:blue">/&gt;</span></p> <p style="margin:0px"><span style="color:blue">    &lt;/</span><span style="color:maroon">Transform3DGroup</span><span style="color:blue">&gt;</span></p> <p style="margin:0px"> </p> <p style="margin:0px"><span style="color:blue">    &lt;</span><span style="color:maroon">Transform3DGroup</span><span style="color:blue"> </span><span style="color:red">x:Name</span><span style="color:blue">=</span>&quot;<span style="color:blue">TG2</span>&quot;<span style="color:blue">&gt;</span></p> <p style="margin:0px"><span style="color:blue">      &lt;</span><span style="color:maroon">TranslateTransform3D</span><span style="color:blue">/&gt;</span></p> <p style="margin:0px"><span style="color:blue">      &lt;</span><span style="color:maroon">StaticResource</span><span style="color:blue"> </span><span style="color:red">ResourceKey</span><span style="color:blue">=</span>&quot;<span style="color:blue">ts2</span>&quot;<span style="color:blue">/&gt;</span></p> <p style="margin:0px"><span style="color:blue">    &lt;/</span><span style="color:maroon">Transform3DGroup</span><span style="color:blue">&gt;</span></p></div> <p>Could someone provide me some information on how to achieve this ?</p> <p> </p> <p>Thanks</p> <p> </p>© 2009 Microsoft Corporation. All rights reserved.Wed, 11 Nov 2009 22:49:56 Z08aebbf1-0a61-4305-83b2-a0a37bb24002http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#08aebbf1-0a61-4305-83b2-a0a37bb24002http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#08aebbf1-0a61-4305-83b2-a0a37bb24002gumtoohttp://social.msdn.microsoft.com/Profile/en-US/?user=gumtooXamlWriter, MarkupObject How to ?<p>I'm trying to serialize a tree of object in xaml in some designer app.</p> <p>some node of this tree refers to the same reference that i added to a resource dictionnary.</p> <p>XamlWriter won't serialize the reference to the dictionnary resources as a StaticResource + ResourceKey</p> <p>but i understand reading this post :</p> <p><a title="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=286265&amp;SiteID=1" href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=286265&amp;SiteID=1">http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=286265&amp;SiteID=1</a></p> <p>that this should be possible using MarkupObject and modifying the ouput for some node.</p> <p> let say :</p> <div style="font-size:10pt;background:white;color:black;font-family:Courier New"> <p style="margin:0px"> </p> <p style="margin:0px">            <span style="color:teal">Transform3DGroup</span> TG1 = <span style="color:blue">new</span> <span style="color:teal">Transform3DGroup</span>();</p> <p style="margin:0px">            <span style="color:teal">Transform3DGroup</span> TG2 = <span style="color:blue">new</span> <span style="color:teal">Transform3DGroup</span>();</p> <p style="margin:0px"> </p> <p style="margin:0px">            <span style="color:teal">TranslateTransform3D</span> ts2 = <span style="color:blue">new</span> <span style="color:teal">TranslateTransform3D</span>();    </p> <p style="margin:0px"> </p> <p style="margin:0px">            TG1.Children.Add(<span style="color:blue">new</span> <span style="color:teal">TranslateTransform3D</span>());</p> <p style="margin:0px">            TG1.Children.Add(ts2);</p> <p style="margin:0px"> </p> <p style="margin:0px">            TG2.Children.Add(<span style="color:blue">new</span> <span style="color:teal">TranslateTransform3D</span>());</p> <p style="margin:0px">            TG2.Children.Add(ts2);</p> <p style="margin:0px"> </p></div> <p> here's basically the output i'm looking for :</p> <div style="font-size:10pt;background:white;color:black;font-family:Courier New"> <p style="margin:0px"><span style="color:blue">    &lt;</span><span style="color:maroon">Transform3DGroup</span><span style="color:blue"> </span><span style="color:red">x:Name</span><span style="color:blue">=</span>&quot;<span style="color:blue">TG1</span>&quot;<span style="color:blue">&gt;</span></p> <p style="margin:0px"><span style="color:blue">      &lt;</span><span style="color:maroon">TranslateTransform3D</span><span style="color:blue">/&gt;</span></p> <p style="margin:0px"><span style="color:blue">      &lt;</span><span style="color:maroon">StaticResource</span><span style="color:blue"> </span><span style="color:red">ResourceKey</span><span style="color:blue">=</span>&quot;<span style="color:blue">ts2</span>&quot;<span style="color:blue">/&gt;</span></p> <p style="margin:0px"><span style="color:blue">    &lt;/</span><span style="color:maroon">Transform3DGroup</span><span style="color:blue">&gt;</span></p> <p style="margin:0px"> </p> <p style="margin:0px"><span style="color:blue">    &lt;</span><span style="color:maroon">Transform3DGroup</span><span style="color:blue"> </span><span style="color:red">x:Name</span><span style="color:blue">=</span>&quot;<span style="color:blue">TG2</span>&quot;<span style="color:blue">&gt;</span></p> <p style="margin:0px"><span style="color:blue">      &lt;</span><span style="color:maroon">TranslateTransform3D</span><span style="color:blue">/&gt;</span></p> <p style="margin:0px"><span style="color:blue">      &lt;</span><span style="color:maroon">StaticResource</span><span style="color:blue"> </span><span style="color:red">ResourceKey</span><span style="color:blue">=</span>&quot;<span style="color:blue">ts2</span>&quot;<span style="color:blue">/&gt;</span></p> <p style="margin:0px"><span style="color:blue">    &lt;/</span><span style="color:maroon">Transform3DGroup</span><span style="color:blue">&gt;</span></p></div> <p>Could someone provide me some information on how to achieve this ?</p> <p> </p> <p>Thanks</p> <p> </p>Tue, 12 Sep 2006 13:39:28 Z2006-09-12T16:01:59Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#e9f08442-5484-4db2-8119-baf26dfe76d3http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#e9f08442-5484-4db2-8119-baf26dfe76d3Rob Relyeahttp://social.msdn.microsoft.com/Profile/en-US/?user=Rob%20RelyeaXamlWriter, MarkupObject How to ?<P>Here is a quick sample I worked on recently...</P> <P>I built my own XamlWriter using MarkupObject and MarkupProperties.<BR>Please let me know of any enhancements you have problems making.</P> <P>Thanks, Rob</P> <P><FONT color=#0000ff size=4>Update 1: enhanced this code to handle IDictionary cases (like Page.Resources - ResourceDictionary)<BR>Update 2: enhanced this code to generate xmlns in the heading &amp; cached the ContentProperty per Type (helps speed even more)...also looks for a null value and uses {x:Null}.</FONT></P> <P><FONT color=#0000ff size=4>Reminder: this sample is not complete...add features you may need...</FONT></P><FONT color=#0000ff size=4><FONT color=#0000ff size=4> <P>using</FONT><FONT size=4><FONT color=#000000> System;</FONT></P></FONT><FONT color=#0000ff size=4> <P>using</FONT><FONT size=4><FONT color=#000000> System.IO;</FONT></P></FONT><FONT color=#0000ff size=4> <P>using</FONT><FONT size=4><FONT color=#000000> System.Xml;</FONT></P></FONT><FONT color=#0000ff size=4> <P>using</FONT><FONT size=4><FONT color=#000000> System.Windows.Markup.Primitives;</FONT></P></FONT><FONT color=#0000ff size=4> <P>using</FONT><FONT size=4><FONT color=#000000> System.Text;</FONT></P></FONT><FONT color=#0000ff size=4> <P>using</FONT><FONT size=4><FONT color=#000000> System.Windows.Markup;</FONT></P></FONT><FONT color=#0000ff size=4> <P>using</FONT><FONT size=4><FONT color=#000000> System.Collections.Generic;</FONT></P></FONT><FONT color=#0000ff size=4> <P>using</FONT><FONT size=4><FONT color=#000000> System.Collections;</FONT></P></FONT><FONT color=#0000ff size=4> <P>using</FONT><FONT size=4><FONT color=#000000> System.Diagnostics;</FONT></P></FONT><FONT color=#0000ff size=4> <P>namespace</FONT><FONT size=4><FONT color=#000000> Microsoft.Samples.XamlWriter</FONT></P> <P>{</P> <P></FONT><FONT color=#008000 size=4>// Summary:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// Provides a single static Overload:SXamlWriter.Save method</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// that can be used for limited Extensible Application</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// Markup Language (XAML) serialization of provided runtime objects. This class</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// cannot be inherited, and only has static methods.</P></FONT><FONT size=4> <P></FONT><FONT color=#0000ff size=4>public</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>static</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>class</FONT><FONT size=4> </FONT><FONT color=#008080 size=4>XamlWriter</P></FONT><FONT size=4> <P>{</P> <P></FONT><FONT color=#008000 size=4>// Summary:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// Returns a Extensible Application Markup Language (XAML) string that serializes</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// the provided object.</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>//</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// Parameters:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// obj:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// The element to be serialized. Typically, this is the root element of a page</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// or application.</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>//</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// Returns:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// Extensible Application Markup Language (XAML) string that can be written</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// to a stream or file. The logical tree of all elements that fall under the</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// provided obj element will be serialized.</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>//</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// Exceptions:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// System.Security.SecurityException:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// the application is not running in full trust.</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>//</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// System.ArgumentNullException:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// obj is null.</P></FONT><FONT size=4> <P></FONT><FONT color=#0000ff size=4>public</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>static</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>string</FONT><FONT size=4> Save(</FONT><FONT color=#0000ff size=4>object</FONT><FONT size=4> obj)</P> <P>{</P> <P></FONT><FONT color=#008080 size=4>StringBuilder</FONT><FONT size=4> sb = </FONT><FONT color=#0000ff size=4>new</FONT><FONT size=4> </FONT><FONT color=#008080 size=4>StringBuilder</FONT><FONT size=4>();</P> <P>WriteObject(obj, sb, </FONT><FONT color=#0000ff size=4>true</FONT><FONT size=4>);</P> <P></FONT><FONT color=#0000ff size=4>return</FONT><FONT size=4> sb.ToString();</P> <P>}</P> <P></FONT><FONT color=#008000 size=4>//WriteObject - 3 params (used primarily when isRoot is true or by the 2 param version)</P></FONT><FONT size=4> <P></FONT><FONT color=#0000ff size=4>private</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>static</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>void</FONT><FONT size=4> WriteObject(</FONT><FONT color=#0000ff size=4>object</FONT><FONT size=4> obj, </FONT><FONT color=#008080 size=4>StringBuilder</FONT><FONT size=4> sb, </FONT><FONT color=#0000ff size=4>bool</FONT><FONT size=4> isRoot)</P> <P>{</P> <P>WriteObjectWithKey(</FONT><FONT color=#0000ff size=4>null</FONT><FONT size=4>, obj, sb, isRoot);</P> <P>}</P> <P></FONT><FONT color=#008000 size=4>//WriteObject - 2 param version</P></FONT><FONT size=4> <P></FONT><FONT color=#0000ff size=4>private</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>static</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>void</FONT><FONT size=4> WriteObject(</FONT><FONT color=#0000ff size=4>object</FONT><FONT size=4> obj, </FONT><FONT color=#008080 size=4>StringBuilder</FONT><FONT size=4> sb)</P> <P>{</P> <P>WriteObjectWithKey(</FONT><FONT color=#0000ff size=4>null</FONT><FONT size=4>, obj, sb, </FONT><FONT color=#0000ff size=4>false</FONT><FONT size=4>);</P> <P>}</P> <P></FONT><FONT color=#008000 size=4>//WriteObject - 3 param version</P></FONT><FONT size=4> <P></FONT><FONT color=#0000ff size=4>private</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>static</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>void</FONT><FONT size=4> WriteObjectWithKey(</FONT><FONT color=#0000ff size=4>object</FONT><FONT size=4> key, </FONT><FONT color=#0000ff size=4>object</FONT><FONT size=4> obj, </FONT><FONT color=#008080 size=4>StringBuilder</FONT><FONT size=4> sb)</P> <P>{</P> <P>WriteObjectWithKey(key, obj, sb, </FONT><FONT color=#0000ff size=4>false</FONT><FONT size=4>);</P> <P>}</P> <P></FONT><FONT color=#0000ff size=4>private</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>static</FONT><FONT size=4> </FONT><FONT color=#008080 size=4>Dictionary</FONT><FONT size=4>&lt;</FONT><FONT color=#008080 size=4>Type</FONT><FONT size=4>, </FONT><FONT color=#0000ff size=4>string</FONT><FONT size=4>&gt; contentProperties = </FONT><FONT color=#0000ff size=4>new</FONT><FONT size=4> </FONT><FONT color=#008080 size=4>Dictionary</FONT><FONT size=4>&lt;</FONT><FONT color=#008080 size=4>Type</FONT><FONT size=4>, </FONT><FONT color=#0000ff size=4>string</FONT><FONT size=4>&gt;();</P> <P></FONT><FONT color=#008000 size=4>//WriteObject - 4 params (used primarily when isRoot is true or by the 3 param version)</P></FONT><FONT size=4> <P></FONT><FONT color=#0000ff size=4>private</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>static</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>void</FONT><FONT size=4> WriteObjectWithKey(</FONT><FONT color=#0000ff size=4>object</FONT><FONT size=4> key, </FONT><FONT color=#0000ff size=4>object</FONT><FONT size=4> obj, </FONT><FONT color=#008080 size=4>StringBuilder</FONT><FONT size=4> sb, </FONT><FONT color=#0000ff size=4>bool</FONT><FONT size=4> isRoot)</P> <P>{</P> <P></FONT><FONT color=#008080 size=4>List</FONT><FONT size=4>&lt;</FONT><FONT color=#008080 size=4>MarkupProperty</FONT><FONT size=4>&gt; propertyElements = </FONT><FONT color=#0000ff size=4>new</FONT><FONT size=4> </FONT><FONT color=#008080 size=4>List</FONT><FONT size=4>&lt;</FONT><FONT color=#008080 size=4>MarkupProperty</FONT><FONT size=4>&gt;();</P> <P></FONT><FONT color=#008000 size=4>//If the value is a string</P></FONT><FONT size=4> <P></FONT><FONT color=#0000ff size=4>string</FONT><FONT size=4> s = obj </FONT><FONT color=#0000ff size=4>as</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>string</FONT><FONT size=4>;</P> <P></FONT><FONT color=#0000ff size=4>if</FONT><FONT size=4> (s != </FONT><FONT color=#0000ff size=4>null</FONT><FONT size=4>)</P> <P>{</P> <P></FONT><FONT color=#008000 size=4>//TODO: in a dictionary, this should be serialized as a &lt;s:String /&gt;</P></FONT><FONT size=4> <P>sb.Append(s);</P> <P></FONT><FONT color=#0000ff size=4>return</FONT><FONT size=4>;</P> <P>}</P> <P></FONT><FONT color=#008080 size=4>MarkupProperty</FONT><FONT size=4> contentProperty = </FONT><FONT color=#0000ff size=4>null</FONT><FONT size=4>;</P> <P></FONT><FONT color=#0000ff size=4>string</FONT><FONT size=4> contentPropertyName = </FONT><FONT color=#0000ff size=4>null</FONT><FONT size=4>;</P> <P></FONT><FONT color=#008080 size=4>MarkupObject</FONT><FONT size=4> markupObj = </FONT><FONT color=#008080 size=4>MarkupWriter</FONT><FONT size=4>.GetMarkupObjectFor(obj);</P> <P></FONT><FONT color=#008080 size=4>Type</FONT><FONT size=4> objectType = obj.GetType();</P> <P>sb.Append(</FONT><FONT color=#800000 size=4>"&lt;"</FONT><FONT size=4> + markupObj.ObjectType.Name);</P> <P></FONT><FONT color=#0000ff size=4>if</FONT><FONT size=4> (isRoot)</P> <P>{</P> <P>sb.Append(</FONT><FONT color=#800000 size=4>" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\""</FONT><FONT size=4>);</P> <P>}</P> <P></FONT><FONT color=#0000ff size=4>if</FONT><FONT size=4> (key != </FONT><FONT color=#0000ff size=4>null</FONT><FONT size=4>)</P> <P>{</P> <P></FONT><FONT color=#0000ff size=4>string</FONT><FONT size=4> keyString = key.ToString();</P> <P></FONT><FONT color=#0000ff size=4>if</FONT><FONT size=4> (keyString.Length &gt; 0)</P> <P>sb.Append(</FONT><FONT color=#800000 size=4>" x:Key=\""</FONT><FONT size=4> + keyString + </FONT><FONT color=#800000 size=4>"\""</FONT><FONT size=4>);</P> <P></FONT><FONT color=#0000ff size=4>else</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>//TODO: key may not be a string, what about x:Type...</P></FONT><FONT size=4> <P></FONT><FONT color=#0000ff size=4>throw</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>new</FONT><FONT size=4> </FONT><FONT color=#008080 size=4>NotImplementedException</FONT><FONT size=4>(</FONT><FONT color=#800000 size=4>"Sample XamlWriter cannot yet handle keys that aren't strings"</FONT><FONT size=4>);</P> <P>}</P> <P></FONT><FONT color=#008000 size=4>//Look for CPA info in our cache that keeps contentProperty names per Type</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>//If it doesn't have an entry, go get the info and store it.</P></FONT><FONT size=4> <P></FONT><FONT color=#0000ff size=4>if</FONT><FONT size=4> (!contentProperties.ContainsKey(objectType))</P> <P>{</P> <P></FONT><FONT color=#0000ff size=4>string</FONT><FONT size=4> lookedUpContentProperty = </FONT><FONT color=#0000ff size=4>string</FONT><FONT size=4>.Empty;</P> <P></FONT><FONT color=#0000ff size=4>foreach</FONT><FONT size=4> (</FONT><FONT color=#008080 size=4>Attribute</FONT><FONT size=4> attr </FONT><FONT color=#0000ff size=4>in</FONT><FONT size=4> markupObj.Attributes)</P> <P>{</P> <P></FONT><FONT color=#008080 size=4>ContentPropertyAttribute</FONT><FONT size=4> cpa = attr </FONT><FONT color=#0000ff size=4>as</FONT><FONT size=4> </FONT><FONT color=#008080 size=4>ContentPropertyAttribute</FONT><FONT size=4>;</P> <P></FONT><FONT color=#0000ff size=4>if</FONT><FONT size=4> (cpa != </FONT><FONT color=#0000ff size=4>null</FONT><FONT size=4>)</P> <P>lookedUpContentProperty = cpa.Name;</P> <P>}</P> <P>contentProperties.Add(objectType, lookedUpContentProperty);</P> <P>}</P> <P>contentPropertyName = contentProperties[objectType];</P> <P>&nbsp;</P> <P></FONT><FONT color=#0000ff size=4>string</FONT><FONT size=4> contentString = </FONT><FONT color=#0000ff size=4>string</FONT><FONT size=4>.Empty;</P> <P></FONT><FONT color=#0000ff size=4>foreach</FONT><FONT size=4> (</FONT><FONT color=#008080 size=4>MarkupProperty</FONT><FONT size=4> markupProperty </FONT><FONT color=#0000ff size=4>in</FONT><FONT size=4> markupObj.Properties)</P> <P>{</P> <P></FONT><FONT color=#0000ff size=4>if</FONT><FONT size=4> (markupProperty.Name != contentPropertyName)</P> <P>{</P> <P></FONT><FONT color=#0000ff size=4>if</FONT><FONT size=4> (markupProperty.IsValueAsString)</P> <P>contentString = markupProperty.Value </FONT><FONT color=#0000ff size=4>as</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>string</FONT><FONT size=4>;</P> <P></FONT><FONT color=#0000ff size=4>else</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>if</FONT><FONT size=4> (!markupProperty.IsComposite)</P> <P>sb.Append(</FONT><FONT color=#800000 size=4>" "</FONT><FONT size=4> + markupProperty.Name + </FONT><FONT color=#800000 size=4>"=\""</FONT><FONT size=4> + markupProperty.Value + </FONT><FONT color=#800000 size=4>"\""</FONT><FONT size=4>);</P> <P></FONT><FONT color=#0000ff size=4>else</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>if</FONT><FONT size=4> (markupProperty.Value.GetType() == </FONT><FONT color=#0000ff size=4>typeof</FONT><FONT size=4>(</FONT><FONT color=#008080 size=4>NullExtension</FONT><FONT size=4>))</P> <P>sb.Append(</FONT><FONT color=#800000 size=4>" "</FONT><FONT size=4> + markupProperty.Name + </FONT><FONT color=#800000 size=4>"=\"{x:Null}\""</FONT><FONT size=4>);</P> <P></FONT><FONT color=#0000ff size=4>else</P></FONT><FONT size=4> <P>{</P> <P></P> <P>propertyElements.Add(markupProperty);</P> <P>}</P> <P>}</P> <P></FONT><FONT color=#0000ff size=4>else</P></FONT><FONT size=4> <P>contentProperty = markupProperty;</P> <P>}</P> <P>&nbsp;</P> <P></FONT><FONT color=#0000ff size=4>if</FONT><FONT size=4> (contentProperty != </FONT><FONT color=#0000ff size=4>null</FONT><FONT size=4> || propertyElements.Count &gt; 0 || contentString != </FONT><FONT color=#0000ff size=4>string</FONT><FONT size=4>.Empty)</P> <P>{</P> <P>sb.Append(</FONT><FONT color=#800000 size=4>"&gt;"</FONT><FONT size=4>);</P> <P></FONT><FONT color=#0000ff size=4>foreach</FONT><FONT size=4> (</FONT><FONT color=#008080 size=4>MarkupProperty</FONT><FONT size=4> markupProp </FONT><FONT color=#0000ff size=4>in</FONT><FONT size=4> propertyElements)</P> <P>{</P> <P></FONT><FONT color=#0000ff size=4>string</FONT><FONT size=4> propElementName = markupObj.ObjectType.Name + </FONT><FONT color=#800000 size=4>"."</FONT><FONT size=4> + markupProp.Name;</P> <P>sb.Append(</FONT><FONT color=#800000 size=4>"&lt;"</FONT><FONT size=4> + propElementName + </FONT><FONT color=#800000 size=4>"&gt;"</FONT><FONT size=4>);</P> <P>WriteChildren(sb, markupProp);</P> <P>sb.Append(</FONT><FONT color=#800000 size=4>"&lt;/"</FONT><FONT size=4> + propElementName + </FONT><FONT color=#800000 size=4>"&gt;"</FONT><FONT size=4>);</P> <P>}</P> <P></FONT><FONT color=#0000ff size=4>if</FONT><FONT size=4> (contentString != </FONT><FONT color=#0000ff size=4>string</FONT><FONT size=4>.Empty)</P> <P>sb.Append(contentString);</P> <P></FONT><FONT color=#0000ff size=4>else</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>if</FONT><FONT size=4> (contentProperty != </FONT><FONT color=#0000ff size=4>null</FONT><FONT size=4>)</P> <P>WriteChildren(sb, contentProperty);</P> <P>sb.Append(</FONT><FONT color=#800000 size=4>"&lt;/"</FONT><FONT size=4> + markupObj.ObjectType.Name + </FONT><FONT color=#800000 size=4>"&gt;"</FONT><FONT size=4>);</P> <P>}</P> <P></FONT><FONT color=#0000ff size=4>else</P></FONT><FONT size=4> <P>{</P> <P>sb.Append(</FONT><FONT color=#800000 size=4>"/&gt;"</FONT><FONT size=4>);</P> <P>}</P> <P>}</P> <P></FONT><FONT color=#0000ff size=4>private</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>static</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>void</FONT><FONT size=4> WriteChildren(</FONT><FONT color=#008080 size=4>StringBuilder</FONT><FONT size=4> sb, </FONT><FONT color=#008080 size=4>MarkupProperty</FONT><FONT size=4> markupProp)</P> <P>{</P> <P></FONT><FONT color=#0000ff size=4>if</FONT><FONT size=4> (!markupProp.IsComposite)</P> <P>{</P> <P></FONT><FONT color=#008080 size=4>XamlWriter</FONT><FONT size=4>.WriteObject(markupProp.Value, sb);</P> <P>}</P> <P></FONT><FONT color=#0000ff size=4>else</P></FONT><FONT size=4> <P>{</P> <P></FONT><FONT color=#008080 size=4>IList</FONT><FONT size=4> collection = markupProp.Value </FONT><FONT color=#0000ff size=4>as</FONT><FONT size=4> </FONT><FONT color=#008080 size=4>IList</FONT><FONT size=4>;</P> <P></FONT><FONT color=#008080 size=4>IDictionary</FONT><FONT size=4> dictionary = markupProp.Value </FONT><FONT color=#0000ff size=4>as</FONT><FONT size=4> </FONT><FONT color=#008080 size=4>IDictionary</FONT><FONT size=4>;</P> <P></FONT><FONT color=#0000ff size=4>if</FONT><FONT size=4> (collection != </FONT><FONT color=#0000ff size=4>null</FONT><FONT size=4>)</P> <P>{</P> <P></FONT><FONT color=#0000ff size=4>foreach</FONT><FONT size=4> (</FONT><FONT color=#0000ff size=4>object</FONT><FONT size=4> o </FONT><FONT color=#0000ff size=4>in</FONT><FONT size=4> collection)</P> <P></FONT><FONT color=#008080 size=4>XamlWriter</FONT><FONT size=4>.WriteObject(o, sb);</P> <P>}</P> <P></FONT><FONT color=#0000ff size=4>else</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>if</FONT><FONT size=4> (dictionary != </FONT><FONT color=#0000ff size=4>null</FONT><FONT size=4>)</P> <P>{</P> <P></FONT><FONT color=#0000ff size=4>foreach</FONT><FONT size=4> (</FONT><FONT color=#0000ff size=4>object</FONT><FONT size=4> key </FONT><FONT color=#0000ff size=4>in</FONT><FONT size=4> dictionary.Keys)</P> <P>{</P> <P></FONT><FONT color=#008080 size=4>XamlWriter</FONT><FONT size=4>.WriteObjectWithKey(key, dictionary[key], sb);</P> <P>}</P> <P>}</P> <P></FONT><FONT color=#0000ff size=4>else</P></FONT><FONT size=4> <P></FONT><FONT color=#008080 size=4>XamlWriter</FONT><FONT size=4>.WriteObject(markupProp.Value, sb);</P> <P>}</P> <P>}</P> <P>&nbsp;</P> <P>&nbsp;</P> <P></FONT><FONT color=#008000 size=4>//</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// Summary:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// Saves Extensible Application Markup Language (XAML) information into a provided</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// stream to serialize the provided object.</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>//</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// Parameters:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// obj:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// The element to be serialized. Typically, this is the root element of a page</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// or application.</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>//</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// stream:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// Destination stream for the serialized XAML information.</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>//</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// Exceptions:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// System.Security.SecurityException:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// the application is not running in full trust.</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>//</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// System.ArgumentNullException:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// obj is null -or- stream is null.</P></FONT><FONT size=4> <P></FONT><FONT color=#0000ff size=4>public</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>static</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>void</FONT><FONT size=4> Save(</FONT><FONT color=#0000ff size=4>object</FONT><FONT size=4> obj, </FONT><FONT color=#008080 size=4>Stream</FONT><FONT size=4> stream) { }</P> <P></FONT><FONT color=#008000 size=4>//</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// Summary:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// Saves Extensible Application Markup Language (XAML) information as the source</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// for a provided text writer object. The output of the text writer can then</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// be used to serialize the provided object.</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>//</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// Parameters:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// writer:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// TextWriter instance to use to write the serialized XAML information.</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>//</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// obj:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// The element to be serialized. Typically, this is the root element of a page</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// or application.</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>//</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// Exceptions:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// System.ArgumentNullException:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// obj is null -or- writer is null.</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>//</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// System.Security.SecurityException:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// the application is not running in full trust.</P></FONT><FONT size=4> <P></FONT><FONT color=#0000ff size=4>public</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>static</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>void</FONT><FONT size=4> Save(</FONT><FONT color=#0000ff size=4>object</FONT><FONT size=4> obj, </FONT><FONT color=#008080 size=4>TextWriter</FONT><FONT size=4> writer) { }</P> <P></FONT><FONT color=#008000 size=4>//</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// Summary:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// Saves Extensible Application Markup Language (XAML) information as the source</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// for a provided XML writer object. The output of the XML writer can then be</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// used to serialize the provided object.</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>//</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// Parameters:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// obj:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// The element to be serialized. Typically, this is the root element of a page</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// or application.</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>//</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// xmlWriter:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// Writer to use to write the serialized XAML information.</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>//</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// Exceptions:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// System.ArgumentNullException:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// obj is null -or- manager is null.</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>//</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// System.Security.SecurityException:</P></FONT><FONT size=4> <P></FONT><FONT color=#008000 size=4>// the application is not running in full trust.</P></FONT><FONT size=4> <P></FONT><FONT color=#0000ff size=4>public</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>static</FONT><FONT size=4> </FONT><FONT color=#0000ff size=4>void</FONT><FONT size=4> Save(</FONT><FONT color=#0000ff size=4>object</FONT><FONT size=4> obj, </FONT><FONT color=#008080 size=4>XmlWriter</FONT><FONT size=4> xmlWriter) { }</P> <P>}</P> <P>}</P></FONT></FONT><FONT size=4></FONT>Tue, 12 Sep 2006 15:38:06 Z2006-09-12T16:01:59Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#ff2955e3-8bf9-4c93-a0a5-12cdc26bed6ehttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#ff2955e3-8bf9-4c93-a0a5-12cdc26bed6egumtoohttp://social.msdn.microsoft.com/Profile/en-US/?user=gumtooXamlWriter, MarkupObject How to ?<p>Thank you very much Rob !</p> <p>I did not have the time to dig into it but after days of msdn reading on Markup.Primitives &amp; others,  this look like the answer to the rubik cube.</p> <p> </p> <p> </p>Tue, 12 Sep 2006 15:52:04 Z2006-09-12T15:52:04Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#65f11f2f-502b-4d90-bbfc-427725282642http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#65f11f2f-502b-4d90-bbfc-427725282642gumtoohttp://social.msdn.microsoft.com/Profile/en-US/?user=gumtooXamlWriter, MarkupObject How to ?<p>working great so far, by adding a check to a lookup hashTable to see if the object (node) that</p> <p>is going to be serialized needs to be replaced by a &lt;StaticResource/&gt; node.</p> <p>serializing a ResourceDictionary will end up with a </p> <p>&quot;An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll&quot;</p> <p>since ResourceDictionary have a property ( or attached prop )  'ResourceDictionary.Entries'</p> <p>which is a ResourceDictionary itself.</p> <p> </p> <p>i understand this, since this is a basic learning sample.</p> <p>i just added a line that check if the node is a ResourceDictionary and if so replace the output by the</p> <p>the full XamlWriter.Save output for that node ( which handle that just fine). </p> <p>Funny, dirty, but i'm only prototyping, experimenting right now.</p> <p> </p> <p>on a side note 'System.StackOverflowException' can append with the full XamlWriter too.</p> <p> </p> <p>Thanks again Rob, this sample is really helpfull.</p> <p> </p> <p> </p>Thu, 14 Sep 2006 13:08:24 Z2006-09-14T13:08:24Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#ec868291-e762-4198-a290-3d9173ab1b43http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#ec868291-e762-4198-a290-3d9173ab1b43Rob Relyeahttp://social.msdn.microsoft.com/Profile/en-US/?user=Rob%20RelyeaXamlWriter, MarkupObject How to ?<p>shortly after i posted the example, i realized it hadn't handled IDictionary (RD, and other) cases.  I updated the sample to handle it...(see the post above.)</p> <p>Let me know what other problems you run into here...</p>Thu, 14 Sep 2006 14:34:31 Z2006-09-14T14:34:31Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#da2d5a00-17ea-4b84-b7e6-e8caa3c369a4http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#da2d5a00-17ea-4b84-b7e6-e8caa3c369a4Fritzenhammerhttp://social.msdn.microsoft.com/Profile/en-US/?user=FritzenhammerXamlWriter, MarkupObject How to ?<p>Thanks Rob - looks like this could be a lot faster from my initial tests.</p> <p>However, there are some problems with the markup this generates.  At some point, I will need to load the generated markup with XamlReader, and it's currently choking on a few things.  Consider the following Xaml (sorry about the formatting - I tried):</p> <p><strong>Generated by existing XamlWriter</strong>:</p> <p>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</p> <p><font face="Tahoma,Helvetica,Sans-Serif">&lt;GeometryDrawing Brush=&quot;{x:Null}&quot; Geometry=&quot;M0,0L100,100 200,200A100,100,0,0,1,300,300&quot; xmlns=&quot;</font><a title="http://schemas.microsoft.com/winfx/2006/xaml/presentation" href="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><font face="Tahoma,Helvetica,Sans-Serif">http://schemas.microsoft.com/winfx/2006/xaml/presentation</font></a><font face="Tahoma,Helvetica,Sans-Serif">&quot; xmlns:x=&quot;</font><a href="http://schemas.microsoft.com/winfx/2006/xaml&quot;&gt;"><font face="Tahoma,Helvetica,Sans-Serif">http://schemas.microsoft.com/winfx/2006/xaml&quot;&gt;</font></a><font face="Tahoma,Helvetica,Sans-Serif"></font></p> <p><font face="Tahoma,Helvetica,Sans-Serif">&lt;GeometryDrawing.Pen&gt;</font></p> <p><font face="Tahoma,Helvetica,Sans-Serif">&lt;Pen Thickness=&quot;1&quot; Brush=&quot;#FFFF0000&quot; /&gt;</font></p> <p><font face="Tahoma,Helvetica,Sans-Serif">&lt;/GeometryDrawing.Pen&gt;</font></p> <p><font face="Tahoma,Helvetica,Sans-Serif">&lt;/GeometryDrawing&gt;</font></p> <p><strong>Generated by new, lighter XamlWriter:</strong></p> <p>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</p> <p>&lt;GeometryDrawing Geometry=&quot;M0,0L100,100 200,200A100,100,0,0,1,300,300&quot;&gt;<br>&lt;GeometryDrawing.Brush&gt;<br>&lt;NullExtension /&gt;<br>&lt;/GeometryDrawing.Brush&gt;<br>&lt;GeometryDrawing.Pen&gt;<br>&lt;Pen Thickness=&quot;1&quot; Brush=&quot;#FFFF0000&quot; /&gt;<br>&lt;/GeometryDrawing.Pen&gt;<br>&lt;GeometryDrawing/&gt;<br></p> <p>XamlReader initially chokes on the missing xmlns attributes, and after that, the NullExtension Element.  I can modify the code to place the xmlns attributes on the root element, but I'm not so sure what to do about NullExtension, since it seems likely that this will happen in other instances as well.</p> <p>Ideas?</p> <p>Thanks,</p> <p>Fred</p>Fri, 15 Sep 2006 13:10:29 Z2006-09-15T13:10:29Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#bb959496-d6fc-47cb-9c54-8dff96b05940http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#bb959496-d6fc-47cb-9c54-8dff96b05940Rob Relyeahttp://social.msdn.microsoft.com/Profile/en-US/?user=Rob%20RelyeaXamlWriter, MarkupObject How to ?<P>I've added a check for a null value, added a small cache for storing the contentproperty for a type, and added the real xmlns files.<BR>The cache may make scenarios that repeat the same element several time faster.</P> <P>Thanks for the bug finding in the sample.</P> <P>Note: the &lt;NullExtension /&gt; failed because it should have put it in the x: namespace.<BR>Regardless, it is more succinct to just use {x:Null} in attribute syntax.</P> <P>-Rob</P>Fri, 15 Sep 2006 16:48:09 Z2006-09-15T16:48:09Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#958a4d81-e1b6-42c6-b430-d6a6c107e96ehttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#958a4d81-e1b6-42c6-b430-d6a6c107e96eFritzenhammerhttp://social.msdn.microsoft.com/Profile/en-US/?user=FritzenhammerXamlWriter, MarkupObject How to ?<p>Thanks Rob - Everything seems to be working now.</p> <p>In my scenario, this is yielding somewhere between 50 and 59% improvements in terms of speed - twice as fast as before!</p> <p>Still not knocking on XamlReader's door, but a nice gain nonetheless.  Thanks a lot for looking into this, and double thanks for the code - I now have something to work with.</p> <p>Thanks again,</p> <p>F.</p> <p> </p>Fri, 15 Sep 2006 18:29:06 Z2006-09-15T18:29:06Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#760aa29d-b93c-49fa-9f5b-5c1816805c6chttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#760aa29d-b93c-49fa-9f5b-5c1816805c6cgumtoohttp://social.msdn.microsoft.com/Profile/en-US/?user=gumtooXamlWriter, MarkupObject How to ?<P>Hello Rob,</P> <P>So far everything run's fine in my atempt to extend your sample to add the possibility for some elements in the tree<BR>i'm xamlizing to be replaced by a &lt;StaticResource ResourceKey="..."&gt; elements.</P> <P>right now, the serialized object is a custom class extending FrameworkElement so i can put my Resources in the&nbsp; .Resources property.</P> <P>i get to the point but in my output . the .&lt;local:theNode.Resources&gt;&nbsp; is at the bottom of the xaml output instead of the top as it usually sit.</P> <P>when i try to read the xaml. an error is throwned that it can't find the specified resourceKey in the resources.<BR>my guess is that the resources are not parsed yet.</P> <P>i tryed to add a [DependsOnAttribute("Resources")] on my property but i guess that if i don't do anything in the&nbsp;writer to handle this , it won't do anything , right ?</P> <P><BR>Thanks.</P> <P>&nbsp;&nbsp;<BR>&lt;local:theNode xmlns="<a target="_blank" title="http://schemas.microsoft.com/winfx/2006/xaml/presentation" href="http://schemas.microsoft.com/winfx/2006/xaml/presentation">http://schemas.microsoft.com/winfx/2006/xaml/presentation</a>"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xmlns:x="<a target="_blank" title="http://schemas.microsoft.com/winfx/2006/xaml" href="http://schemas.microsoft.com/winfx/2006/xaml">http://schemas.microsoft.com/winfx/2006/xaml</a>"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xmlns:local="....."&gt;<BR>....<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Transform3DGroup&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;StaticResource ResourceKey="0422eb15-70c6-42c6-91e9-72d506e98c68" /&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Transform3DGroup&gt;<BR>&nbsp;....&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp; &lt;local:theNode.Resources&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;TranslateTransform3D x:Key="0422eb15-70c6-42c6-91e9-72d506e98c68" /&gt;<BR>&nbsp; &lt;/local:theNode.Resources&gt;<BR>&lt;/local:theNode&gt;</P> <P>&nbsp;</P> <P>Edit: yes, that's the problem. by manually moving Resources up in the xaml it works fine.</P> <P>i might be able to find a way to move it up at write time but any advice would be great. : ]</P> <P>&nbsp;</P>Tue, 26 Sep 2006 15:50:47 Z2006-09-26T15:50:47Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#3156d7eb-40b7-41e2-bc82-9d4d9b876a04http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#3156d7eb-40b7-41e2-bc82-9d4d9b876a04DotNetWisehttp://social.msdn.microsoft.com/Profile/en-US/?user=DotNetWiseXamlWriter, MarkupObject How to ?<p>Guys,</p> <p>Could you please, post a finaly full source code for this?</p> <p>Thanks!</p>Fri, 10 Nov 2006 13:16:59 Z2006-11-10T13:16:59Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#ad0df11f-5f6a-4a21-818a-c42e0d0a8130http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#ad0df11f-5f6a-4a21-818a-c42e0d0a8130BryanLivingstonhttp://social.msdn.microsoft.com/Profile/en-US/?user=BryanLivingstonXamlWriter, MarkupObject How to ?<p> </p> <p>The serializer was producing the following invalid xaml:</p> <p> </p> <p>&lt;Canvas xmlns=&quot;<a title="http://schemas.microsoft.com/winfx/2006/xaml/presentation" href="http://schemas.microsoft.com/winfx/2006/xaml/presentation">http://schemas.microsoft.com/winfx/2006/xaml/presentation</a>&quot; xmlns:x=&quot;<a title="http://schemas.microsoft.com/winfx/2006/xaml" href="http://schemas.microsoft.com/winfx/2006/xaml">http://schemas.microsoft.com/winfx/2006/xaml</a>&quot;&gt;<br>&lt;Path Fill=&quot;{x:Null}&quot; Stroke=&quot;#FF6C3131&quot; Canvas.Left=&quot;-59.5344272949037&quot; Canvas.Top=&quot;159.146446614676&quot;&gt;<br>&lt;Path.Data&gt;<br>&lt;PathGeometry&gt;<br>&lt;PathFigureCollection&gt;M143.534427294904,0.353553385324918L143.534427294904,0.353553385324918 143.534427294904,0.353553385324918 142.534427294904,1.35355338532492 142.534427294904,1.35355338532492 141.534427294904,1.35355338532492 292.534427294904,307.353553385324 295.534427294904,309.353553385324 296.534427294904,309.353553385324 297.534427294904,310.353553385324 298.534427294904,310.353553385324&lt;/PathFigureCollection&gt;<br>&lt;/PathGeometry&gt;<br>&lt;/Path.Data&gt;<br>&lt;/Path&gt;<br>&lt;/Canvas&gt;<br></p> <p> </p> <p>I fixed it by adding <font size=2> || !markupProperty.IsContent) to the following section of <font size=2></p> <p>WriteObjectWithKey.</p></font></font> <p> </p><font size=2> <p></font><font color="#0000ff" size=2>string</font><font size=2> contentString = </font><font color="#0000ff" size=2>string</font><font size=2>.Empty;</p> <p></font><font color="#0000ff" size=2>foreach</font><font size=2> (</font><font color="#2b91af" size=2>MarkupProperty</font><font size=2> markupProperty </font><font color="#0000ff" size=2>in</font><font size=2> markupObj.Properties)</p> <p>{</p> <p></font><font color="#0000ff" size=2>if</font><font size=2> (markupProperty.Name != contentPropertyName || !markupProperty.IsContent)</p> <p>{</p> <p></font><font color="#0000ff" size=2>if</font><font size=2> (markupProperty.IsValueAsString)</p> <p>contentString = markupProperty.Value </font><font color="#0000ff" size=2>as</font><font size=2> </font><font color="#0000ff" size=2>string</font><font size=2>;</p> <p></font><font color="#0000ff" size=2>else</font><font size=2> </font><font color="#0000ff" size=2>if</font><font size=2> (!markupProperty.IsComposite)</p> <p>sb.Append(</font><font color="#a31515" size=2>&quot; &quot;</font><font size=2> + markupProperty.Name + </font><font color="#a31515" size=2>&quot;=\&quot;&quot;</font><font size=2> + markupProperty.Value + </font><font color="#a31515" size=2>&quot;\&quot;&quot;</font><font size=2>);</p> <p></font><font color="#0000ff" size=2>else</font><font size=2> </font><font color="#0000ff" size=2>if</font><font size=2> (markupProperty.Value.GetType() == </font><font color="#0000ff" size=2>typeof</font><font size=2>(</font><font color="#2b91af" size=2>NullExtension</font><font size=2>))</p> <p>sb.Append(</font><font color="#a31515" size=2>&quot; &quot;</font><font size=2> + markupProperty.Name + </font><font color="#a31515" size=2>&quot;=\&quot;{x:Null}\&quot;&quot;</font><font size=2>);</p> <p></font><font color="#0000ff" size=2>else</p></font><font size=2> <p>{</p> <p>propertyElements.Add(markupProperty);</p> <p>}</p> <p>}</p> <p></font><font color="#0000ff" size=2>else</p></font><font size=2> <p>contentProperty = markupProperty;</p> <p>}</p> <p> </p> <p> </p> <p>The IsContent flag on MarkupProperty makes me wonder if the serializer shouldn't be using the ContentPropertyAttribute at all.</font></p> <p> </p> <p>Thanks again for the great serializer.  It's saved me a huge amout of time and I recently customized it accept &quot;adoptedChildren&quot; on the root element so I can ues it for copy/paste without moving selected entites around.</p> <p> </p> <p>Bryan Livingston</p>Sat, 16 Jun 2007 22:24:17 Z2007-06-16T22:24:17Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#8ba6f1b4-2c12-47ac-bdb2-285b5908ea3dhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#8ba6f1b4-2c12-47ac-bdb2-285b5908ea3dJaap Taalhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jaap%20TaalXamlWriter, MarkupObject How to ?<font face=Arial size=2> <p class=MsoNormal style="margin:0cm 0cm 0pt"><span style=""><font size=3><font face="Trebuchet MS">I’m trying to use the Sample XamlWriter, however I get an exception in some situation. After some debugging I found out that the MarkupProperty throws an exception.</font></font></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><span style=""><font size=3><font face="Trebuchet MS">I’ve isolated the problem. Funny thing is that the original XamlWriter functions without any error.</font></font></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><span style=""><font face="Trebuchet MS" size=3> </font></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style="font-size:10pt;color:#2b91af;font-family:'Courier New'">Setter</span><span style="font-size:10pt;font-family:'Courier New'"> setter = <span style="color:blue">new</span> <span style="color:#2b91af">Setter</span>();</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style="font-size:10pt;font-family:'Courier New'">setter.Property = <span style="color:#2b91af">TextBox</span>.VerticalScrollBarVisibilityProperty;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style="font-size:10pt;font-family:'Courier New'">setter.Value = <span style="color:#2b91af">ScrollBarVisibility</span>.Auto;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style="font-size:10pt;color:#2b91af;font-family:'Courier New'">MarkupObject</span><span style="font-size:10pt;font-family:'Courier New'"> markupObject = <span style="color:#2b91af">MarkupWriter</span>.GetMarkupObjectFor(setter);</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style="font-size:10pt;color:blue;font-family:'Courier New'">foreach</span><span style="font-size:10pt;font-family:'Courier New'"> (<span style="color:#2b91af">MarkupProperty</span> markupProperty <span style="color:blue">in</span> markupObject.Properties)</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style="font-size:10pt;font-family:'Courier New'">{</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style="font-size:10pt;color:#2b91af;font-family:'Courier New'"><span style="">      </span>Console</span><span style="font-size:10pt;font-family:'Courier New'">.WriteLine(<span style="color:#a31515">&quot;Value&quot;</span>, markupProperty.Value);</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style="font-size:10pt;font-family:'Courier New'">}</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style="font-size:10pt;color:#2b91af;font-family:'Courier New'">Console</span><span style="font-size:10pt;font-family:'Courier New'">.WriteLine(System.Windows.Markup.<span style="color:#2b91af">XamlWriter</span>.Save(setter));</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style=""><font face="Trebuchet MS" size=3> </font></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style=""><font size=3><font face="Trebuchet MS">Output:</font></font></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style=""><font size=3><font face="Trebuchet MS">NullReferenceException</font></font></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style=""><font size=3><font face="Trebuchet MS">Object reference not set to an instance of an object.</font></font></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style=""><font size=3><font face="Trebuchet MS"><span style="">   </span>at System.Windows.Markup.Primitives.ElementProperty.CheckForMarkupExtension(Type propertyType, Object value, IValueSerializerContext context, Boolean convertEnums)</font></font></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style=""><font size=3><font face="Trebuchet MS"><span style="">   </span>at System.Windows.Markup.Primitives.ElementProperty.get_Value()</font></font></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style=""><font size=3><font face="Trebuchet MS"><span style="">   </span>at …</font></font></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style=""><font face="Trebuchet MS" size=3> </font></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style=""><font size=3><font face="Trebuchet MS">(Somehow the Sample XamlWriter throws this exception testing the IsComposite property, my code only throws exception when using the Value property)</font></font></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style=""><font face="Trebuchet MS" size=3> </font></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style=""><font size=3><font face="Trebuchet MS">I guess it has something to do with that the value is an enum value, the output of the original xamlwriter is:</font></font></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style="font-size:8.5pt;font-family:'Courier New'">&lt;Setter Property=&quot;ScrollViewer.VerticalScrollBarVisibility&quot; xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot; xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;&gt;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style="font-size:8.5pt;font-family:'Courier New'">&lt;Setter.Value&gt;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style="font-size:8.5pt;font-family:'Courier New'">&lt;x<img alt="Tongue Tied" src="http://forums.microsoft.com/MSDN/emoticons/emotion-7.gif">tatic Member=&quot;ScrollBarVisibility.Auto&quot; /&gt;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style="font-size:8.5pt;font-family:'Courier New'">&lt;/Setter.Value&gt;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style="font-size:8.5pt;font-family:'Courier New'">&lt;/Setter&gt;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style="font-size:8.5pt;font-family:'Courier New'"> </span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span style=""><font size=3><font face="Trebuchet MS">Please help me understand this error, I’m really stuck on this. How should I use the markupobject/markupproperty classes to know that I need a x<img alt="Tongue Tied" src="http://forums.microsoft.com/MSDN/emoticons/emotion-7.gif">tatic markupextension?</font></font></span></p> <p align=left></font> </p>Fri, 19 Oct 2007 08:10:56 Z2007-10-19T08:10:56Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#7197ae17-a0fb-4094-bf7b-1910b2104d71http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#7197ae17-a0fb-4094-bf7b-1910b2104d71BryanLivingstonhttp://social.msdn.microsoft.com/Profile/en-US/?user=BryanLivingstonXamlWriter, MarkupObject How to ?<p align=left><font face=Arial size=2></font> </p> <p>I've found a bug in this serializer, and am not sure it can be fixed.</p> <p align=left> </p> <p align=left>It happens with certain international languages, for example <strong>dutch netherlands</strong>.  The Writer will produce xaml that looks like this:</p> <p align=left> </p> <p align=left>&lt;Path xmlns=&quot;<a title="http://schemas.microsoft.com/winfx/2006/xaml/presentation" href="http://schemas.microsoft.com/winfx/2006/xaml/presentation">http://schemas.microsoft.com/winfx/2006/xaml/presentation</a>&quot; xmlns:x=&quot;<a title="http://schemas.microsoft.com/winfx/2006/xaml" href="http://schemas.microsoft.com/winfx/2006/xaml">http://schemas.microsoft.com/winfx/2006/xaml</a>&quot; Stroke=&quot;#FF000000&quot; StrokeThickness=&quot;1&quot; StrokeStartLineCap=&quot;Flat&quot; StrokeEndLineCap=&quot;Flat&quot; StrokeDashCap=&quot;Square&quot; StrokeLineJoin=&quot;Miter&quot; StrokeDashOffset=&quot;0&quot; StrokeDashArray=&quot;&quot;&gt;<br>&lt;Path.Data&gt;<br>&lt;GeometryGroup FillRule=&quot;Nonzero&quot;&gt;<br>&lt;GeometryGroup.Children&gt;<br>&lt;PathGeometry FillRule=&quot;Nonzero&quot; Figures=&quot;M21,9839992523193;27,216667175293L47,1360015869141;30,3846664428711C50,4000015258789;32,1766738891602 52,1279983520508;34...</p> <p> </p> <p align=left><strong>The code in the figures is incorrect, it uses comma for a decimal point and the semicolon's should be spaces.</strong></p> <p align=left> </p> <p align=left>Since the value for figures is being created by the system method <font color="#2b91af" size=2>MarkupWriter</font><font size=2>.GetMarkupObjectFor(obj), there isn't away to control it or pass it a invariant culture.</font></p> <p align=left><font size=2></font> </p> <p align=left><font size=2>Is this MarkupWriter class broken or is there a way to fix this?</font></p> <p align=left><font size=2></font> </p> <p align=left><font size=2>Thanks.</font></p> <p align=left><font size=2></font> </p> <p align=left><font size=2>Bryan</p></font>Thu, 20 Dec 2007 20:26:57 Z2007-12-20T20:26:57Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#114d2a01-d206-46ec-b3ad-a28b709c0f6ehttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#114d2a01-d206-46ec-b3ad-a28b709c0f6epeanut_zweihttp://social.msdn.microsoft.com/Profile/en-US/?user=peanut_zweiXamlWriter, MarkupObject How to ?Hi, i tried to use the XamlWriter in the sample in my application.<div><br></div><div>However, in my application, i will have lots and lots of self defined elements which required explicit namespaces to be appended as prefix of each elements. In that case, the XamlWriter doesn't work anymore since it is generating content all without the user defined namespaces.</div><div><br></div><div>Could anything be done to cater for the mentioned issue?</div><div><br></div><div>Thank you.</div><div><br></div><div>peanut_zwei</div>Fri, 22 Aug 2008 02:40:41 Z2008-08-22T02:40:41Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#112b3a05-9cc9-434d-9e58-a827c4529cd9http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#112b3a05-9cc9-434d-9e58-a827c4529cd9Virender Sandhuhttp://social.msdn.microsoft.com/Profile/en-US/?user=Virender%20SandhuXamlWriter, MarkupObject How to ? I am working on XamlWriter to support explicit namespaces. It shall be ready in 2-3 days. Once it is in working condition, I'll post it here.Fri, 29 Aug 2008 16:21:36 Z2008-08-29T16:21:36Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#905d5e75-c0b6-4cbb-a45f-9cd50851cee3http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#905d5e75-c0b6-4cbb-a45f-9cd50851cee3peanut_zweihttp://social.msdn.microsoft.com/Profile/en-US/?user=peanut_zweiXamlWriter, MarkupObject How to ?Thanks Sandhu for the effort. I will be waiting for your code to try it out within my application.<div>Thanks a lot. :)</div><div><br></div><div>peanut_zwei</div>Thu, 04 Sep 2008 07:43:26 Z2008-09-04T07:43:26Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#00d9fd86-4f75-4266-aa75-4b556c9ee501http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08aebbf1-0a61-4305-83b2-a0a37bb24002#00d9fd86-4f75-4266-aa75-4b556c9ee501Virender Sandhuhttp://social.msdn.microsoft.com/Profile/en-US/?user=Virender%20SandhuXamlWriter, MarkupObject How to ? Hello,<br><br>I couldn't spare time to cleanup the code. So the code works but may need performance tuneup depending on your usages. The most of code is copied from Rob's sample code; I have added custom namespace support.<br><br><font color="#0000ff" size=2><font color="#0000ff" size=2> <p>using</font></font><font size=2> System;</p></font><font color="#0000ff" size=2><font color="#0000ff" size=2> <p>using</font></font><font size=2> System.Collections.Generic;</p></font><font color="#0000ff" size=2><font color="#0000ff" size=2> <p>using</font></font><font size=2> System.Linq;</p></font><font color="#0000ff" size=2><font color="#0000ff" size=2> <p>using</font></font><font size=2> System.Text;</p></font><font color="#0000ff" size=2><font color="#0000ff" size=2> <p>using</font></font><font size=2> System.Xml;</p></font><font color="#0000ff" size=2><font color="#0000ff" size=2> <p>using</font></font><font size=2> System.Windows.Markup.Primitives;</p></font><font color="#0000ff" size=2><font color="#0000ff" size=2> <p>using</font></font><font size=2> System.IO;</p></font><font color="#0000ff" size=2><font color="#0000ff" size=2> <p>using</font></font><font size=2> System.Windows.Markup;</p></font><font color="#0000ff" size=2><font color="#0000ff" size=2> <p>using</font></font><font size=2> System.Collections;</p></font><font color="#0000ff" size=2><font color="#0000ff" size=2> <p>namespace</font></font><font size=2> Ikon.Samples.Wpf</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>public</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>class</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>XamlWriter</p></font></font><font size=2> <p>{</p> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>Dictionary</font></font><font size=2>&lt;</font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>, </font><font color="#2b91af" size=2><font color="#2b91af" size=2>NamespaceMap</font></font><font size=2>&gt; m_nsMap = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>new</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>Dictionary</font></font><font size=2>&lt;</font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>, </font><font color="#2b91af" size=2><font color="#2b91af" size=2>NamespaceMap</font></font><font size=2>&gt;();</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>private</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>Dictionary</font></font><font size=2>&lt;</font><font color="#2b91af" size=2><font color="#2b91af" size=2>Type</font></font><font size=2>, </font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>&gt; m_contentPropertiesCache = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>new</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>Dictionary</font></font><font size=2>&lt;</font><font color="#2b91af" size=2><font color="#2b91af" size=2>Type</font></font><font size=2>, </font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>&gt;();</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>private</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>NamespaceCache</font></font><font size=2> m_namespaces = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>new</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>NamespaceCache</font></font><font size=2>();</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>public</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> Save(</font><font color="#0000ff" size=2><font color="#0000ff" size=2>object</font></font><font size=2> obj)</p> <p>{</p> <p>ResolveXmlNamespaces(obj);</p> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>StringBuilder</font></font><font size=2> sb = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>new</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>StringBuilder</font></font><font size=2>();</p> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>StringWriter</font></font><font size=2> writer = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>new</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>StringWriter</font></font><font size=2>(sb);</p> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>XmlTextWriter</font></font><font size=2> tw = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>new</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>XmlTextWriter</font></font><font size=2>(writer);</p> <p>tw.Namespaces = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>false</font></font><font size=2>;</p> <p>WriteObject(</font><font color="#0000ff" size=2><font color="#0000ff" size=2>null</font></font><font size=2>, obj, tw, </font><font color="#0000ff" size=2><font color="#0000ff" size=2>true</font></font><font size=2>);</p> <p>tw.Flush();</p> <p>tw.Close();</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>return</font></font><font size=2> sb.ToString();</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>private</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>void</font></font><font size=2> WriteObject(</font><font color="#0000ff" size=2><font color="#0000ff" size=2>object</font></font><font size=2> key, </font><font color="#0000ff" size=2><font color="#0000ff" size=2>object</font></font><font size=2> obj, </font><font color="#2b91af" size=2><font color="#2b91af" size=2>XmlTextWriter</font></font><font size=2> writer, </font><font color="#0000ff" size=2><font color="#0000ff" size=2>bool</font></font><font size=2> isRoot)</p> <p>{</p> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>List</font></font><font size=2>&lt;</font><font color="#2b91af" size=2><font color="#2b91af" size=2>MarkupProperty</font></font><font size=2>&gt; propertyElements = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>new</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>List</font></font><font size=2>&lt;</font><font color="#2b91af" size=2><font color="#2b91af" size=2>MarkupProperty</font></font><font size=2>&gt;();</p> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>MarkupProperty</font></font><font size=2> contentProperty = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>null</font></font><font size=2>;</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> contentPropertyName = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>null</font></font><font size=2>;</p> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>MarkupObject</font></font><font size=2> markupObj = </font><font color="#2b91af" size=2><font color="#2b91af" size=2>MarkupWriter</font></font><font size=2>.GetMarkupObjectFor(obj);</p> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>Type</font></font><font size=2> objectType = markupObj.ObjectType;</p> <p>m_namespaces.GetXmlNamespace(objectType);</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> ns = m_namespaces.GetXmlNamespace(objectType);</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> prefix = m_namespaces.GetPrefixForNamespace(ns);</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (</font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>.IsNullOrEmpty(prefix))</p> <p>writer.WriteStartElement(markupObj.ObjectType.Name);</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</p></font></font><font size=2> <p>writer.WriteStartElement(prefix + </font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;:&quot;</font></font><font size=2> + markupObj.ObjectType.Name);</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (isRoot)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>foreach</font></font><font size=2> (</font><font color="#2b91af" size=2><font color="#2b91af" size=2>NamespaceMap</font></font><font size=2> map </font><font color="#0000ff" size=2><font color="#0000ff" size=2>in</font></font><font size=2> m_nsMap.Values)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (</font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>.IsNullOrEmpty(map.Prefix))</p> <p>writer.WriteAttributeString(</font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;xmlns&quot;</font></font><font size=2>, map.XmlNamespace);</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</p></font></font><font size=2> <p>writer.WriteAttributeString(</font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;xmlns:&quot;</font></font><font size=2> + map.Prefix, map.XmlNamespace);</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (!m_nsMap.ContainsKey(</font><font color="#2b91af" size=2><font color="#2b91af" size=2>NamespaceCache</font></font><font size=2>.XamlNamespace))</p> <p>{</p> <p>writer.WriteAttributeString(</font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;xmlns:x&quot;</font></font><font size=2>, </font><font color="#2b91af" size=2><font color="#2b91af" size=2>NamespaceCache</font></font><font size=2>.XamlNamespace);</p> <p>}</p> <p>writer.WriteAttributeString(</font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;xml:space&quot;</font></font><font size=2>, </font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;preserve&quot;</font></font><font size=2>);</p> <p>}</p> <p></font><font color="#008000" size=2><font color="#008000" size=2>//writer.</p></font></font><font size=2> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (key != </font><font color="#0000ff" size=2><font color="#0000ff" size=2>null</font></font><font size=2>)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> keyString = key.ToString();</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (keyString.Length &gt; 0)</p> <p>writer.WriteAttributeString(</font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;x:Key&quot;</font></font><font size=2>, keyString);</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</p></font></font><font size=2> <p></font><font color="#008000" size=2><font color="#008000" size=2>//TODO: key may not be a string, what about x:Type...</p></font></font><font size=2> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>throw</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>new</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>NotImplementedException</font></font><font size=2>(</font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;Sample XamlWriter cannot yet handle keys that aren't strings&quot;</font></font><font size=2>);</p> <p>}</p> <p></font><font color="#008000" size=2><font color="#008000" size=2>//Look for CPA info in our cache that keeps contentProperty names per Type</p></font></font><font size=2> <p></font><font color="#008000" size=2><font color="#008000" size=2>//If it doesn't have an entry, go get the info and store it.</p></font></font><font size=2> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (!m_contentPropertiesCache.ContainsKey(objectType))</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> lookedUpContentProperty = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>.Empty;</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>foreach</font></font><font size=2> (</font><font color="#2b91af" size=2><font color="#2b91af" size=2>Attribute</font></font><font size=2> attr </font><font color="#0000ff" size=2><font color="#0000ff" size=2>in</font></font><font size=2> markupObj.Attributes)</p> <p>{</p> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>ContentPropertyAttribute</font></font><font size=2> cpa = attr </font><font color="#0000ff" size=2><font color="#0000ff" size=2>as</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>ContentPropertyAttribute</font></font><font size=2>;</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (cpa != </font><font color="#0000ff" size=2><font color="#0000ff" size=2>null</font></font><font size=2>)</p> <p>{</p> <p>lookedUpContentProperty = cpa.Name;</p> <p></font><font color="#008000" size=2><font color="#008000" size=2>//Once content property is found, come out of the loop.</p></font></font><font size=2> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>break</font></font><font size=2>;</p> <p>}</p> <p>}</p> <p>m_contentPropertiesCache.Add(objectType, lookedUpContentProperty);</p> <p>}</p> <p>contentPropertyName = m_contentPropertiesCache[objectType];</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> contentString = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>.Empty;</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>foreach</font></font><font size=2> (</font><font color="#2b91af" size=2><font color="#2b91af" size=2>MarkupProperty</font></font><font size=2> markupProperty </font><font color="#0000ff" size=2><font color="#0000ff" size=2>in</font></font><font size=2> markupObj.Properties)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (markupProperty.Name != contentPropertyName)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (markupProperty.IsValueAsString)</p> <p>{</p> <p>contentString = markupProperty.Value </font><font color="#0000ff" size=2><font color="#0000ff" size=2>as</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>;</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (!markupProperty.IsComposite)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> temp = markupProperty.Value == </font><font color="#0000ff" size=2><font color="#0000ff" size=2>null</font></font><font size=2> ? </font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>.Empty : markupProperty.Value.ToString();</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (markupProperty.IsAttached)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> ns1 = m_namespaces.GetXmlNamespace(markupProperty.DependencyProperty.OwnerType);</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> prefix1 = m_namespaces.GetPrefixForNamespace(ns1);</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (</font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>.IsNullOrEmpty(prefix1))</p> <p>{</p> <p>writer.WriteAttributeString(markupProperty.Name, temp);</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</p></font></font><font size=2> <p>{</p> <p>writer.WriteAttributeString(prefix1 + </font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;:&quot;</font></font><font size=2> + markupProperty.Name, temp);</p> <p>}</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</p></font></font><font size=2> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (markupProperty.Name == </font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;Name&quot;</font></font><font size=2> &amp;&amp; </font><font color="#0000ff" size=2><font color="#0000ff" size=2>this</font></font><font size=2>.m_namespaces.GetAssemblyNameFromType(markupProperty.DependencyProperty.OwnerType).Equals(</font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;PresentationFramework&quot;</font></font><font size=2>))</p> <p>{</p> <p>writer.WriteAttributeString(</font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;x:&quot;</font></font><font size=2> + markupProperty.Name, temp);</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</p></font></font><font size=2> <p>{</p> <p>writer.WriteAttributeString(markupProperty.Name, temp);</p> <p>}</p> <p>}</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (markupProperty.Value.GetType() == </font><font color="#0000ff" size=2><font color="#0000ff" size=2>typeof</font></font><font size=2>(</font><font color="#2b91af" size=2><font color="#2b91af" size=2>NullExtension</font></font><font size=2>))</p> <p>{</p> <p>writer.WriteAttributeString(markupProperty.Name, </font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;{x:Null}&quot;</font></font><font size=2>);</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</p></font></font><font size=2> <p>{</p> <p>propertyElements.Add(markupProperty);</p> <p>}</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</p></font></font><font size=2> <p>{</p> <p>contentProperty = markupProperty;</p> <p>}</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (contentProperty != </font><font color="#0000ff" size=2><font color="#0000ff" size=2>null</font></font><font size=2> || propertyElements.Count &gt; 0 || contentString != </font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>.Empty)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>foreach</font></font><font size=2> (</font><font color="#2b91af" size=2><font color="#2b91af" size=2>MarkupProperty</font></font><font size=2> markupProp </font><font color="#0000ff" size=2><font color="#0000ff" size=2>in</font></font><font size=2> propertyElements)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> ns2 = m_namespaces.GetXmlNamespace(markupObj.ObjectType);</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> prefix2 = m_namespaces.GetPrefixForNamespace(ns2);</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> propElementName = markupObj.ObjectType.Name + </font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;.&quot;</font></font><font size=2> + markupProp.Name;</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (</font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>.IsNullOrEmpty(prefix2))</p> <p>{</p> <p>writer.WriteStartElement(propElementName);</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</p></font></font><font size=2> <p>{</p> <p>writer.WriteStartElement(prefix2 + </font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;:&quot;</font></font><font size=2> + propElementName);</p> <p>}</p> <p>WriteChildren(writer, markupProp);</p> <p>writer.WriteEndElement();</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (contentString != </font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>.Empty)</p> <p>{</p> <p>writer.WriteValue(contentString);</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (contentProperty != </font><font color="#0000ff" size=2><font color="#0000ff" size=2>null</font></font><font size=2>)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (contentProperty.Value </font><font color="#0000ff" size=2><font color="#0000ff" size=2>is</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>)</p> <p>{</p> <p>writer.WriteValue(contentProperty.StringValue);</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</p></font></font><font size=2> <p>{</p> <p>WriteChildren(writer, contentProperty);</p> <p>}</p> <p>}</p> <p>}</p> <p>writer.WriteEndElement();</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>private</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>void</font></font><font size=2> WriteChildren(</font><font color="#2b91af" size=2><font color="#2b91af" size=2>XmlTextWriter</font></font><font size=2> writer, </font><font color="#2b91af" size=2><font color="#2b91af" size=2>MarkupProperty</font></font><font size=2> markupProp)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (!markupProp.IsComposite)</p> <p>{</p> <p>WriteObject(</font><font color="#0000ff" size=2><font color="#0000ff" size=2>null</font></font><font size=2>, markupProp.Value, writer, </font><font color="#0000ff" size=2><font color="#0000ff" size=2>false</font></font><font size=2>);</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</p></font></font><font size=2> <p>{</p> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>IList</font></font><font size=2> collection = markupProp.Value </font><font color="#0000ff" size=2><font color="#0000ff" size=2>as</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>IList</font></font><font size=2>;</p> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>IDictionary</font></font><font size=2> dictionary = markupProp.Value </font><font color="#0000ff" size=2><font color="#0000ff" size=2>as</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>IDictionary</font></font><font size=2>;</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (collection != </font><font color="#0000ff" size=2><font color="#0000ff" size=2>null</font></font><font size=2>)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>foreach</font></font><font size=2> (</font><font color="#0000ff" size=2><font color="#0000ff" size=2>object</font></font><font size=2> o </font><font color="#0000ff" size=2><font color="#0000ff" size=2>in</font></font><font size=2> collection)</p> <p>{</p> <p>WriteObject(</font><font color="#0000ff" size=2><font color="#0000ff" size=2>null</font></font><font size=2>, o, writer, </font><font color="#0000ff" size=2><font color="#0000ff" size=2>false</font></font><font size=2>);</p> <p>}</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (dictionary != </font><font color="#0000ff" size=2><font color="#0000ff" size=2>null</font></font><font size=2>)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>foreach</font></font><font size=2> (</font><font color="#0000ff" size=2><font color="#0000ff" size=2>object</font></font><font size=2> key </font><font color="#0000ff" size=2><font color="#0000ff" size=2>in</font></font><font size=2> dictionary.Keys)</p> <p>{</p> <p>WriteObject(key, dictionary[key], writer, </font><font color="#0000ff" size=2><font color="#0000ff" size=2>false</font></font><font size=2>);</p> <p>}</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</p></font></font><font size=2> <p>{</p> <p>WriteObject(</font><font color="#0000ff" size=2><font color="#0000ff" size=2>null</font></font><font size=2>, markupProp.Value, writer, </font><font color="#0000ff" size=2><font color="#0000ff" size=2>false</font></font><font size=2>);</p> <p>}</p> <p>}</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>private</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>void</font></font><font size=2> ResolveXmlNamespaces(</font><font color="#0000ff" size=2><font color="#0000ff" size=2>object</font></font><font size=2> obj)</p> <p>{</p> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>List</font></font><font size=2>&lt;</font><font color="#2b91af" size=2><font color="#2b91af" size=2>MarkupProperty</font></font><font size=2>&gt; propertyElements = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>new</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>List</font></font><font size=2>&lt;</font><font color="#2b91af" size=2><font color="#2b91af" size=2>MarkupProperty</font></font><font size=2>&gt;();</p> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>MarkupProperty</font></font><font size=2> contentProperty = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>null</font></font><font size=2>;</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> contentPropertyName = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>null</font></font><font size=2>;</p> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>MarkupObject</font></font><font size=2> markupObj = </font><font color="#2b91af" size=2><font color="#2b91af" size=2>MarkupWriter</font></font><font size=2>.GetMarkupObjectFor(obj);</p> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>Type</font></font><font size=2> objectType = markupObj.ObjectType;</p> <p>m_namespaces.GetXmlNamespace(objectType);</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> ns = m_namespaces.GetXmlNamespace(objectType);</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> prefix = m_namespaces.GetPrefixForNamespace(ns);</p> <p>m_nsMap[ns] = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>new</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>NamespaceMap</font></font><font size=2>(prefix, ns);</p> <p></font><font color="#008000" size=2><font color="#008000" size=2>//Look for CPA info in our cache that keeps contentProperty names per Type</p></font></font><font size=2> <p></font><font color="#008000" size=2><font color="#008000" size=2>//If it doesn't have an entry, go get the info and store it.</p></font></font><font size=2> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (!m_contentPropertiesCache.ContainsKey(objectType))</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> lookedUpContentProperty = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>.Empty;</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>foreach</font></font><font size=2> (</font><font color="#2b91af" size=2><font color="#2b91af" size=2>Attribute</font></font><font size=2> attr </font><font color="#0000ff" size=2><font color="#0000ff" size=2>in</font></font><font size=2> markupObj.Attributes)</p> <p>{</p> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>ContentPropertyAttribute</font></font><font size=2> cpa = attr </font><font color="#0000ff" size=2><font color="#0000ff" size=2>as</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>ContentPropertyAttribute</font></font><font size=2>;</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (cpa != </font><font color="#0000ff" size=2><font color="#0000ff" size=2>null</font></font><font size=2>)</p> <p>{</p> <p>lookedUpContentProperty = cpa.Name;</p> <p></font><font color="#008000" size=2><font color="#008000" size=2>//Once content property is found, come out of the loop.</p></font></font><font size=2> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>break</font></font><font size=2>;</p> <p>}</p> <p>}</p> <p>m_contentPropertiesCache.Add(objectType, lookedUpContentProperty);</p> <p>}</p> <p>contentPropertyName = m_contentPropertiesCache[objectType];</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> contentString = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>.Empty;</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>foreach</font></font><font size=2> (</font><font color="#2b91af" size=2><font color="#2b91af" size=2>MarkupProperty</font></font><font size=2> markupProperty </font><font color="#0000ff" size=2><font color="#0000ff" size=2>in</font></font><font size=2> markupObj.Properties)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (markupProperty.Name != contentPropertyName)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (markupProperty.IsValueAsString)</p> <p>{</p> <p>contentString = markupProperty.Value </font><font color="#0000ff" size=2><font color="#0000ff" size=2>as</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>;</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (!markupProperty.IsComposite)</p> <p>{</p> <p></font><font color="#008000" size=2><font color="#008000" size=2>//Bug Fix DX-0120123</p></font></font><font size=2> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> ns1 = m_namespaces.GetXmlNamespace(markupProperty.DependencyProperty.OwnerType);</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> prefix1 = m_namespaces.GetPrefixForNamespace(ns1);</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (!</font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>.IsNullOrEmpty(prefix1))</p> <p>{</p> <p>m_nsMap[ns1] = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>new</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>NamespaceMap</font></font><font size=2>(prefix1, ns1);</p> <p>}</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (markupProperty.Value.GetType() == </font><font color="#0000ff" size=2><font color="#0000ff" size=2>typeof</font></font><font size=2>(</font><font color="#2b91af" size=2><font color="#2b91af" size=2>NullExtension</font></font><font size=2>))</p> <p>{</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</p></font></font><font size=2> <p>{</p> <p>propertyElements.Add(markupProperty);</p> <p>}</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</p></font></font><font size=2> <p>{</p> <p>contentProperty = markupProperty;</p> <p>}</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (contentProperty != </font><font color="#0000ff" size=2><font color="#0000ff" size=2>null</font></font><font size=2> || propertyElements.Count &gt; 0 || contentString != </font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>.Empty)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>foreach</font></font><font size=2> (</font><font color="#2b91af" size=2><font color="#2b91af" size=2>MarkupProperty</font></font><font size=2> markupProp </font><font color="#0000ff" size=2><font color="#0000ff" size=2>in</font></font><font size=2> propertyElements)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> ns2 = m_namespaces.GetXmlNamespace(markupObj.ObjectType);</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> prefix2 = m_namespaces.GetPrefixForNamespace(ns2);</p> <p>m_nsMap[ns2] = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>new</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>NamespaceMap</font></font><font size=2>(prefix2, ns2);</p> <p>ResolveChildXmlNamespaces(markupProp);</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (contentProperty != </font><font color="#0000ff" size=2><font color="#0000ff" size=2>null</font></font><font size=2>)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (!(contentProperty.Value </font><font color="#0000ff" size=2><font color="#0000ff" size=2>is</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>String</font></font><font size=2>))</p> <p>{</p> <p>ResolveChildXmlNamespaces(contentProperty);</p> <p>}</p> <p>}</p> <p>}</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>private</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>void</font></font><font size=2> ResolveChildXmlNamespaces(</font><font color="#2b91af" size=2><font color="#2b91af" size=2>MarkupProperty</font></font><font size=2> markupProp)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (!markupProp.IsComposite)</p> <p>{</p> <p>ResolveXmlNamespaces(markupProp);</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</p></font></font><font size=2> <p>{</p> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>IList</font></font><font size=2> collection = markupProp.Value </font><font color="#0000ff" size=2><font color="#0000ff" size=2>as</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>IList</font></font><font size=2>;</p> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>IDictionary</font></font><font size=2> dictionary = markupProp.Value </font><font color="#0000ff" size=2><font color="#0000ff" size=2>as</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>IDictionary</font></font><font size=2>;</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (collection != </font><font color="#0000ff" size=2><font color="#0000ff" size=2>null</font></font><font size=2>)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>foreach</font></font><font size=2> (</font><font color="#0000ff" size=2><font color="#0000ff" size=2>object</font></font><font size=2> o </font><font color="#0000ff" size=2><font color="#0000ff" size=2>in</font></font><font size=2> collection)</p> <p>{</p> <p>ResolveXmlNamespaces(o);</p> <p>}</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (dictionary != </font><font color="#0000ff" size=2><font color="#0000ff" size=2>null</font></font><font size=2>)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>foreach</font></font><font size=2> (</font><font color="#0000ff" size=2><font color="#0000ff" size=2>object</font></font><font size=2> key </font><font color="#0000ff" size=2><font color="#0000ff" size=2>in</font></font><font size=2> dictionary.Keys)</p> <p>{</p> <p>ResolveXmlNamespaces(dictionary[key]);</p> <p>}</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</p></font></font><font size=2> <p>{</p> <p>ResolveXmlNamespaces(markupProp.Value);</p> <p>}</p> <p>}</p> <p>}</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>internal</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>class</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>NamespaceCache</p></font></font><font size=2> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>private</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>Dictionary</font></font><font size=2>&lt;</font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>, </font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>&gt; m_namespaceKeyCache = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>new</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>Dictionary</font></font><font size=2>&lt;</font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>, </font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>&gt;();</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>private</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>Dictionary</font></font><font size=2>&lt;</font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>, </font><font color="#2b91af" size=2><font color="#2b91af" size=2>Dictionary</font></font><font size=2>&lt;</font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>, </font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>&gt;&gt; m_assemblyNamespaces = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>new</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>Dictionary</font></font><font size=2>&lt;</font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>, </font><font color="#2b91af" size=2><font color="#2b91af" size=2>Dictionary</font></font><font size=2>&lt;</font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>, </font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>&gt;&gt;();</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>public</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>static</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>readonly</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> DefaultNamespace = </font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</font></font><font size=2>;</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>public</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>static</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>readonly</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> XmlNamespace = </font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;http://www.w3.org/XML/1998/namespace&quot;</font></font><font size=2>;</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>public</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>static</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>readonly</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> XamlNamespace = </font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</font></font><font size=2>;</p> <p></font><font color="#008000" size=2><font color="#008000" size=2>//private </p></font></font><font size=2> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>public</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> GetPrefixForNamespace(</font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> ns)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> ret = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>.Empty;</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (ns != DefaultNamespace &amp;&amp; m_namespaceKeyCache.ContainsKey(ns))</p> <p>{</p> <p>ret = m_namespaceKeyCache[ns];</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>return</font></font><font size=2> ret;</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>public</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> GetXmlNamespace(</font><font color="#2b91af" size=2><font color="#2b91af" size=2>Type</font></font><font size=2> type)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> uri = EnsureNamespaceMaps(type);</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (!m_namespaceKeyCache.ContainsKey(uri))</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> prefix = </font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;ns&quot;</font></font><font size=2> + (m_namespaceKeyCache.Count + 1).ToString();</p> <p>m_namespaceKeyCache[uri] = prefix;</p> <p>}</p> <p></font><font color="#008000" size=2><font color="#008000" size=2>//System.Diagnostics.Debug.WriteLine(&quot;Returned URI: &quot; + uri);</p></font></font><font size=2> <p></font><font color="#008000" size=2><font color="#008000" size=2>//string assemblyName = this.GetAssemblyNameFromType(type);</p></font></font><font size=2> <p></font><font color="#008000" size=2><font color="#008000" size=2>//if (assemblyName.Equals(&quot;PresentationFramework&quot;))</p></font></font><font size=2> <p></font><font color="#008000" size=2><font color="#008000" size=2>//if(uri.Equals(NamespaceCache.DefaultNamespace)</p></font></font><font size=2> <p></font><font color="#008000" size=2><font color="#008000" size=2>//{</p></font></font><font size=2> <p></font><font color="#008000" size=2><font color="#008000" size=2>// uri = DefaultNamespace;</p></font></font><font size=2> <p></font><font color="#008000" size=2><font color="#008000" size=2>//}</p></font></font><font size=2> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>return</font></font><font size=2> uri;</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>private</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> EnsureNamespaceMaps(</font><font color="#2b91af" size=2><font color="#2b91af" size=2>Type</font></font><font size=2> type)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> assemblyName = GetAssemblyNameFromType(type);</p> <p></font><font color="#008000" size=2><font color="#008000" size=2>//System.Diagnostics.Debug.WriteLine(&quot;Assembly Name: &quot; + assemblyName);</p></font></font><font size=2> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (!m_assemblyNamespaces.ContainsKey(type.Assembly.FullName))</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>foreach</font></font><font size=2> (</font><font color="#2b91af" size=2><font color="#2b91af" size=2>XmlnsPrefixAttribute</font></font><font size=2> attribute </font><font color="#0000ff" size=2><font color="#0000ff" size=2>in</font></font><font size=2> type.Assembly.GetCustomAttributes(</font><font color="#0000ff" size=2><font color="#0000ff" size=2>typeof</font></font><font size=2>(</font><font color="#2b91af" size=2><font color="#2b91af" size=2>XmlnsPrefixAttribute</font></font><font size=2>), </font><font color="#0000ff" size=2><font color="#0000ff" size=2>true</font></font><font size=2>))</p> <p>{</p> <p>m_namespaceKeyCache[attribute.XmlNamespace] = attribute.Prefix;</p> <p>}</p> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>Dictionary</font></font><font size=2>&lt;</font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>, </font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>&gt; temp = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>new</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>Dictionary</font></font><font size=2>&lt;</font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>, </font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>&gt;();</p> <p>m_assemblyNamespaces.Add(type.Assembly.FullName, temp);</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>foreach</font></font><font size=2> (</font><font color="#2b91af" size=2><font color="#2b91af" size=2>XmlnsDefinitionAttribute</font></font><font size=2> attribute2 </font><font color="#0000ff" size=2><font color="#0000ff" size=2>in</font></font><font size=2> type.Assembly.GetCustomAttributes(</font><font color="#0000ff" size=2><font color="#0000ff" size=2>typeof</font></font><font size=2>(</font><font color="#2b91af" size=2><font color="#2b91af" size=2>XmlnsDefinitionAttribute</font></font><font size=2>), </font><font color="#0000ff" size=2><font color="#0000ff" size=2>true</font></font><font size=2>))</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (attribute2.AssemblyName == </font><font color="#0000ff" size=2><font color="#0000ff" size=2>null</font></font><font size=2>)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (!attribute2.XmlNamespace.Contains(</font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;2007&quot;</font></font><font size=2>))</p> <p>{</p> <p>temp[attribute2.ClrNamespace] = attribute2.XmlNamespace;</p> <p>}</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>else</p></font></font><font size=2> <p>{</p> <p></font><font color="#008000" size=2><font color="#008000" size=2>//System.Diagnostics.Debug.WriteLine(&quot;Assembly Name: - &quot; + attribute2.AssemblyName);</p></font></font><font size=2> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>throw</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>new</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>ApplicationException</font></font><font size=2>(</font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;XmlnsDefinitions - Not handled yet.&quot;</font></font><font size=2>);</p> <p>}</p> <p></font><font color="#008000" size=2><font color="#008000" size=2>//System.Diagnostics.Debug.WriteLine(&quot;ClrNS - &quot; + attribute2.ClrNamespace);</p></font></font><font size=2> <p></font><font color="#008000" size=2><font color="#008000" size=2>//System.Diagnostics.Debug.WriteLine(&quot;XMLNS - &quot; + attribute2.XmlNamespace);</p></font></font><font size=2> <p>}</p> <p>}</p> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>Dictionary</font></font><font size=2>&lt;</font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>, </font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>&gt; namespaces = m_assemblyNamespaces[type.Assembly.FullName];</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (!namespaces.ContainsKey(type.Namespace))</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> uri = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>.Format(</font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;clr-namespace:{0};assembly={1}&quot;</font></font><font size=2>, type.Namespace, assemblyName);</p> <p>namespaces.Add(type.Namespace, uri);</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>return</font></font><font size=2> namespaces[type.Namespace];</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>public</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> GetAssemblyNameFromType(</font><font color="#2b91af" size=2><font color="#2b91af" size=2>Type</font></font><font size=2> type)</p> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> ret = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>.Empty;</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2>[] names = type.Assembly.FullName.Split(</font><font color="#a31515" size=2><font color="#a31515" size=2>','</font></font><font size=2>);</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (names.Length &gt; 0)</p> <p>{</p> <p>ret = names[0];</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>return</font></font><font size=2> ret;</p> <p>}</p> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>internal</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>class</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>NamespaceMap</p></font></font><font size=2> <p>{</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>public</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> Prefix;</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>public</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> XmlNamespace;</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>public</font></font><font size=2> NamespaceMap(</font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> prefix, </font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> xmlNamespace)</p> <p>{</p> <p>Prefix = prefix;</p> <p>XmlNamespace = xmlNamespace;</p> <p>}</p> <p>}</p> <p>}</p></font>Thu, 04 Sep 2008 23:57:45 Z2008-09-04T23:57:45Z