我有一些使用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;
}
爲什麼你被限制爲.Net 2? – svick 2011-05-06 17:37:13
升級到現有系統,由於與供應商應用程序的潛在衝突,用戶無法獲得更高的.net版本,幫助臺不想推出任何潛在的修補程序,該列表依然存在。 – 2011-05-06 17:49:07
@Bill,我知道你可以做的不多,但我認爲你應該嘗試一下。 .Net庫是故意版本化的,因此您可以在任何給定時間擁有多個版本,而不會出現任何問題。 – svick 2011-05-06 18:12:18