简体   繁体   English

在迭代XML LINQ查询时访问嵌套元素?

[英]Accessing nested elements while iterating an XML LINQ query?

With this XML data: 使用此XML数据:

<item>
  <placemarks>
    <placemark>
      <uid>{EA5FA2B2-78CB-4FAA-9F17-EBB361410499}</uid>
    </placemark>
  </placemarks>
  <links>
    <link>
      <title>Book Online</title>
      <link>https://blah</link>
    </link>
  <link>
    <title>Call to Book</title>
    <link>tel:1-866-555-5555</link>
  </link>
</links>
</item>

I'm trying to create Hotel objects w/ various attributes from the XML. 我正在尝试使用XML创建带有各种属性的Hotel对象。 Everything works fine until I hit nested XML tags and then I got stuck: 一切正常,直到我打了嵌套的XML标签,然后被卡住:

var items = root.Descendants ("item");
var hotels = from it in items
        select new Hotel () {
                Uid = it.Element ("uid").Value,
                Name = it.Element ("name").Value,
                Description = it.Element ("description").Value,
                ImageUrl = it.Element ("image_url").Value,
                RoomType = it.Element("custom_1").Value,
                PlacemarkId = it.Element("placemarks").Element("placemark").Element("uid").Value,
                BookUrl = (from links in it.Descendents("links") where links.Element("link").Element("title) = "Book Online").Value
            };

How do I get PlacemarkId to work? 如何使PlacemarkId正常工作? I keep getting null because the methods after the first Element("placemarks") evidently fails :-( And obviously, setting the BookUrl property won't compile, but that's what I'd like to do. It's really ugly because of the weird XML schema w/ nested link tags :-( 我会一直为null,因为第一个Element(“ placemarks”)之后的方法显然失败了:-(而且,显然,设置BookUrl属性不会编译,但这是我想做的。由于怪异,这确实很丑陋带嵌套链接标记的XML模式:-(

Sorry for the noob question. 对不起,菜鸟问题。 I tried googling for every combo of "nested xml linq select" I could think of w/ no luck :-P Would help even more if someone can let me know what I'm trying to do is called in LINQ. 我尝试过搜索“嵌套xml linq select”的每个组合,我想起来可能没有运气:-P如果有人可以让我知道我要执行的操作在LINQ中可以提供更多帮助。 I would think it's possible... 我认为有可能...

Thanks in advance :-) 提前致谢 :-)

You can use XPathSelectElement() extension method to avoid null reference exception in case some <item> don't have placemark child (without having to manually check for nulls from C# code) : 您可以使用XPathSelectElement()扩展方法来避免null引用异常,以防某些<item>没有placemark子(不必从C#代码中手动检查null):

var hotels = from it in items
        select new Hotel()
        {
            ......
            ......
            PlacemarkId = (string)it.XPathSelectElement("placemarks/placemark/uid"),
            BookUrl = (string)it.XPathSelectElement("links/link[title='Book Online']/link"),
        };

Getting BookUrl value can also be done using XPath as demonstrated above. BookUrl还可以使用XPath来获取BookUrl值。 Or if you're sure the XML structure is consistent for this part (no element is ever missing), you can use LINQ without null checking like so : 或者,如果您确定此部分的XML结构是一致的(不会丢失任何元素),则可以使用LINQ而不进行null检查,如下所示:

BookUrl = (from link in it.Elements("links").Elements("link") 
           where (string)link.Element("title") == "Book Online"
           select link.Element("link")).First().Value

For reference : 以供参考 :

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM