在迭代xml文件时跳过不存在的属性 [英] Skipping non existing attributes when iterating through xml files

查看:114
本文介绍了在迭代xml文件时跳过不存在的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我正在阅读一些XML文件,其结构如下:

In my application I am reading some XML files which are structured like this:

<Locations>
    <Location elevation="933.0" id="3072" latitude="56.879" longitude="-3.42" name="Cairnwell" nationalPark="Cairngorms National Park" region="ta" unitaryAuthArea="Perth and Kinross"/>
    <Location elevation="4.0" id="3094" latitude="57.698" longitude="-2.121" name="Rosehearty Samos" region="gr" unitaryAuthArea="Aberdeenshire"/>
    <Location elevation="35.0" id="3144" latitude="56.326" longitude="-3.729" name="Strathallan" region="ta" unitaryAuthArea="Perth and Kinross"/>
    <Location elevation="57.0" id="3166" latitude="55.928" longitude="-3.343" name="Edinburgh/Gogarbank" region="dg" unitaryAuthArea="Edinburgh"/>
    <Location elevation="16.0" id="3204" latitude="54.0849" longitude="-4.6321" name="Ronaldsway" region="nw"/>
...



我正在读取要放入字符串列表的名称和unitaryAuthArea属性,但并非所有元素都具有unitaryAuthArea属性(如上面的xml中的最后一个)所以当我遍历它们时,它会堆积unitaryAuthArea attributesn而不返回所以它们被它们的name属性偏移并且所有混合。如果'Area3'不存在,我应该得到什么:


I am reading the name and unitaryAuthArea attributes to put into a string list but not all of the elements have the unitaryAuthArea attribute (like the last one in the xml above) so when I iterate through them it piles up the unitaryAuthArea attributesn without returning "" so they are offset by their name attribute and the all mix up. What I should get if 'Area3' does not exist with its name:

Name1, Area1
Name2, Area2
Name3, Area3 = "Area is not specified"
Name4, Area4
Name5, Area5



如果'Area3'不存在,我实际得到的是:


What I actually get if 'Area3' is non existent:

Name1, Area1
Name2, Area2
Name3, Area4
Name4, Area5
Name5, [error is thrown]



它们都堆叠起来,名称/值对不再匹配。我怎么能得到它,如果一个属性不存在,那么它应该返回一个像未指定的字符串?我之前也有这个,当从另一个文件读取阵风时,如果有一个值,那么只添加gust属性我在一个街区的顶部获得了所有阵风,而不是它应该是它的行。我试过这个但它不起作用:


They all stack up and the name/value pairs don't match anymore. How can I get it so if an attribute doesn't exist then it should return a string like "Not specified'? I have also had this before when reading gusts from another file where only the gust attribute is added if there is a value so I get all the gusts at the top in one block instead of in with its row that it should be. I have tried this but it doesn't work:

List sites = new List();
XmlDocument data = new XmlDocument();
data.Load("http://datapoint.metoffice.gov.uk/public/data/val/wxobs/all/xml/sitelist?key=");

for (int x = 0; x < data.SelectNodes("Locations/Location").Count; x++)
{
    string location = "";
    string name = data.SelectNodes("Locations/Location/@name")[x].InnerText;

    if (data.SelectNodes("Locations/Location/@unitaryAuthArea")[x] == null) 
    { location = "NOT AVAILABLE"; }
    else { location = data.SelectNodes("Locations/Location/@unitaryAuthArea")[x].InnerText; }
    sites.Add(name + ", " + location);
}
siteList.ItemsSource = sites;



我在最后一项上得到NOT AVAILABLE而不是第五项属性是失踪。我怎样才能解决这个问题?谢谢。


I just get "NOT AVAILABLE" on the last item and not the fifth down where the attribute is missing. How can I fix this? Thank you.

推荐答案



我遇到类似许多月前的事情,如果记忆在我的第一次尝试中对我有用,我选择节点并将它们放入XmlNodeList(例如XmlNodeList myNodeList = myDoc.SelectNodes()...)



从那里,我遍历节点(使用'foreach XmlNode ...')然后必须将节点强制转换为XmlElement,这使我能够使用.HasAttribute()方法。



尝试2 (这次不同的项目)是放弃使用XmlDocument并使用System.Xml.Linq(可能因为我只想尝试不同的东西)。



长话短说 - 如果你有时间,请调查使用System.Xml.Linq,如果没有,请尝试尝试1.



希望这是一些帮助。
Hi,
I came across something similar many moons ago and if memory serves me right in my first attempt, I selected the nodes and put them into an XmlNodeList (e.g. XmlNodeList myNodeList = myDoc.SelectNodes("")...)

From there, I iterated over the nodes (using a 'foreach XmlNode...') and then had to cast the node as an XmlElement which enabled me to use the .HasAttribute() method.

Attempt 2 (different project this time) was to abandon using XmlDocument and used System.Xml.Linq instead (probably as I just wanted to try something different).

Long story short - if you've time, investigate using System.Xml.Linq, if not, try attempt 1.

Hopefully that's some help of sorts.


这篇关于在迭代xml文件时跳过不存在的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆