<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Roy Triesscheijn’s Weblog &#187; Content Pipeline</title>
	<atom:link href="http://roy-t.nl/index.php/tag/content-pipeline/feed/" rel="self" type="application/rss+xml" />
	<link>http://roy-t.nl</link>
	<description>My programming world</description>
	<lastBuildDate>Wed, 07 Dec 2011 23:19:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>XNA XML ContentImporter</title>
		<link>http://roy-t.nl/index.php/2010/08/07/xna-xml-contentimporter/</link>
		<comments>http://roy-t.nl/index.php/2010/08/07/xna-xml-contentimporter/#comments</comments>
		<pubDate>Sat, 07 Aug 2010 11:09:43 +0000</pubDate>
		<dc:creator>Roy Triesscheijn</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[General Coding]]></category>
		<category><![CDATA[General Gamedesign]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[Content Pipeline]]></category>
		<category><![CDATA[ContentImporter]]></category>
		<category><![CDATA[ContentTypeReader]]></category>
		<category><![CDATA[ContentTypeWriter]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://roy-t.nl/?p=358</guid>
		<description><![CDATA[Quite often you&#8217;d like to use XML files in your games. You can use the standard XNA XML Importer for this, however then you&#8217;d need to write specific class that serialize to and from the special XNA XML documents that have a &#60;XNAContent&#62; tree. If you&#8217;re trying to build your levels in XML or if [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://roy-t.nl/index.php/2010/08/07/xna-xml-contentimporter/' addthis:title='XNA XML ContentImporter' ><a class="addthis_button_twitter"></a><a class="addthis_button_stumbleupon"></a><a class="addthis_button_reddit"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Quite often you&#8217;d like to use XML files in your games. You can use the standard XNA XML Importer for this, however then you&#8217;d need to write specific class that serialize to and from the special XNA XML documents that have a &lt;XNAContent&gt; tree.</p>
<p>If you&#8217;re trying to build your levels in XML or if you&#8217;re trying to set a high volume of settings this might not be the easiest way. Easier would be to just parse a hand written XML document yourself. Luckily this is very easy to accomplish.</p>
<p>The naive way of doing this is by just writing a custom content importer that would look similar to this:</p>
<pre class="brush: csharp; title: ; notranslate">
[ContentImporter(&quot;.xml&quot;, DisplayName = &quot;XML Content Importer&quot;, DefaultProcessor = &quot;None&quot;)]  //TImport was defined as System.Xml.XmlDocument a bit higher
    public class XMLContentImporter : ContentImporter&lt;TImport&gt;
    {
        public override TImport Import(string filename, ContentImporterContext context)
        {
            XmlDocument document = new XmlDocument();
            try
            {
                document.Load(filename);
            }
            catch (Exception e)
            {
                //Write error logger here or update the exception with more information
                throw e;
            }
            return document;
        }
    }
</pre>
<p>(Don&#8217;t forget to add a reference to your content pipeline project to the<strong> Content project inside your game&#8217;s project</strong>)</p>
<p>Now the following code doesn&#8217;t work and will throw an error similar to this:</p>
<pre class="brush: plain; title: ; notranslate">
Error   1   Building content threw InvalidOperationException: Cyclic reference found while serializing System.Xml.XmlDocument. You may be missing a ContentSerializerAttribute.SharedResource flag.
</pre>
<p>Now I&#8217;m not going to be holier than the pope, I actually made the same error as you can blatantly see <a href="http://forums.xna.com/forums/p/58481/358409.aspx">here</a>.</p>
<p>Unfortunately the error message is very unhelpful in this case, and might even throw you off completely since the error is not visual from the actual content file itself. Any XML file while give you this error.</p>
<p>Luckily user Saw was sitting around in #XNA on Efnet (IRC). He told me that the error comes from the fact that I&#8217;ve written no ContentWriter (and ContentReader). This means that reflection is used to serialize the XML document.</p>
<p>An XmlDocument has a lot of XMLNodes. An XmlNode has the member variable parent, which is an XmlNode, which has the member variable &#8216;parent&#8217;, which&#8230;. Well you get the idea. Probably the recursion error comes from there.</p>
<p>He then showed me how two write a very simple reader and writer. If we extend our content importer with these two extra classes this problem is easily solved.</p>
<pre class="brush: csharp; title: ; notranslate">
[ContentTypeWriter()]
    public class CustomXmlWriter : ContentTypeWriter&lt;XmlDocument&gt;
    {
        protected override void Write(ContentWriter output, XmlDocument value)
        {
            value.Save(output.BaseStream);
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return typeof(CustomXmlReader).AssemblyQualifiedName;
        }
    }

    public class CustomXmlReader : ContentTypeReader&lt;XmlDocument&gt;
    {
        protected override XmlDocument Read(ContentReader input, XmlDocument existingInstance)
        {
            if (existingInstance == null)
            {
                existingInstance = new XmlDocument();
            }
            existingInstance.Load(input.BaseStream);
            return existingInstance;
        }
    }
</pre>
<p>The custom content writer just uses the XMLDocuments built in Save function which is aware of these cycles and deals with them properly, the other method is overload to tell XNA which content reader to use for this type. The content reader again uses the simple built in Load function, nothing special here.</p>
<p>However now compile your project again, see the errors are gone <img src='http://roy-t.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Let&#8217;s add a simple XML file to your content project, I&#8217;ve named it hello.xml.</p>
<pre class="brush: xml; title: ; notranslate">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;MyTag&gt;Hello World!&lt;/MyTag&gt;
</pre>
<p>Click the file in the solution explorer and set the build action to &#8216;Compile&#8217; and the Content Processor to &#8216;XML Content Importer&#8217; (you can change the name by changing the DisplayName attribute). Note: you might need to recompile before you can do this.</p>
<p>Load and display it like this:</p>
<pre class="brush: csharp; title: ; notranslate">
XmlDocument xml = Content.Load&lt;XmlDocument&gt;(@&quot;Tests\xmltest&quot;);
XmlNode node = xml.SelectSingleNode(&quot;//MyTag&quot;);
Console.Out.WriteLine(node.InnerText);
</pre>
<p>The expression &#8220;//MyTag&#8221; is an XPATH expression to query the XML document, you can learn more about that <a href="http://www.w3schools.com/xpath/xpath_syntax.asp">here</a>.</p>
<p>I hope you have a lot of fun with this code, and that it makes life a little bit easier for you, either by handling XML files for you, or by being a small source on how to write custom ContentTypeWriters and readers.</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://roy-t.nl/index.php/2010/08/07/xna-xml-contentimporter/' addthis:title='XNA XML ContentImporter' ><a class="addthis_button_twitter"></a><a class="addthis_button_stumbleupon"></a><a class="addthis_button_reddit"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://roy-t.nl/index.php/2010/08/07/xna-xml-contentimporter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tutorial: Using the XNA Content Pipeline for localization: Part 2, Assets</title>
		<link>http://roy-t.nl/index.php/2010/02/20/tutorial-using-the-xna-content-pipeline-for-localization-part-2-assets/</link>
		<comments>http://roy-t.nl/index.php/2010/02/20/tutorial-using-the-xna-content-pipeline-for-localization-part-2-assets/#comments</comments>
		<pubDate>Sat, 20 Feb 2010 11:31:18 +0000</pubDate>
		<dc:creator>Roy Triesscheijn</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[General Gamedesign]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[Assets]]></category>
		<category><![CDATA[Content Pipeline]]></category>
		<category><![CDATA[ContentManager]]></category>
		<category><![CDATA[localization]]></category>
		<category><![CDATA[XNABabylon]]></category>

		<guid isPermaLink="false">http://roy-t.nl/?p=281</guid>
		<description><![CDATA[(read about part 1 here) SgtConker has uploaded the second part of my tutorial on localization. This time we automatically localize assets and the code is going to make a lot more sense. (The way strings where localized might look a bit odd without seeing part 2). Anyway without further introduction, you can find part [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://roy-t.nl/index.php/2010/02/20/tutorial-using-the-xna-content-pipeline-for-localization-part-2-assets/' addthis:title='Tutorial: Using the XNA Content Pipeline for localization: Part 2, Assets' ><a class="addthis_button_twitter"></a><a class="addthis_button_stumbleupon"></a><a class="addthis_button_reddit"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>(read about part 1 <a href="http://roy-t.nl/index.php/2010/01/30/tutorial-using…part-1-strings/">here</a>)</p>
<p>SgtConker has uploaded the second part of my tutorial on localization. This time we automatically localize assets and the code is going to make a lot more sense. (The way strings where localized might look a bit odd without seeing part 2).</p>
<p>Anyway without further introduction, you can find part 2 at sgtconker.com <a href="http://www.sgtconker.com/2010/02/article-using-xna-content-pipeline-extensions-for-localization-part-2/">here</a>.</p>
<p><img src="http://www.sgtconker.com/wp-content/uploads/2010/02/flag.png" alt="First screen" /></p>
<p><a href="http://www.gamedevkicks.com/kick/?url=http%3a%2f%2froy-t.nl%2findex.php%2f2010%2f02%2f20%2ftutorial-using-the-xna-content-pipeline-for-localization-part-2-assets%2f"><img src="http://www.gamedevkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2froy-t.nl%2findex.php%2f2010%2f02%2f20%2ftutorial-using-the-xna-content-pipeline-for-localization-part-2-assets%2f" border="0" alt="kick it on GameDevKicks.com" /></a></p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://roy-t.nl/index.php/2010/02/20/tutorial-using-the-xna-content-pipeline-for-localization-part-2-assets/' addthis:title='Tutorial: Using the XNA Content Pipeline for localization: Part 2, Assets' ><a class="addthis_button_twitter"></a><a class="addthis_button_stumbleupon"></a><a class="addthis_button_reddit"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://roy-t.nl/index.php/2010/02/20/tutorial-using-the-xna-content-pipeline-for-localization-part-2-assets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tutorial: Using the XNA Content Pipeline for localization: Part 1, Strings</title>
		<link>http://roy-t.nl/index.php/2010/01/30/tutorial-using-the-xna-content-pipeline-for-localization-part-1-strings/</link>
		<comments>http://roy-t.nl/index.php/2010/01/30/tutorial-using-the-xna-content-pipeline-for-localization-part-1-strings/#comments</comments>
		<pubDate>Sat, 30 Jan 2010 17:20:21 +0000</pubDate>
		<dc:creator>Roy Triesscheijn</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[General Gamedesign]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[Content]]></category>
		<category><![CDATA[Content Manager]]></category>
		<category><![CDATA[Content Pipeline]]></category>
		<category><![CDATA[localization]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[XNABabylon]]></category>

		<guid isPermaLink="false">http://roy-t.nl/?p=264</guid>
		<description><![CDATA[Lately I&#8217;ve been working on making localization easier. I came up with an idea to use the content pipeline and content manager to automatically localize your strings*, textures and sounds (and any other content type you might want to localize). In part one we create a new content pipeline extension and a new content manager [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://roy-t.nl/index.php/2010/01/30/tutorial-using-the-xna-content-pipeline-for-localization-part-1-strings/' addthis:title='Tutorial: Using the XNA Content Pipeline for localization: Part 1, Strings' ><a class="addthis_button_twitter"></a><a class="addthis_button_stumbleupon"></a><a class="addthis_button_reddit"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Lately I&#8217;ve been working on making localization easier. I came up with an idea to use the content pipeline and content manager to automatically localize your strings*, textures and sounds (and any other content type you might want to localize).</p>
<p>In part one we create a new content pipeline extension and a new content manager without much effort. And we start to replace our strings with calls to our new content manager (that replaces the default one). In the end you will be able to load a string using Content.Load&lt;String&gt;(&#8220;Package.Key&#8221;); The correct string will be loaded from our xnb files (imported using a custom xml importer) in the language set in the content manager.</p>
<p>In part two we are going to extend this example to load other content as normal (Content.Load&lt;Texture2D&gt;(@&#8221;Folder\Name&#8221;);). And if you&#8217;ve created a localized version this will be loaded instead of the original file.</p>
<p>Part 1 is now up at SgtConker.Com: <a href="http://www.sgtconker.com/2010/01/article-using-xna-content-pipeline-extensions-for-localization/">http://www.sgtconker.com/2010/01/article-using-xna-content-pipeline-extensions-for-localization/</a></p>
<p>Feel free to post any comments and questions either here or at sgtconker.com.</p>
<p>Part 2 will probably be up end of next week.</p>
<p><a href="http://www.gamedevkicks.com/kick/?url=http%3a%2f%2froy-t.nl%2findex.php%2f2010%2f01%2f30%2ftutorial-using-the-xna-content-pipeline-for-localization-part-1-strings%2f"><img src="http://www.gamedevkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2froy-t.nl%2findex.php%2f2010%2f01%2f30%2ftutorial-using-the-xna-content-pipeline-for-localization-part-1-strings%2f" border="0" alt="kick it on GameDevKicks.com" /></a></p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://roy-t.nl/index.php/2010/01/30/tutorial-using-the-xna-content-pipeline-for-localization-part-1-strings/' addthis:title='Tutorial: Using the XNA Content Pipeline for localization: Part 1, Strings' ><a class="addthis_button_twitter"></a><a class="addthis_button_stumbleupon"></a><a class="addthis_button_reddit"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://roy-t.nl/index.php/2010/01/30/tutorial-using-the-xna-content-pipeline-for-localization-part-1-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

