XML 文档中的特定元素

托尼·基尔南

我有以下 XML:

<?xml version="1.0" encoding="utf-8" ?>
<catalog>
  <books>
<book id="bk101"> 
  <author id="1">Gambardella, Matthew</author>
  <title>XML Developer's Guide</title>
  <genre>Computer</genre>
  <cover-price>
    <price>
        <country>US</country>
        <amount>44.95</amount>
    </price>
    <price>
        <country>UK</country>
        <amount>39.99</amount>
    </price>
  </cover-price>
  <unit-price>
    <price>
        <country>US</country>
        <amount>25.00</amount>
    </price>
    <price>
        <country>UK</country>
        <amount>20.00</amount>
    </price>
  </unit-price>
  <publish_date>2000-10-01</publish_date>
   <description> An in-depth look at creating applications with XML.</description>
</book>
<book id="bk102">
  <author id="2">Ralls, Kim</author>
  <title>Midnight Rain</title>
  <genre>Fantasy</genre>
  <cover-price>
    <price>
        <country>US</country>
        <amount>5.95</amount>
    </price>
    <price>
        <country>UK</country>
        <amount>3.99</amount>
    </price>
  </cover-price>
  <unit-price>
    <price>
        <country>US</country>
        <amount>2.50</amount>
    </price>
    <price>
        <country>UK</country>
        <amount>2.00</amount>
    </price>
  </unit-price>
  <publish_date>2000-12-16</publish_date>
  <description> A former architect battles corporate zombies,an evil sorceress, and her own childhood to become
queen of the world.</description>
</book>
<book id="bk103">
  <author id="3">Corets, Eva</author>
  <title>Maeve Ascendant</title>
  <genre>Fantasy</genre>
  <cover-price>
    <price>
        <country>US</country>
        <amount>5.95</amount>
    </price>
    <price>
        <country>UK</country>
        <amount>3.99</amount>
    </price>
  </cover-price>
  <unit-price>
    <price>
        <country>US</country>
        <amount>2.50</amount>
    </price>
    <price>
        <country>UK</country>
        <amount>2.00</amount>
    </price>
  </unit-price>
    <publish_date>2000-11-17</publish_date>
    <description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for
a new society. </description>
  </book>

<book id="bk104">
    <author id="4">Corets, Eva</author>
    <title>Oberon's Legacy</title>
    <genre>Fantasy</genre>
  <cover-price>
    <price>
        <country>US</country>
        <amount>5.95</amount>
    </price>
    <price>
        <country>UK</country>
        <amount>3.99</amount>
    </price>
  </cover-price>
  <unit-price>
    <price>
        <country>US</country>
        <amount>2.50</amount>
    </price>
    <price>
        <country>UK</country>
        <amount>2.00</amount>
    </price>
  </unit-price>
    <publish_date>2001-03-10</publish_date>
    <description>In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for
 the inhabitants of London. Sequel to Maeve Ascendant.</description>

  </book>

<book id="bk105">
    <author id="5">Corets, Eva</author>
    <title>The Sundered Grail</title>
    <genre>Fantasy</genre>
  <cover-price>
    <price>
        <country>US</country>
        <amount>5.95</amount>
    </price>
    <price>
        <country>UK</country>
        <amount>3.99</amount>
    </price>
  </cover-price>
  <unit-price>
    <price>
        <country>US</country>
        <amount>2.50</amount>
    </price>
    <price>
        <country>UK</country>
        <amount>2.00</amount>
    </price>
  </unit-price>
    <publish_date>2001-09-10</publish_date>
    <description>The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to
Oberon's Legacy.</description>

  </book>
  </books>
</catalog>

我可以使用以下来选择特定的书

var selectedBook = from r in document.Descendants("book").Where
                                   (r=>(string)r.Attribute("id")=="bk102")
                        select new
                        {
                            Author = r.Element("author").Value,
                            Title = r.Element("title").Value,
                            Genere = r.Element("genre").Value,
                            Price = r.Element("price").Value,
                            PublishDate = r.Element("publish_date").Value,
                            Description = r.Element("description").Value,

                        };

然后我如何选择该书在美国的封面价格?我尝试过的每个组合都只返回所有四个金额字段

查克·萨维奇

你很近

var selectedBook = from r in document.Descendants("book").Where
                                   (r=>(string)r.Attribute("id")=="bk102")
                        select new
                        {
                            Author = r.Element("author").Value,
                            Title = r.Element("title").Value,
                            Genere = r.Element("genre").Value,
                            Price = r.Element("price").Value,
                            PublishDate = r.Element("publish_date").Value,
                            Description = r.Element("description").Value,
                            USPrice = r.Element("cover-price") 
                                       .Descendants("country")
                                       .Where(c => (string)c == "US"))
                                       .Select(c => (decimal)c.Parent.Element("amount"))
                                       .First()
                        };

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

与XML文档中首次出现的特定元素匹配的模板

来自分类Dev

选择存储在SQL中的多个元素的xml文档的特定属性的值

来自分类Dev

处理xml文档中的缺失元素

来自分类Dev

检索XML文档中的HTML元素

来自分类Dev

计算 PHP 中多个文档的 XML 元素

来自分类Dev

Java中的XML:从特定元素获取值

来自分类Dev

Java中的XML:从特定元素获取值

来自分类Dev

在Delphi中删除特定的XML元素

来自分类Dev

使用Etree从XML中删除特定元素

来自分类Dev

在 Labview 中获取特定的 XML 元素属性

来自分类Dev

如何从python中的xml文档中获取特定标签

来自分类Dev

如何从Windows Phone中的XML文档中检索特定值

来自分类Dev

如何从python中的xml文档中获取特定标签

来自分类Dev

在xml文档中查找化合价最高的元素

来自分类Dev

如何使用XSLT遍历xml文档中的元素

来自分类Dev

如何使用XQuery将元素插入当前的XML文档中?

来自分类Dev

使用Python lxml从XML文档中获取几个元素

来自分类Dev

使用Groovy XmlParser搜索整个文档中的XML元素

来自分类Dev

如何使用XSLT遍历xml文档中的元素

来自分类Dev

在jQuery XML文档中的DOM中选择和操作元素?

来自分类Dev

根据节点在xml文档中的位置查找元素的值

来自分类Dev

将元素插入从现有内容派生的XML文档中

来自分类Dev

使用 Powershell 在现有 XML 文档中创建新元素

来自分类Dev

Java for循环从xml文档中获取子元素

来自分类Dev

C#-从XML文档中的特定标签获取价值

来自分类Dev

XSLT - 从外部 XML 文档中获取特定值

来自分类Dev

从python中的XML文档中提取特定数据

来自分类Dev

在C#中删除特定的XML元素及其子元素

来自分类Dev

修改 XML 中相似元素的特定子元素

Related 相关文章

热门标签

归档