title: How to deserialize only part of a large xml file to c# classes?
body: <p>I've already read some posts and articles on how to deserialize xml but still haven't figured out the way I should write the code to match my needs, so.. I'm apologizing for another question about deserializing xml )) </p>
  
  <p>I have a large (50 MB) xml file which I need to deserialize. I use xsd.exe to get xsd schema of the document and than autogenerate c# classes file which I put into my project. I want to get some (not all) data from this xml file and put it into my sql database. </p>
  
  <p>Here is the hierarchy of the file (simplified, xsd is very large):  </p>
  
  <pre><code>public class yml_catalog 
  {
      public yml_catalogShop[] shop { /*realization*/ }
  }
  
  public class yml_catalogShop
  {
      public yml_catalogShopOffersOffer[][] offers { /*realization*/ }
  }
  
  public class yml_catalogShopOffersOffer
  {
      // here goes all the data (properties) I want to obtain ))
  }
  </code></pre>
  
  <p>And here is my code:  </p>
  
  <p><b>first approach:</b>  </p>
  
  <pre><code>yml_catalogShopOffersOffer catalog;
  var serializer = new XmlSerializer(typeof(yml_catalogShopOffersOffer));
  var reader = new StreamReader(@"C:\div_kid.xml");
  catalog = (yml_catalogShopOffersOffer) serializer.Deserialize(reader);//exception occures
  reader.Close();
  </code></pre>
  
  <p>I get InvalidOperationException: There is an error in the XML(3,2) document</p>
  
  <p><b>second approach:</b></p>
  
  <pre><code>XmlSerializer ser = new XmlSerializer(typeof(yml_catalogShopOffersOffer));
  yml_catalogShopOffersOffer result;
  using (XmlReader reader = XmlReader.Create(@"C:\div_kid.xml"))          
  {
      result = (yml_catalogShopOffersOffer)ser.Deserialize(reader); // exception occures
  }
  </code></pre>
  
  <p>InvalidOperationException: There is an error in the XML(0,0) document</p>
  
  <p><b>third: </b>I tried to deserialize the entire file: </p>
  
  <pre><code> XmlSerializer ser = new XmlSerializer(typeof(yml_catalog)); // exception occures
   yml_catalog result;
   using (XmlReader reader = XmlReader.Create(@"C:\div_kid.xml"))          
   {
       result = (yml_catalog)ser.Deserialize(reader);
   }
  </code></pre>
  
  <p>And I get the following: </p>
  
  <pre><code>error CS0030: The convertion of type "yml_catalogShopOffersOffer[]" into "yml_catalogShopOffersOffer" is not possible.
  
  error CS0029: The implicit convertion of type "yml_catalogShopOffersOffer" into "yml_catalogShopOffersOffer[]" is not possible.
  </code></pre>
  
  <p>So, how to fix (or overwrite) the code to not get the exceptions?</p>
  
  <p><b>edits: </b> Also when I write:    </p>
  
  <pre><code>XDocument doc = XDocument.Parse(@"C:\div_kid.xml");
  </code></pre>
  
  <p>The XmlException occures: unpermitted data on root level, string 1, position 1.</p>
  
  <p>Here is the first string of the xml file: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="windows-1251"?&gt;
  </code></pre>
  
  <p><b>edits 2:</b>
  The xml file short example:  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="windows-1251"?&gt;
  &lt;!DOCTYPE yml_catalog SYSTEM "shops.dtd"&gt;
  &lt;yml_catalog date="2012-11-01 23:29"&gt;
  &lt;shop&gt;
     &lt;name&gt;OZON.ru&lt;/name&gt;
     &lt;company&gt;?????? "???????????????? ??????????????"&lt;/company&gt;
     &lt;url&gt;http://www.ozon.ru/&lt;/url&gt;
     &lt;currencies&gt;
       &lt;currency id="RUR" rate="1" /&gt;
     &lt;/currencies&gt;
     &lt;categories&gt;
        &lt;category id=""1126233&gt;base category&lt;/category&gt;
        &lt;category id="1127479" parentId="1126233"&gt;bla bla bla&lt;/category&gt;
        // here goes all the categories
     &lt;/categories&gt;
     &lt;offers&gt;
        &lt;offer&gt;
           &lt;price&gt;&lt;/price&gt;
           &lt;picture&gt;&lt;/picture&gt;
        &lt;/offer&gt;
        // other offers
     &lt;/offers&gt;
  &lt;/shop&gt;
  &lt;/yml_catalog&gt;
  </code></pre>
  
  <p><b>P.S.</b>
  I've already acccepted the answer (it's perfect). But now I need to find "base category" for each Offer using categoryId. The data is hierarchical and the base category is the category that has no "parentId" attribute. So, I wrote a recursive method to find the "base category", but it never finishes. Seems like the algorythm is not very fast))<br>
  Here is my code: (in the main() method)</p>
  
  <pre><code>var doc = XDocument.Load(@"C:\div_kid.xml");
  var offers = doc.Descendants("shop").Elements("offers").Elements("offer");
  foreach (var offer in offers.Take(2))
          {
              var category = GetCategory(categoryId, doc);
              // here goes other code
          }
  </code></pre>
  
  <p>Helper method: </p>
  
  <pre><code>public static string GetCategory(int categoryId, XDocument document)
      {
          var tempId = categoryId;
              var categories = document.Descendants("shop").Elements("categories").Elements("category");
              foreach (var category in categories)
              {
                  if (category.Attribute("id").ToString() == categoryId.ToString())
                  {
                      if (category.Attributes().Count() == 1)
                      {
                          return category.ToString();
                      }
                      tempId = Convert.ToInt32(category.Attribute("parentId"));
                  }
              }
          return GetCategory(tempId, document);
      }
  </code></pre>
  
  <p>Can I use recursion in such situation? If not, how else can I find the "base category"?</p>