2010-05-16 98 views
1

我有了这个XML:如何迭代由LINQ-to-XML查询返回的对象集合?

<BillingLog> 
    <BillingItem> 
    <date-and-time>2003-11-04</date-and-time> 
    <application-name>Billing Service</application-name> 
    <severity>Warning</severity> 
    <process-id>123</process-id> 
    <description>Timed out on a connection</description> 
    <detail>Timed out after three retries.</detail> 
    </BillingItem> 
    <BillingItem> 
    <date-and-time>2010-05-15</date-and-time> 
    <application-name>Callback Service</application-name> 
    <severity>Error</severity> 
    <process-id>456</process-id> 
    <description>Unable to process callback</description> 
    <detail>Reconciliation timed out after two retries.</detail> 
    </BillingItem> 
</BillingLog> 

那我想使用LINQ到XML为包含在一个单一BillingLog对象BillingItem对象的集合项目。

public class BillingLog 
{ 
    public IEnumerable<BillingItem> items { get; set; } 
} 

public class BillingItem 
{ 
    public string Date { get; set; } 
    public string ApplicationName { get; set; } 
    public string Severity { get; set; } 
    public int ProcessId { get; set; } 
    public string Description { get; set; } 
    public string Detail { get; set;}  
} 

这是我使用的项目(其中包含字符串变量中)的XML LINQ查询。

XDocument xdoc = XDocument.Parse(source); 

var log = 
    from i in xdoc.Elements("BillingLog") 
    select new BillingLog 
    { 
     items = 
      from j in i.Descendants("BillingItem") 
      select new BillingItem 
      { 
       Date = (string)j.Element("date-and-time"), 
       ApplicationName = (string)j.Element("application-name"), 
       Severity = (string)j.Element("severity"), 
       ProcessId = (int)j.Element("process-id"), 
       Description = (string)j.Element("description"), 
       Detail = (string)j.Element("detail") 
      } 
    }; 

当我尝试并使用的foreach遍历对象日志

foreach (BillingItem item in log) 
{ 
Console.WriteLine ("{0} | {1} | {2} | {3} | {4} | {5}", 
        item.Date, item.ApplicationName, 
        item.Severity, item.ProcessId.ToString(), 
        item.Description, item.Detail); 
} 

我从LINQPad得到以下错误消息。

Cannot convert type 'UserQuery.BillingLog' to 'UserQuery.BillingItem' 

在此先感谢。

回答

3

这是因为您的log变量包含一个BillingLog对象的集合,而不是BillingItem。你必须做一些事情,如:

foreach(BillingLog l in log) 
{ 
    foreach(BillingItem item in l.items) 
    { 
     Console.WriteLine(...); 
    } 
} 

另外,如果你的初衷是只选择所有BillingItem S,BillingLog小号完全不顾他们的父母,你可以重写查询,如下所示:

var log = 
     from l in xdoc.Elements("BillingLog") 
     from j in l.Descendants("BillingItem") 
     select new BillingItem 
     { 
      Date = (string)j.Element("date-and-time"), 
      ApplicationName = (string)j.Element("application-name"), 
      Severity = (string)j.Element("severity"), 
      ProcessId = (int)j.Element("process-id"), 
      Description = (string)j.Element("description"), 
      Detail = (string)j.Element("detail") 
     } 

这会给你从所有BillingLog s中选出的所有BillingItems的简单收集,而BillingLog本身将被完全丢弃。

+0

嵌套的foreach工作(不知道为什么我没有看到)。另外,感谢LINQ-to-XML查询的细化,它很优雅。 – billmaya 2010-05-16 13:42:53