C# XML文档中的特定元素
我有以下XML:C# XML文档中的特定元素,c#,xml,linq,C#,Xml,Linq,我有以下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</
<?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>
那么,我如何为美国选择该书的封面价格金额?我尝试的每个组合只返回所有四个金额字段- 目前,您正试图直接在
下获取book
元素,因此这在一开始是行不通的price
- 接下来,您只想选择带有右侧
子元素的country
元素price
- 最后,您不需要
元素的值,您需要它的price
子元素的值-我建议将其作为amount
而不是字符串十进制
Price = r.Element("price").Value
你想要的是:
Price = (decimal) r.Element("cover-price")
.Elements("price")
.Single(p => (string) p.Element("country") == "US")
.Element("amount")
如果没有任何price
元素与正确的国家/地区匹配,则仍然会失败。您可以使用它来获取适当的十进制数?
:
Price = (decimal?) r.Element("cover-price")
.Elements("price")
.SingleOrDefault(p => (string) p.Element("country") == "US")
?.Element("amount")
- 目前,您正试图直接在
下获取book
元素,因此这在一开始是行不通的price
- 接下来,您只想选择带有右侧
子元素的country
元素price
- 最后,您不需要
元素的值,您需要它的price
子元素的值-我建议将其作为amount
而不是字符串十进制
Price = r.Element("price").Value
你想要的是:
Price = (decimal) r.Element("cover-price")
.Elements("price")
.Single(p => (string) p.Element("country") == "US")
.Element("amount")
如果没有任何price
元素与正确的国家/地区匹配,则仍然会失败。您可以使用它来获取适当的十进制数?
:
Price = (decimal?) r.Element("cover-price")
.Elements("price")
.SingleOrDefault(p => (string) p.Element("country") == "US")
?.Element("amount")
你很接近
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()
};
你很接近
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()
};
您必须手动删除与您的首选项不匹配的价格,因为所有子项都将包含在内。请将您的XML缩减为一本或两本书-拥有所有这些数据没有任何好处。您必须手动删除与您的首选项不匹配的价格,因为所有的孩子都会被包括在内。请把你的XML压缩成一本或两本书——拥有这些数据没有任何好处。@JonSkeet是的,先生,我累了。谢谢你。@JonSkeet是的,先生,我累了。非常感谢。