2017-05-08 99 views
0

我有下面的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, 

         }; 

我如何再选择这本书的封面价量为美国?我想尽组合仅返回量的所有四个域

+0

你必须手动删除不符合您的喜好的价格,所有儿童都包括在内。 – CodeCaster

+0

请将您的XML简化为一两本书 - 这些数据没有任何好处。 –

回答

1

你接近

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() 
         }; 
+0

我想你需要选择'amount'子元素... –

+0

@JonSkeet是的,先生,我累了。谢谢。 –

1
  • 目前你想直属book去取price元素,所以这是行不通的开始。
  • 接下来,你要只用正确的country子元素中选择price元素
  • 最后,你不希望price元素的值,你想要它amount子元素的价值 - 我会建议得到,作为一个decimal,而不是作为一个字符串

因此,这意味着不是这样的:

Price = r.Element("price").Value 

你想要的东西,如:

Price = (decimal) r.Element("cover-price") 
        .Elements("price") 
        .Single(p => (string) p.Element("country") == "US") 
        .Element("amount") 

如果有没有任何元素price用正确的国家,仍然会失败。你可以用这个代替以获得适当的decimal?

Price = (decimal?) r.Element("cover-price") 
        .Elements("price") 
        .SingleOrDefault(p => (string) p.Element("country") == "US") 
        ?.Element("amount")