1

我正在尝试导入一个 XML 文件,其中包含 drawLine 函数的点列表,它是典型的 drawLine 函数,给它你的 X1、Y1、X2、Y2 坐标,它会画线。我的 XML 文件中的内容如下:

<LINES>
  <LINE>
    <ID>J3U93</ID>
    <POINT X="454.5" Y="93.5"></POINT>
    <POINT X="454.5" Y="371"></POINT>
    <POINT X="433.5" Y="351"></POINT>
    <POINT X="433.5" Y="329.5"></POINT>
  </LINE>
  <LINE>
    <ID>U231U93</ID>
    <POINT X="23.5" Y="526"></POINT>
    <POINT X="417" Y="341.875"></POINT>
    <POINT X="380" Y="341.875"></POINT>
    <POINT X="188.5" Y="526"></POINT>
    <POINT X="23.5" Y="526"></POINT>
  </LINE>
   .
   .
</LINES>

每条线在文件中都有一个 ID 来区分它,我的线将连接起来基本上形成 Z 形图案,即根据 LINE 标签内的点数量不同的转数。

我需要知道,或者如果你能指出我正确的方向,我如何将 ID 标签中的一组线与另一个 ID 标签中的另一组线分开?

到目前为止,我尝试过:

List<Point> Points;

XDocument lineDataXml = XDocument.Load(filename);

Points = (
     from point in lineDataXml.Descendants("LINE")
      select new Point 
      { 
          X = Double.Parse(point.Attribute("X").Value), 
          Y = Double.Parse(point.Attribute("Y").Value) 
      }).ToList();

 foreach(Point a in Points)
 {
     Console.WriteLine(a);
 }

但这会返回 XML 中所有点的列表,而不知道哪些点属于哪个 ID。

您的帮助将不胜感激

彼得。

4

1 回答 1

2

试试这个,

XElement root = XElement.Load(file); // or .Parse(string)
var lines = root.Descendants("LINE")
    .Select(line =>
        new
        {
            Id = (string)line.Element("ID"),
            Points = line.Elements("POINT")
                .Select(p => new PointF
                {
                    X = (float)p.Attribute("X"),
                    Y = (float)p.Attribute("Y")
                }).ToArray()
        }).ToArray();
于 2012-06-12T15:09:34.503 回答