问题描述
嗨
从下面的XML结构我想基于TimeFrame属性进行分组,然后添加SuccessCount属性的值并有一个值。请找到
from the below XML structure I want to group by based on the TimeFrame Attribute , then later add the values of SuccessCount attribute and have one value . Please find the
XML结构如下
the XML structure is as below
< Users>
< User ID ="" 10""的LoginName = QUOT;" 222"" SuccessCount = QUOT;" 3英寸;" FailureCount = QUOT;" 2英寸;" LastAccessedTimeStamp ="" 1/1/2011 12:55:00 AM"" TimeFrame ="" 2011-01-01 00:00:00.000"" />
< User ID ="" 5""的LoginName = QUOT;"管理员"" SuccessCount = QUOT;" 7英寸;" FailureCount = QUOT;" 2英寸;" LastAccessedTimeStamp ="" 1/1/2011 12:49:00 AM"" TimeFrame =""" 2011-01-01 00:00:00.000"" />
< User ID ="" 9""的LoginName = QUOT;" 2222"" SuccessCount = QUOT;" 2英寸;" FailureCount = QUOT;" 8英寸;" LastAccessedTimeStamp ="" 1/1/2011 12:57:40 AM"" TimeFrame =""" 2011-01-01 00:00:00.000"" />
< User ID ="" 7""的LoginName = QUOT;" MSR2"" SuccessCount = QUOT;" 1 QUOT;" FailureCount = QUOT;" 3英寸;" LastAccessedTimeStamp ="" 1/1/2011 12:23:20 AM"" TimeFrame =""" 2011-01-01 00:00:00.000"" />
< User ID ="" 6""的LoginName = QUOT;" MSR1"" SuccessCount = QUOT;" 5英寸;" FailureCount = QUOT;" 3英寸;" LastAccessedTimeStamp ="" 1/1/2011 12:59:00 AM"" TimeFrame =""" 2011-01-01 00:01:00.000"" />
< User ID ="" 10""的LoginName = QUOT;" 222"" SuccessCount = QUOT;" 3英寸;" FailureCount = QUOT;" 2英寸;" LastAccessedTimeStamp ="" 1/1/2011 12:55:00 AM"" TimeFrame =""" 2011-01-01 00:01:00.000"" />
< User ID ="" 5""的LoginName = QUOT;"管理员"" SuccessCount = QUOT;" 7英寸;" FailureCount = QUOT;" 2英寸;" LastAccessedTimeStamp ="" 1/1/2011 12:49:00 AM"" TimeFrame ="" 2011-01-01 00:01:00.000"" />
< User ID ="" 9""的LoginName = QUOT;" 2222"" SuccessCount = QUOT;" 2英寸;" FailureCount = QUOT;" 8英寸;" LastAccessedTimeStamp ="" 1/1/2011 12:57:40 AM"" TimeFrame ="" 2011-01-01 00:01:00.000"" />
< User ID ="" 7""的LoginName = QUOT;" MSR2"" SuccessCount = QUOT;" 1 QUOT;" FailureCount = QUOT;" 3英寸;" LastAccessedTimeStamp ="" 1/1/2011 12:23:20 AM"" TimeFrame =""" 2011-01-01 00:01:00.000"" />
< User ID ="" 6""的LoginName = QUOT;" MSR1"" SuccessCount = QUOT;" 5英寸;" FailureCount = QUOT;" 3英寸;" LastAccessedTimeStamp ="" 1/1/2011 12:59:00 AM"" TimeFrame =""" 2011-01-01 00:01:00.000"" />
< / Users>
<Users>
<User ID=""10"" LoginName=""222"" SuccessCount=""3"" FailureCount=""2"" LastAccessedTimeStamp=""1/1/2011 12:55:00 AM"" TimeFrame=""2011-01-01 00:00:00.000""/>
<User ID=""5"" LoginName=""admin"" SuccessCount=""7"" FailureCount=""2"" LastAccessedTimeStamp=""1/1/2011 12:49:00 AM"" TimeFrame=""2011-01-01 00:00:00.000""/>
<User ID=""9"" LoginName=""2222"" SuccessCount=""2"" FailureCount=""8"" LastAccessedTimeStamp=""1/1/2011 12:57:40 AM"" TimeFrame=""2011-01-01 00:00:00.000""/>
<User ID=""7"" LoginName=""msr2"" SuccessCount=""1"" FailureCount=""3"" LastAccessedTimeStamp=""1/1/2011 12:23:20 AM"" TimeFrame=""2011-01-01 00:00:00.000""/>
<User ID=""6"" LoginName=""msr1"" SuccessCount=""5"" FailureCount=""3"" LastAccessedTimeStamp=""1/1/2011 12:59:00 AM"" TimeFrame=""2011-01-01 00:01:00.000""/>
<User ID=""10"" LoginName=""222"" SuccessCount=""3"" FailureCount=""2"" LastAccessedTimeStamp=""1/1/2011 12:55:00 AM"" TimeFrame=""2011-01-01 00:01:00.000""/>
<User ID=""5"" LoginName=""admin"" SuccessCount=""7"" FailureCount=""2"" LastAccessedTimeStamp=""1/1/2011 12:49:00 AM"" TimeFrame=""2011-01-01 00:01:00.000"" />
<User ID=""9"" LoginName=""2222"" SuccessCount=""2"" FailureCount=""8"" LastAccessedTimeStamp=""1/1/2011 12:57:40 AM"" TimeFrame=""2011-01-01 00:01:00.000"" />
<User ID=""7"" LoginName=""msr2"" SuccessCount=""1"" FailureCount=""3"" LastAccessedTimeStamp=""1/1/2011 12:23:20 AM"" TimeFrame=""2011-01-01 00:01:00.000""/>
<User ID=""6"" LoginName=""msr1"" SuccessCount=""5"" FailureCount=""3"" LastAccessedTimeStamp=""1/1/2011 12:59:00 AM"" TimeFrame=""2011-01-01 00:01:00.000""/>
</Users>
推荐答案
string xml = @"<Users>
<User ID=""10"" LoginName=""222"" SuccessCount=""3"" FailureCount=""2"" LastAccessedTimeStamp=""1/1/2011 12:55:00 AM"" TimeFrame=""2011-01-01 00:00:00.000""/>
<User ID=""5"" LoginName=""admin"" SuccessCount=""7"" FailureCount=""2"" LastAccessedTimeStamp=""1/1/2011 12:49:00 AM"" TimeFrame=""2011-01-01 00:00:00.000""/>
<User ID=""9"" LoginName=""2222"" SuccessCount=""2"" FailureCount=""8"" LastAccessedTimeStamp=""1/1/2011 12:57:40 AM"" TimeFrame=""2011-01-01 00:00:00.000""/>
<User ID=""7"" LoginName=""msr2"" SuccessCount=""1"" FailureCount=""3"" LastAccessedTimeStamp=""1/1/2011 12:23:20 AM"" TimeFrame=""2011-01-01 00:00:00.000""/>
<User ID=""6"" LoginName=""msr1"" SuccessCount=""5"" FailureCount=""3"" LastAccessedTimeStamp=""1/1/2011 12:59:00 AM"" TimeFrame=""2011-01-01 00:01:00.000""/>
<User ID=""10"" LoginName=""222"" SuccessCount=""3"" FailureCount=""2"" LastAccessedTimeStamp=""1/1/2011 12:55:00 AM"" TimeFrame=""2011-01-01 00:01:00.000""/>
<User ID=""5"" LoginName=""admin"" SuccessCount=""7"" FailureCount=""2"" LastAccessedTimeStamp=""1/1/2011 12:49:00 AM"" TimeFrame=""2011-01-01 00:01:00.000"" />
<User ID=""9"" LoginName=""2222"" SuccessCount=""2"" FailureCount=""8"" LastAccessedTimeStamp=""1/1/2011 12:57:40 AM"" TimeFrame=""2011-01-01 00:01:00.000"" />
<User ID=""7"" LoginName=""msr2"" SuccessCount=""1"" FailureCount=""3"" LastAccessedTimeStamp=""1/1/2011 12:23:20 AM"" TimeFrame=""2011-01-01 00:01:00.000""/>
<User ID=""6"" LoginName=""msr1"" SuccessCount=""5"" FailureCount=""3"" LastAccessedTimeStamp=""1/1/2011 12:59:00 AM"" TimeFrame=""2011-01-01 00:01:00.000""/>
</Users>";
XDocument doc = XDocument.Parse(xml);
var groups = from user in doc.Root.Elements("User")
group user by DateTimeOffset.Parse(user.Attribute("TimeFrame").Value.Replace(' ', 'T')) into g
select new
{
timeFrame = g.Key,
successCount = g.Attributes("SuccessCount").Sum(s => (int)s)
};
foreach (var group in groups)
{
Console.WriteLine("TimeFrame: {0}; Count: {1}", group.timeFrame, group.successCount);
}
这篇关于基于属性的XML组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!