在迭代XML LINQ查询时访问嵌套元素? [英] Accessing nested elements while iterating an XML LINQ query?

查看:65
本文介绍了在迭代XML LINQ查询时访问嵌套元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此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>

我正在尝试使用XML创建带有各种属性的Hotel对象.一切正常,直到我打了嵌套的XML标签,然后被卡住:

I'm trying to create Hotel objects w/ various attributes from the XML. Everything works fine until I hit nested XML tags and then I got stuck:

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
            };

如何使PlacemarkId正常工作?我一直得到null,因为第一个Element("placemarks")之后的方法显然失败了:-( 显然,设置BookUrl属性不会编译,但这就是我想要做的.由于带有嵌套链接标签的奇怪XML模式,这真的很丑陋:-(

How do I get PlacemarkId to work? 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 :-(

很抱歉出现菜鸟问题.我尝试过搜索嵌套xml linq select"的每个组合,我想起来可能没有运气:-P 如果有人可以让我知道我要执行的操作在LINQ中会提供更多帮助.我认为有可能...

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. I would think it's possible...

预先感谢:-)

推荐答案

您可以使用

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) :

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"),
        };

也可以使用XPath来获得BookUrl值,如上所述.或者,如果您确定此部分的XML结构是一致的(永远不会丢失任何元素),则可以使用LINQ而不进行null检查,就像这样:

Getting BookUrl value can also be done using XPath as demonstrated above. 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 :

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

供参考:

  • W3C : XML Path Language (XPath) Version 1.0 specification (formal & complete spec of xpath)
  • w3schools : XPath syntax (relaxed intro to basic xpath syntax)

这篇关于在迭代XML LINQ查询时访问嵌套元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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