使用Linq查询记录的Xml文件
以下是我的xml文件。 我必须为每个页面提供字段,并为逗号分隔字符串中的每个类型提供字段。 请帮助您了解如何使用Linq
示例:如果我想为“page1”定义“Type = customFields”,则必须以逗号分隔的ProjectID,EmployeeID,EmployeeName,hasExpiration等输出
ProjectID EmployeeID EmployeeName HasExpiration EndDate ProjectID EmployeeID EmployeeName HasExpiration EndDate IsInUpdateMode TimesheetSpendLimit ProjectID EmployeeID EmployeeName HasExpiration EndDate IsInUpdateMode TimesheetSpendLimit ProjectID EmployeeID EmployeeName HasExpiration EndDate IsInUpdateMode TimesheetSpendLimit
你可以这样做:
var Result = from a in element.Descendants("Page") from b in a.Descendants("Type") select new { Page = a.Attribute("Name").Value, Type = b.Attribute("TypeID").Value, Fields = String.Join(",", b.Elements("Field").Select(x => x.Value)) }; foreach (var item in Result) { Console.WriteLine(String.Format("Page = {0}:Type={1}:Fields:{2}", item.Page, item.Type, item.Fields)); }
工作时尚
查看我的博客文章以获取更多信息。