回答 Ienumerable.'s foreach不起本准则

aricherie.

新成员
加入
2月10日,2021年
消息
3
编程经验
1-3
嗨,我需要从XML文件(特别是KML文件)中提取坐标。我用IEnumerable来搜索所有标签"coordinates"但是什么都没有打印出来"foreach"环形。有人可以引导我在这里不正确吗?这是一个测试kml文件来帮助: 时尚.

C#:
            XDocument kmlDoc = XDocument.Load("Documents\\c#\\SafeFlyZone.kml");
            IEnumerable<XElement> coordinates = kmlDoc.Elements("coordinates") ;
            
            foreach (XElement c in coordinates)
            {
                Console.WriteLine(c);
                Console.WriteLine("hi");
            }
 
最后编辑了主持人:

jmplhinney.

C#论坛主持人
工作人员
加入
2011年4月23日
消息
3,732
地点
悉尼,澳大利亚
编程经验
10+
Did you debug your code, i.e. set a breakpoint and step through the code? If you haven't, go back and do it. If you have, tell us what happened. If execution gets to the Foreach. line but doesn't get into the loop then there's no items in the list, plain and simple.
 

跳伞运动员

工作人员
加入
2019年4月6日
消息
2,903
地点
切萨皮克,va.
编程经验
10+
Part of the problem here is that you are using Elements() which just returns matching nodes that are direct children of the parent node. Looking at the KML file you have in hastebin, the <coordinates> node is several levels deep. Perhaps you meant to use [icodeDescendants()[/icode]?
 

aricherie.

新成员
加入
2月10日,2021年
消息
3
编程经验
1-3
Part of the problem here is that you are using Elements() which just returns matching nodes that are direct children of the parent node. Looking at the KML file you have in hastebin, the <coordinates> node is several levels deep. Perhaps you meant to use [icodeDescendants()[/icode]?
嘿,
我更新到代码以使用后代。

C#:
           XDocument kmlDoc = XDocument.Load("C:\\Users\\ari.paterson.CGW\\Documents\\c#\\SafeFlyZone.kml");
            IEnumerable<XElement>coordinates = kmldoc.descendants() ;
         
            foreach (XElement c in coordinates)
            {
                Console.WriteLine(c);
            }
问题是,如果我指定了"coordinates"在后代参数中的标记,即。改变这条线:Ienumerable<XElement>coordinates = kmldoc.descendants("coordinates") ;
然后,Foreach循环不显示任何内容。
 
Last edited:

跳伞运动员

工作人员
加入
2019年4月6日
消息
2,903
地点
切萨皮克,va.
编程经验
10+
回想一下,您的.kml文件使用XML命名空间,因此您需要符合使用命名空间传入的XNames。

无论如何,以下代码适用于我:
C#:
var xdoc = XDocument.Load("SafeFlyZone.kml");
var coordinates = xdoc.Descendants("{http://www.opengis.net/kml/2.2}coordinates");
foreach(var c in coordinates)
    Console.WriteLine(c);
 

aricherie.

新成员
加入
2月10日,2021年
消息
3
编程经验
1-3
回想一下,您的.kml文件使用XML命名空间,因此您需要符合使用命名空间传入的XNames。

无论如何,以下代码适用于我:
C#:
var xdoc = XDocument.Load("SafeFlyZone.kml");
var coordinates = xdoc.Descendants("{http://www.opengis.net/kml/2.2}coordinates");
foreach(var c in coordinates)
    Console.WriteLine(c);
谢谢你。现在它是正确的读取,我需要提取每个坐标的纬度,经度和高度。我有一个带有这些属性的坐标类。正如您可以在该KML文件中看到的,所以每个点的所有纬度,经度和高度都在一个坐标标签内向后列出(基本上所有坐标点,在一起,包括关闭"area")。您如何建议我提取每个坐标的3个属性?我想高效
 

跳伞运动员

工作人员
加入
2019年4月6日
消息
2,903
地点
切萨皮克,va.
编程经验
10+
对于那些在家里玩耍而不打算打开时髦的人(或者企业防火墙阻止进入Hastebin):
XML:
<coordinates>
  -81.63342996909985,26.00624888622895,5.511072327176461 -81.69436462186725,26.00111266059973,5.775688596799045 -81.72714274216864,25.97200771522702,5.891206165085467 -81.74804280929955,25.89820713561373,5.516336686554482 -81.7194878797953,25.86969159426276,5.406925156662283 -81.68777978463756,25.8678037174685,5.565123116987332 -81.64888892173191,25.89234574717341,5.847666518438936 -81.65501830196251,25.90413728541918,5.577885344879334 -81.67890577689818,25.90698854943053,5.376601784872362 -81.69497545624714,25.92429045472441,6.031950330691663 -81.62838022283984,25.91364075147099,5.399419633485818 -81.60404574616953,25.9581287386885,5.715948449035759 -81.63342996909985,26.00624888622895,5.511072327176461
</coordinates>

Personally, if I were not concerned about efficiency, I would split the InnerText value by spaces first to get each group of X,Y,Z values, and then for each group split by commas to get each X, Y, Z value and then parse the floating point values into properties of the Coordinate class. I would ignore the cost of allocating all the arrays and strings created by each parsing process, and the overhead of using a class instead of struct.

If I were concerned about efficiency, I would first ensure that the Coordinate class is converted to a Coordinate struct. Next, I would write a custom parser that would parse the string from left to write consuming the X, Y, Z values as it encounters them and spits out Coordinate structs as each one is completely read in.
 
最佳 底部