<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title>dentedtriangle.com</title>
  <link href="http://dentedtriangle.com/"/>
  <link type="application/atom+xml" rel="self" href="http://dentedtriangle.com/atom.xml"/>
  <updated>2011-11-04T17:43:29+08:00</updated>
  <id>http://dentedtriangle.com/</id>
  <author>
    <name>Dented Triangle</name>
    <email>graham@dentedtriangle.com</email>
  </author>

  
  <entry>
    <id>http://dentedtriangle.com/First-of-many</id>
    <link type="text/html" rel="alternate" href="http://dentedtriangle.com/First-of-many"/>
    <title>First of many...hopefully</title>
    <updated>2011-11-02T00:00:00+08:00</updated>
    <author>
      <name>Dented Triangle</name>
      <uri>http://dentedtriangle.com/</uri>
    </author>
    <content type="html">&lt;p&gt;So I've been thinking about what to write about, there are so many topics that people want to know more of and there are just small snippets I find useful throughout my day. I'm going to start working on the below topics with hopefully some in depth video showing you some solid examples. The video might come later, but I'll at least get some of these out soon.&lt;/p&gt;&lt;dl&gt;
   &lt;dt&gt;Topics&lt;/dt&gt;
   &lt;dd&gt;LINQ&lt;/dd&gt;
   &lt;dd&gt;Lambda's&lt;/dd&gt;
   &lt;dd&gt;Dependancy Injection&lt;/dd&gt;
   &lt;dd&gt;Test Driven Development&lt;/dd&gt;
   &lt;dd&gt;Behavior Driven Development&lt;/dd&gt;
   &lt;dd&gt;How to Design a Database&lt;/dd&gt;
   &lt;dd&gt;ASP.NET MVC where to begin&lt;/dd&gt;
   &lt;dd&gt;How to start XNA Development&lt;/dd&gt;
   &lt;dd&gt;Daily Snippets&lt;/dd&gt;
   &lt;dd&gt;Podcast&lt;/dd&gt;
&lt;/dl&gt;&lt;dl&gt;
   &lt;dt&gt;Projects&lt;/dt&gt;
   &lt;dd&gt;Portal System&lt;/dd&gt;
   &lt;dd&gt;Learn Cantonese&lt;/dd&gt;
&lt;/dl&gt;&lt;p&gt;I guess the best way to start is in order I listed above while I work on my projects. I'll most certainly touch on different topics while I write, but that's okay since I'm sure it'll spark new thoughts and interests in articles.&lt;/p&gt;</content>
  </entry>
  
  <entry>
    <id>http://dentedtriangle.com/How-to-use-xml-in-.NET</id>
    <link type="text/html" rel="alternate" href="http://dentedtriangle.com/How-to-use-xml-in-.NET"/>
    <title>How to use XML in .NET</title>
    <updated>2009-10-01T00:00:00+08:00</updated>
    <author>
      <name>Dented Triangle</name>
      <uri>http://dentedtriangle.com/</uri>
    </author>
    <content type="html">&lt;p&gt;
   You all know what XML is, but for most people they don't know how to use XML in
   the most efficient way. Since we are dealing with XML more and more and larger sizes
   of it each time, it was about time to ensure that we optimize how we use the XML
   library in .NET. I'm not going to get into designing of XML as that can be done
   in a later how to. What this talk is going to be about is what XML objects to use
   given the situation and why.&lt;/p&gt;&lt;p&gt;
   For the most part there comes down to 4 basic objects that you will have to decide
   between when using XML.&lt;/p&gt;&lt;ul&gt;
   &lt;li&gt;System.Xml.&lt;strong&gt;XmlReader&lt;/strong&gt; this pull model XML parsers, such as the
      &lt;strong&gt;XmlReader&lt;/strong&gt;, operate in a forward only, streaming fashion while only
      showing information about a single node at any given time. This doesn't require
      the entire document to be loaded. If &lt;em&gt;performance&lt;/em&gt; is your highest priority
      but you don't require XSD validation, XSD type information (i.e. Named Properties)
      and you don't need XSLT use this. &lt;/li&gt;
   &lt;li&gt;System.Xml.XPath.&lt;strong&gt;XPathNavigator&lt;/strong&gt; this is used in conjunction
      with &lt;strong&gt;XPathDocument&lt;/strong&gt; most of the time but doesn't have to be. This
      also doesn't require the entire document to be loaded. If you require XSLT and read only.
      You can read the XML in any direction at any time. &lt;/li&gt;
   &lt;li&gt;System.Xml.&lt;strong&gt;XmlDocument&lt;/strong&gt; this is a &lt;strong&gt;DOM&lt;/strong&gt; (Document
      Object Model) representation of the XML data. This requires the entire XML document
      to be loaded. This method is used if you are looking to read/write to the XML.
   &lt;/li&gt;
   &lt;li&gt;System.&lt;strong&gt;LINQ&lt;/strong&gt; the newest and most friendly way to query an XML
      document if &lt;strong&gt;LINQ&lt;/strong&gt;. It provides robust ways to query for data, easy
      and understandable logic and &lt;em&gt;Language Integrated Queries&lt;/em&gt;. &lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;
   Now saying these objects will probably remind you of how use used one of these and
   what purpose you used it for. There are certain situations that it are more suited
   for each object above. Yes you can use any of the objects and they will solve what
   you're doing but possibly done better with another.&lt;/p&gt;&lt;p&gt;
   System.Xml.&lt;strong&gt;XmlReader&lt;/strong&gt;&lt;/p&gt;&lt;h3&gt;
   Where should you use XmlReader?&lt;/h3&gt;&lt;p&gt;
   Methods is a prime example on when to use &lt;strong&gt;XmlReader. &lt;/strong&gt;When passing
   in XML you have to choose what you're going to do with it. If you're just looking
   at reading the XML or passing it off to the next layer then you should be using
   &lt;strong&gt;XmlReader&lt;/strong&gt; or &lt;strong&gt;XPathNavigator&lt;/strong&gt;.&lt;/p&gt;&lt;pre class='brush: xml;'&gt;
&lt;items&gt;
   &lt;compact-disc&gt;
      &lt;price&gt;16.95&lt;/price&gt;
      &lt;artist&gt;Nelly&lt;/artist&gt;
      &lt;title&gt;Nellyville&lt;/title&gt;
   &lt;/compact-disc&gt;
   &lt;compact-disc&gt;
      &lt;price&gt;17.55&lt;/price&gt;
      &lt;artist&gt;Baby D&lt;/artist&gt;
      &lt;title&gt;Lil Chopper Toy&lt;/title&gt;
   &lt;/compact-disc&gt;
&lt;/items&gt;
&lt;/pre&gt;&lt;pre class='brush: csharp;'&gt;
 public static void PrintArtistAndPrice(XmlReader reader)
 {
    reader.MoveToContent();
    while(reader.Read())
    {
       if((reader.NodeType == XmlNodeType.Element) &amp;amp;&amp;amp; reader.Name.Equals(&quot;artist&quot;))
       {
          artist = reader.ReadElementString();
          title  = reader.ReadElementString();
          break;
       }
    }
    Console.WriteLine(&quot;Artist={0}, Title={1}&quot;, artist, title);
 }
&lt;/pre&gt;&lt;p&gt;
   As with the same for return types, if you know the XML will be edited then return
   &lt;strong&gt;XmlDocument&lt;/strong&gt; otherwise return &lt;strong&gt;XmlReader&lt;/strong&gt; or &lt;strong&gt;
      XPathNavigator &lt;/strong&gt;(depending on if the data will require streaming or forward-only
   access).&lt;/p&gt;&lt;p&gt;
   System.Xml.&lt;strong&gt;XPathNavigator&lt;/strong&gt;&lt;/p&gt;&lt;h3&gt;
   Where to use XPathNavigator?&lt;/h3&gt;&lt;p&gt;
   As with &lt;strong&gt;XmlReader&lt;/strong&gt; you'll want to use &lt;strong&gt;XPathNavigator &lt;/strong&gt;
   when dealing with methods. In keeping with the same example above you can see how
   to use &lt;strong&gt;XPathNavigator&lt;/strong&gt;.&lt;/p&gt;&lt;pre class='brush: csharp;'&gt;
 public static void PrintArtistAndPrice(XPathNavigator nav)
 {
    XPathNodeIterator iterate = nav.Select(&quot;/items/compact-disc[1]/artist/items/compact-disc[1]/title&quot;);
    iterator.MoveNext();
    Console.WriteLine(&quot;Artist={0}&quot;, iterator.Current);
    iterator.MoveNext();
    Console.WriteLine(&quot;Title={0}&quot;, iterator.Current);
 }
&lt;/pre&gt;&lt;p&gt;
   So why use &lt;strong&gt;XPathNavigator&lt;/strong&gt; over &lt;strong&gt;XmlReader&lt;/strong&gt;? The
   biggest reason is for &lt;strong&gt;XPath&lt;/strong&gt; support as well as being able to iterate
   through the document in multiple directions.&lt;/p&gt;&lt;p&gt;System.Xml.&lt;strong&gt;XmlDocument&lt;/strong&gt;&lt;/p&gt;&lt;h3&gt;Where to use XmlDocument?&lt;/h3&gt;&lt;p&gt;
   XmlDocument is used primarily for reading/writing to an Xml file. If you are looking
   to write back to a file or need the document validated for consistency this is what
   you want to use. &lt;strong&gt;XmlDocument&lt;/strong&gt; will only load the entire document
   unlike the other readers that will load partial files up to an error. &lt;strong&gt;XmlDocument&lt;/strong&gt;
   also supports &lt;strong&gt;XPath&lt;/strong&gt; navigation which allows for quick navigation
   to any given element at any time. This is the most used Xml type object, but can
   be the most expensive in memory but is often overlooked because of it's productivity
   and ease of use.&lt;/p&gt;&lt;h3&gt;
   Generating XML&lt;/h3&gt;&lt;p&gt;
   Majority of the time you could use &lt;strong&gt;XmlSerializer&lt;/strong&gt; but in some cases
   you want to control the namespaces and structure so you can use &lt;strong&gt;XmlDocument&lt;/strong&gt;.
   If you want to write Xml to an &lt;strong&gt;XmlDocument&lt;/strong&gt; you should use &lt;strong&gt;
      XmlNodeWriter&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;
&lt;strong&gt;XSLT Transformation&lt;/strong&gt;&lt;/p&gt;&lt;pre class='brush: csharp;'&gt;
private void Load_Document()
{
   System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
   doc.Load(Server.MapPath(&quot;MySource.Xml&quot;));
   System.Xml.Xsl.XslTransform trans = newSystem.Xml.Xsl.XslTransform();
   trans.Load(Server.MapPath(&quot;MyStyle.xsl&quot;));
   Xml1.Document = doc;
   Xml1.Transform = trans;
}
&lt;/pre&gt;&lt;strong&gt;XPath&lt;/strong&gt;&lt;pre class='brush: xml;'&gt;
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;Books&gt;
   &lt;Book&gt;
      &lt;Title&gt;Beginning XML&lt;/Title&gt;
      &lt;Publisher&gt;Wrox&lt;/Publisher&gt;
   &lt;/Book&gt;
   &lt;Book&gt;
      &lt;Title&gt;XML Step by Step&lt;/Title&gt;
      &lt;Publisher&gt;MSPress&lt;/Publisher&gt;
   &lt;/Book&gt;
   &lt;Book&gt;
      &lt;Title&gt;Professional XML&lt;/Title&gt;
      &lt;Publisher&gt;Wrox&lt;/Publisher&gt;
   &lt;/Book&gt;
   &lt;Book&gt;
      &lt;Title&gt;Developing XML solutions&lt;/Title&gt;
      &lt;Publisher&gt;MSPress&lt;/Publisher&gt;
   &lt;/Book&gt;
&lt;/Books&gt;
&lt;/pre&gt;&lt;pre class='brush: csharp;'&gt;
System.Xml.XmlDocument Xmldoc = new System.Xml.XmlDocument();
Xmldoc.Load(&quot;C:\\books.Xml&quot;);
System.Xml.XmlNodeList MSPressBookList = Xmldoc.SelectNodes(&quot;//Publisher[. = 'MSPress']/parent::node()/Title&quot;);
foreach (System.Xml.XmlNode MSPressBook in MSPressBookList)
{
   System.Diagnostics.Debug.WriteLine(MSPressBook.InnerText);
}
System.Xml.XmlNode bookNode = Xmldoc.SelectSingleNode(&quot;//Title[.='XML Step by Step']&quot;);
if (!(bookNode == null))
{
   System.Diagnostics.Debug.WriteLine(&quot;Located title 'XML Step by Step'&quot;);
}
else
{
   System.Diagnostics.Debug.WriteLine(&quot;Could not locate title 'XML Step by Step'&quot;);
}
&lt;/pre&gt;&lt;p&gt;
   System.&lt;strong&gt;LINQ&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;
   The great thing about &lt;strong&gt;LINQ&lt;/strong&gt; is everyone usually knows it, they might
   not know every trick or the exact syntax but it follows the structure of &lt;strong&gt;SQL&lt;/strong&gt;.
   The ability to use &lt;strong&gt;LINQ&lt;/strong&gt; on Xml files allows you to have more transparent
   queries which can be used on a more range of Xml files. The biggest advantage of
   using &lt;strong&gt;LINQ&lt;/strong&gt; to query your Xml is &lt;strong&gt;Language Integrated Query&lt;/strong&gt;.
   &lt;strong&gt;LINQ&lt;/strong&gt; also allows for the return type to be Enumerable which allows
   you to put a &lt;strong&gt;foreach&lt;/strong&gt; or &lt;strong&gt;while&lt;/strong&gt; loop with better
   object representation.&lt;/p&gt;&lt;pre class='brush: xml;'&gt;
&lt;contacts&gt;
   &lt;contact&gt;
      &lt;name&gt;Patrick Hines&lt;/name&gt;
      &lt;phone type='home'&gt;206-555-0144&lt;/phone&gt;
      &lt;phone type='work'&gt;425-555-0145&lt;/phone&gt;
      &lt;address&gt;
         &lt;address&gt;
            &lt;state&gt;WA&lt;/state&gt;
         &lt;/address&gt;
      &lt;/address&gt;
   &lt;/contact&gt;
   &lt;contact&gt;
      &lt;name&gt;Gretchen Rivas&lt;/name&gt;
      &lt;address&gt;
         &lt;address&gt;
            &lt;state&gt;WA&lt;/state&gt;
         &lt;/address&gt;
      &lt;/address&gt;
   &lt;/contact&gt;
   &lt;contact&gt;
      &lt;name&gt;Scott MacDonald&lt;/name&gt;
      &lt;phone type='home'&gt;925-555-0134&lt;/phone&gt;
      &lt;phone type='mobile'&gt;425-555-0177&lt;/phone&gt;
      &lt;address&gt;
         &lt;address&gt;
            &lt;state&gt;CA&lt;/state&gt;
         &lt;/address&gt;
      &lt;/address&gt;
   &lt;/contact&gt;
&lt;/contacts&gt;
&lt;/pre&gt;&lt;pre class='brush: csharp;'&gt;
XElement contactsFromFile = XElement.Load(@&quot;c:\myContactList.xml&quot;);
foreach (XElement x in contactsFromFile.Elements(&quot;contact&quot;).Elements())
{
   Console.WriteLine(x);
}
XElement contacts = new XElement(&quot;contacts&quot;,
   from c in contactsFromFile.Elements(&quot;contact&quot;)
   select new XElement(&quot;contact&quot;, c.Element(&quot;name&quot;),
      c.Element(&quot;phone&quot;)));
static XElement GetPhoneNumbers(XElement c)
{
   if (c.Elements(&quot;phone&quot;).Any())
   {
      return new XElement(&quot;phoneNumbers&quot;, c.Elements(&quot;phone&quot;));
   }
   else
   {
      return null;
   }
}
contactsFromFile.Descendants(&quot;phone&quot;).Attributes(&quot;type&quot;).Select(t =&gt; t.Value).Distinct();
&lt;/pre&gt;
&lt;p&gt;Recommendations &lt;ul&gt;
   &lt;li&gt;When returning Xml Data return an &lt;strong&gt;XmlReader&lt;/strong&gt; &lt;/li&gt;
   &lt;li&gt;Use &lt;strong&gt;LINQ&lt;/strong&gt; to Xml with basically everything else as it gives you
      the same robustness but it's light weight compared to &lt;strong&gt;XmlDocument&lt;/strong&gt;
      and more OO then &lt;strong&gt;XPath&lt;/strong&gt; &lt;/li&gt;
&lt;/ul&gt;&lt;/p&gt;</content>
  </entry>
  

</feed>
