基于InnerXML对XDocument元素排序
本文关键字:元素 排序 XDocument InnerXML 基于 | 更新日期: 2023-09-27 18:01:59
我有一个类似的XML文档:
<document>
<post>
<author>Bill Smith</author>
<subject>Test Article</subject>
<dates>
<uploaded>some date</uploaded>
<published>some date</published>
</dates>
<price>
<provider>Amazon</provider>
<cost>1540</cost>
</price>
<price>
<provider>WH Smith</provider>
<cost>2640</cost>
</price>
</post>
<post>
<author>Bill Smith</author>
<subject>Test Article</subject>
<dates>
<uploaded>some date</uploaded>
<published>some date</published>
</dates>
<price>
<provider>Amazon</provider>
<cost>1540</cost>
</price>
<price>
<provider>WH Smith</provider>
<cost>2640</cost>
</price>
</post>
</document>
我正在使用XDocument w/. net 4.5。我知道我可以用其他方法来排序。
我有它的工作OK拉每个帖子,并把它放到一个帖子模型。但是,我想对价格元素进行排序并挑选出最低的价格(以及供应商),以便将其插入到我的EF数据库中。
任何帮助将不胜感激,我完全卡住了,甚至从哪里开始这个
你可以试试:
XDocument doc = XDocument.Load(@"Data.xml");
var elements = doc.Root
.Elements("post")
.Select(post => new
{
Author = post.Element("author").Value,
Subject = post.Element("subject").Value,
Uploaded = Convert.ToDateTime(post.Element("dates").Element("uploaded").Value),
Published = Convert.ToDateTime(post.Element("dates").Element("published").Value),
Price = new
{
P = post
.Elements("price")
.OrderByDescending(price => Convert.ToDecimal(price.Element("cost").Value))
.Select(o => new
{
Provider = o.Element("provider").Value,
Cost = Convert.ToDecimal(o.Element("cost").Value)
})
.First()
}
});
var p = elements.First();
根据您提供的代码,看看是否有效:
var xmlDocumentElement = xDocument.Load("xmlData.xml").Root;
var posts = xmlDocumentElement.Elements("post").Select(post => new
{
Author = post.Element("author").Value.ToString(),
Subject = post.Element("subject").Value.ToString(),
AvailableDate = DateTime.Parse(post.Descendants("dates").FirstOrDefault().Value),
Price = GetPriceFromPriceXElement(post.Elements("price").Aggregate((prev, next) =>
Decimal.Parse(prev.Element("cost").Value.ToString()) <= Decimal.Parse(next.Element("cost").Value.ToString()) ? prev : next ))
}
);
public Price GetPriceFromPriceXElement(XElement price)
{
return new Price
{
Provider = price.Element("provider").Value.ToString(),
Cost = price.Element("cost").Value.ToString()
};
}
试试这个。这应该行得通。一旦你有了数据,你就可以对它进行按摩。请保管NULL
等。这只是关于您的需求的基本想法。
XDocument doc = XDocument.Load(@"XMLFile1.xml");
var posts = doc.Root.Elements("post")
.Select(p =>
new
{
Author = p.Element("author").Value,
Price = p.Elements("price").OrderBy(a => decimal.Parse(a.Element("cost").Value)).First()
});
foreach(var a in posts)
{
Console.WriteLine(a.Author + " " + a.Price);
}
这是一个非常直接的尝试(实际上与Ivan G的答案非常相似):
var posts = from post in document.Root.Elements("post")
select new
{
Author = post.Element("author").Value,
Subject = post.Element("subject").Value,
Uploaded = DateTime.Parse(post.Element("dates").Element("uploaded").Value),
Published = DateTime.Parse(post.Element("dates").Element("published").Value),
Price = (from price in post.Elements("price")
let cost = decimal.Parse(price.Element("cost").Value)
orderby cost
select new
{
Provider = price.Element("provider").Value,
Cost = cost
}).First()
};
对于你给我们的样本数据(除了日期"某些日期"),它工作得很好。
如果数据可能不完整或错误(即缺失/拼写错误的节点或错误的数据类型),我建议只取每个XElement
而不是值:Author = post.Element("author")
等。也许你想允许没有上传或发布日期的帖子。通过这种方式,您可以稍后检查和/或强制转换/转换数据,并可能正确地修复问题。