如何使用C#和linq从此XML块获取所需的数据?

时间:2015-02-16 23:03:48

标签: c# xml linq

我有这个XML数据,我试图使用linq将其放入C#类列表中。

<Competency name="GRAPHICAL USER INTERFACE">
    <CompetencyId id="7310031" /> 
    <TaxonomyId id="731" idOwner="10" description="Sovren" /> 
    <CompetencyEvidence name="GRAPHICAL USER INTERFACE" typeDescription="Found in SKILLS; WORK HISTORY; POS-2; POS-3; POS-4; POS-5; POS-6" typeId="7310031" lastUsed="2010-01-01">
        <NumericValue description="Total Months">204</NumericValue>
    </CompetencyEvidence>
</Competency>

我的代码如下。我尝试抓取值,获取不同的值,然后将它们填充到列表中。我哪里错了?

    var skills = from competency in xmlData.Descendants(ns + "CompetencyEvidence")
        select new 
        {
            Name = competency.Attribute("name").Value,
            Months = from p in xmlData.Descendants(ns + "NumericValue")
                select new SkillDataContract()
                {
                    Months = p.Attribute("description").Value
                }
        };

        //Get DISTINCT
        var distinctSkills = skills.GroupBy(x => x.Name).Select(y => y.First());


        var processedSkills = distinctSkills.Select(item => new SkillDataContract()
        {
            Name = item.Name,
            Months = item.Months.ToString()
        }).ToList();

谢谢, 麦克

0 个答案:

没有答案