我有一些使用XDocument
类的.Net 3.5代码。
您能否建议使用快速而简单的方法在.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 :(得分:3)
您可以尝试编译.Net 2.0项目中的单声道来源:https://github.com/mono
以下是mono实现System.Xml.Linq的源代码:
https://github.com/mono/mono/tree/c7c906d69ac9e360ce3e7d517258b8eea2b962b2/mcs/class/System.Xml.Linq
理论上这应该是可行的,因为.Net 3与.Net 2共享相同的运行时间。但我怀疑这会很快......
答案 1 :(得分:1)
XDocument和XElement在LINQ(Language INtegrated Query)之上运行。因此,代码本身无法在.NET 2.0上下文中运行。
但您可以尝试使用Xml Serialization或XmlReader。