2011-05-05 105 views
1

我有一些使用XDocument類的.net 3.5代碼。XDocument for .Net 2.0?

你可以建議一個快速和骯髒的方式讓這個代碼在.Net 2.0中運行?

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<HaarClassifier>(n); 
      for (j = 0; j < 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<HaarFeature>(classifier.Count); 
       for (k = 0; k < 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 < 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 && cascade.StageClassifiers[parent].Child == -1) 
       cascade.StageClassifiers[parent].Child = i; 

      i++; 
      stage_fn = (XElement)stage_fn.NextNode; 
     } 

     return cascade; 
    } 
+0

爲什麼你被限制爲.Net 2? – svick 2011-05-06 17:37:13

+0

升級到現有系統,由於與供應商應用程序的潛在衝突,用戶無法獲得更高的.net版本,幫助臺不想推出任何潛在的修補程序,該列表依然存在。 – 2011-05-06 17:49:07

+0

@Bill,我知道你可以做的不多,但我認爲你應該嘗試一下。 .Net庫是故意版本化的,因此您可以在任何給定時間擁有多個版本,而不會出現任何問題。 – svick 2011-05-06 18:12:18

回答

3

你可以嘗試編譯單聲道音源在.NET 2.0項目:https://github.com/mono

這裏是單聲道的實現System.Xml.Linq的來源:

https://github.com/mono/mono/tree/c7c906d69ac9e360ce3e7d517258b8eea2b962b2/mcs/class/System.Xml.Linq

這應該理論上可行,因爲.Net 3與.Net 2共享相同的運行時間。但是我懷疑這會很快...

+0

現在下載,我希望這個工程! – 2011-05-06 14:41:13

+0

工作就像一個魅力。我不得不導入所有的System.Linq,System.Xml.Linq和其他幾個,但它的工作原理! – 2011-05-06 17:49:48

1

XDocument和XElement在LINQ(語言集成查詢)之上運行。因此,代碼本身不能在.NET 2.0上下文中運行。

然而,您可以嘗試使用Xml Serialization或XmlReader。

+0

這不太準確。 XDocument和XElement都支持LINQ,它們不會「在其上運行」。請注意,OP發佈的代碼不使用LINQ查詢解析或任何LINQ擴展方法。 – 2011-05-06 00:26:11