title: XDocument for .Net 2.0?
body: <p>I have some .Net 3.5 code that uses the <code>XDocument</code> class.</p>
  
  <p>Can you suggest a quick and dirty way to get this code running in .Net 2.0?</p>
  
  <pre><code>    public static HaarClassifierCascade Parse(XDocument xDoc)
      {
          HaarClassifierCascade cascade = null;
          XElement stages_fn;
          XElement seq_fn = null; /* sequence */
          XElement fn;
          int n;
          int i = 0, j = 0, k = 0, l = 0;
          int parent, next;
  
          stages_fn = xDoc.Descendants(stageId).First();
  
          n = stages_fn.Elements().Count();
          cascade = new HaarClassifierCascade(n);
  
          seq_fn = xDoc.Descendants(sizeId).First();
          string[] size = seq_fn.Value.Split(' ');
          int.TryParse(size[0], out cascade.OriginalWindowSize.Width);
          int.TryParse(size[1], out cascade.OriginalWindowSize.Height);
  
          XElement stage_fn = (XElement)stages_fn.FirstNode;
          while (null != stage_fn)
          {
              XElement trees_fn = stage_fn.Element(treeId);
  
              n = trees_fn.Elements().Count();
              cascade.StageClassifiers[i].Classifiers = new List&lt;HaarClassifier&gt;(n);
              for (j = 0; j &lt; n; j++)
              {
                  cascade.StageClassifiers[i].Classifiers.Add(new HaarClassifier());
                  cascade.StageClassifiers[i].Classifiers[j].HaarFeatures = null;
              }
  
              cascade.StageClassifiers[i].Count = n;
  
              j = 0;
              XElement tree_fn = (XElement)trees_fn.FirstNode;
              while (null != tree_fn)
              {
                  HaarClassifier classifier;
                  int lastIndex;
  
                  classifier = cascade.StageClassifiers[i].Classifiers[j];
                  classifier.Count = tree_fn.Elements().Count();
  
                  classifier.HaarFeatures = new List&lt;HaarFeature&gt;(classifier.Count);
                  for (k = 0; k &lt; classifier.Count; k++)
                  {
                      classifier.HaarFeatures.Add(new HaarFeature());
                      classifier.Left.Add(0);
                      classifier.Right.Add(0);
                      classifier.Threshold.Add(0);
                      classifier.Alpha.Add(0);
                  }
                  classifier.Alpha.Add(0);
  
                  lastIndex = 0;
                  k = 0;
                  XNode node_fn = tree_fn.FirstNode;
                  while (null != node_fn)
                  {
                      if (!(node_fn is XElement))
                          goto next_node_fn;
  
                      XElement feature_fn;
                      XElement rects_fn;
  
                      feature_fn = ((XElement)node_fn).Element(featureId);
                      rects_fn = feature_fn.Element(rectsId);
  
                      l = 0;
                      XNode rect_fn = rects_fn.FirstNode;
                      while (null != rect_fn)
                      {
                          if (!(rect_fn is XElement))
                              goto next_rect_fn;
  
                          {
                              string[] rectangleParams = ((XElement)rect_fn).Value.Split(' ');
                              Rectangle rectangle = new Rectangle();
                              rectangle.X = int.Parse(rectangleParams[0]);
                              rectangle.Y = int.Parse(rectangleParams[1]);
                              rectangle.Width = int.Parse(rectangleParams[2]);
                              rectangle.Height = int.Parse(rectangleParams[3]);
  
                              classifier.HaarFeatures[k].Rectangles[l] = new HaarRectangle();
                              classifier.HaarFeatures[k].Rectangles[l].Weight = float.Parse(rectangleParams[4]);
                              classifier.HaarFeatures[k].Rectangles[l].Rectangle = rectangle;
                          }
  
                          l++;
                      next_rect_fn:
                          rect_fn = (XElement)rect_fn.NextNode;
                      }
  
                      for (; l &lt; 3; ++l)
                          classifier.HaarFeatures[k].Rectangles[l] = new HaarRectangle();
  
                      fn = feature_fn.Element(tiltedId);
                      int.TryParse(fn.Value, out classifier.HaarFeatures[k].Tilted);
  
                      fn = ((XElement)node_fn).Element(thresholdId);
                      classifier.Threshold[k] = float.Parse(fn.Value);
  
                      fn = ((XElement)node_fn).Element(left_nodeId);
                      if (null != fn) /* left node */
                          classifier.Left[k] = int.Parse(fn.Value);
                      else
                      {
                          fn = ((XElement)node_fn).Element(left_valId);
  
                          classifier.Left[k] = -lastIndex;
                          classifier.Alpha[lastIndex++] = float.Parse(fn.Value);
                      }
  
                      fn = ((XElement)node_fn).Element(right_nodeId);
                      if (null != fn) /* right node */
                          classifier.Right[k] = int.Parse(fn.Value);
                      else
                      {
                          fn = ((XElement)node_fn).Element(right_valId);
  
                          classifier.Right[k] = -lastIndex;
                          classifier.Alpha[lastIndex++] = float.Parse(fn.Value);
                      }
  
                      k++;
                  next_node_fn:
                      node_fn = (XElement)node_fn.NextNode;
                  }
  
                  j++;
                  tree_fn = (XElement)tree_fn.NextNode;
              }
  
              fn = stage_fn.Element(stageThresholdId);
              cascade.StageClassifiers[i].Threshold = float.Parse(fn.Value);
  
              parent = i - 1;
              next = -1;
  
              fn = stage_fn.Element(parentId);
              parent = int.Parse(fn.Value);
  
              fn = stage_fn.Element(nextId);
              next = int.Parse(fn.Value);
  
              cascade.StageClassifiers[i].Parent = parent;
              cascade.StageClassifiers[i].Next = next;
              cascade.StageClassifiers[i].Child = -1;
  
              if (parent != -1 &amp;&amp; cascade.StageClassifiers[parent].Child == -1)
                  cascade.StageClassifiers[parent].Child = i;
  
              i++;
              stage_fn = (XElement)stage_fn.NextNode;
          }
  
          return cascade;
      }
  </code></pre>