当只有一个节点实例时,LINQ中的IEnumerable
我正在尝试使用Linq查询一些XML。我有以下XML片段当只有一个节点实例时,LINQ中的IEnumerable,linq,linq-to-xml,Linq,Linq To Xml,我正在尝试使用Linq查询一些XML。我有以下XML片段 <report> <invoice> <id>A4245</id> <date>12-20-2016</date> <amount>12.50</amount> </invoice> <report> 这是LINQ查询 XDocument xml = XDocument.Parse(@"
<report>
<invoice>
<id>A4245</id>
<date>12-20-2016</date>
<amount>12.50</amount>
</invoice>
<report>
这是LINQ查询
XDocument xml = XDocument.Parse(@"C:\Path\To\data.xml");
InvoiceModel invoice = xml.Descendants("invoice")
.Select(x => new InvoiceModel
{
InvoiceId = x.Element("id").Value.ToString(),
InvoiceDate = x.Element("date").Value.ToString(),
InvoiceAmount = x.Element("amount").Value.ToString()
});
我得到的错误是
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<InvoiceModel>' to 'InvoiceModel'. An explicit conversion exists (are you missing a cast?)
无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“InvoiceModel”。存在显式转换(是否缺少强制转换?)
XML中只有一张发票。如何告诉LINQ查询只有一个发票实例,而不需要IEnumerable
另外,我使用所有字符串作为属性值,因为它们只是用作文本,不会被解析。试试看
XDocument xml = XDocument.Parse(@"C:\Path\To\data.xml");
InvoiceModel invoice = xml.Descendants("invoice")
.Select(x => new InvoiceModel
{
InvoiceId = x.Element("id").Value.ToString(),
InvoiceDate = x.Element("date").Value.ToString(),
InvoiceAmount = x.Element("amount").Value.ToString()
}).FirstOrDefault();
试一试
这是因为
子体(…)
返回一个IEnumerable
,然后使用Select
将其转换为IEnumerable
。如果您知道最多有一个具有正确名称的子体,请使用FistSordDefault()
检索它:
InvoiceModel invoice = xml.Descendants("invoice")
.Select(x => new InvoiceModel {
InvoiceId = x.Element("id").Value.ToString(),
InvoiceDate = x.Element("date").Value.ToString(),
InvoiceAmount = x.Element("amount").Value.ToString()
}).FirstOrDefault();
// Make sure that you get an invoice
if (invoice == null) {
throw new InvalidOperationException("Invoice element does not exist in XML document.");
}
这是因为
子体(…)
返回一个IEnumerable
,然后使用Select
将其转换为IEnumerable
。如果您知道最多有一个具有正确名称的子体,请使用FistSordDefault()
检索它:
InvoiceModel invoice = xml.Descendants("invoice")
.Select(x => new InvoiceModel {
InvoiceId = x.Element("id").Value.ToString(),
InvoiceDate = x.Element("date").Value.ToString(),
InvoiceAmount = x.Element("amount").Value.ToString()
}).FirstOrDefault();
// Make sure that you get an invoice
if (invoice == null) {
throw new InvalidOperationException("Invoice element does not exist in XML document.");
}
您可以使用
.FirstOrDefault()
选择唯一的发票
元素:
InvoiceModel invoice = xml.Descendants("invoice")
.Select(x => new InvoiceModel
{
InvoiceId = x.Element("id").Value.ToString(),
InvoiceDate = x.Element("date").Value.ToString(),
InvoiceAmount = x.Element("amount").Value.ToString()
})
.FirstOrDefault();
代码中还有其他问题:
- 要使用
加载文件,XDocument.Load
用于原始xmlParse
- xml没有正确终止。最后一个标记应该是
.FirstOrDefault()
选择唯一的发票
元素:
InvoiceModel invoice = xml.Descendants("invoice")
.Select(x => new InvoiceModel
{
InvoiceId = x.Element("id").Value.ToString(),
InvoiceDate = x.Element("date").Value.ToString(),
InvoiceAmount = x.Element("amount").Value.ToString()
})
.FirstOrDefault();
代码中还有其他问题:
- 要使用
加载文件,XDocument.Load
用于原始xmlParse
- xml没有正确终止。最后一个标记应该是
.Select()
返回一个集合。如果该集合中只有一个元素,请附加:
.Single()
如果可以有多个,并且您想要第一个:
.First()
如果应该有一个,但可能没有:
.SingleOrDefault()
等等
您可以应用多种逻辑,并为任何IEnumerable
应用多种扩展方法。基本上,如果您只需要一个,请从返回的集合中选择一个。select()
。select()
返回一个集合。如果该集合中只有一个元素,请附加:
.Single()
如果可以有多个,并且您想要第一个:
.First()
如果应该有一个,但可能没有:
.SingleOrDefault()
等等
您可以应用多种逻辑,并为任何
IEnumerable
应用多种扩展方法。基本上,如果您只需要一个,请从.select()返回的集合中选择该集合
使用var….var invoice=xml.degenants最初我尝试了var invoice
,但它成功了,但我想从包含此代码的方法返回强类型对象。是否有任何方法修改LINQ语句,以便只返回一个元素?我尝试使用Single()
和First()
但是Select()
在使用这些代码后不可用。use var….var invoice=xml.degenants我最初尝试了var invoice
,但它成功了,但我想从包含此代码的方法返回强类型对象。是否有任何方法修改LINQ语句,以便只返回一个元素?我试过使用Single()
和First()
但是Select()
在使用它们之后不可用。