使用LINQ创建对象

时间:2010-07-21 18:52:45

标签: c# .net xml linq

我有一个看起来像这样的xml文件;

<Employee>
     <EmployeeName>Burt Reynolds</EmployeeName>
     <EmployeeTitle>Bad Ass</EmployeeTitle>
     <EmployeeStory>
        <EmployeeStoryHeaderParagraph>
           <EmployeeHeader>Employee Header 1</EmployeeHeader>
           <EmployeeParagraphs>
              <EmployeeParagraph>Employee Paragraph 1.1</EmployeeParagraph>
           </EmployeeParagraphs>
           <EmployeeParagraphs>
              <EmployeeParagraph>Employee Paragraph 1.2</EmployeeParagraph>
           </EmployeeParagraphs>
        </EmployeeStoryHeaderParagraph>
        <EmployeeStoryHeaderParagraph>
           <EmployeeHeader>Employee Header 2</EmployeeHeader>
           <EmployeeParagraphs>
              <EmployeeParagraph>Employee Paragraph 2.1</EmployeeParagraph>
           </EmployeeParagraphs>
           <EmployeeParagraphs>
              <EmployeeParagraph>Employee Paragraph 2.2</EmployeeParagraph>
           </EmployeeParagraphs>
        </EmployeeStoryHeaderParagraph>
     </EmployeeStory>
     <EmployeeImage>
        <include type="Image" resolve="false" sourcedFrom="local" externalPath="/PublishingImages/2nav_bg.png" height="29" width="2" query="">/PublishingImages/2nav_bg.png</include>
     </EmployeeImage>
     <EmployeeSigImage>
        <include type="Image" resolve="false" sourcedFrom="local" externalPath="/PublishingImages/down_carat.gif" height="7" width="12" query="">/PublishingImages/down_carat.gif</include>
     </EmployeeSigImage>
     <EmployeeVideo>http://sandbox/RichMedia/Robotica_720.wmv</EmployeeVideo>
  </Employee>

Employee标记从1到n。 EmployeeStoryHeaderParagraph标记从1到n。 EmployeeParagraphs标记从1到n。

我正在尝试使用此数据创建一个对象并将其添加到列表中,但我仍然坚持抓住标题和段落。目前代码看起来像这样。

XDocument employeeXML = XDocument.Parse(e.Result);

employeeList = (from employee in employeeXML.Descendants(ns + "Employee")
                select new Employee(employee.Element(ns + "EmployeeName").Value,
                                    employee.Element(ns + "EmployeeTitle").Value,
                                    employee.Element(ns + "EmployeeImage").Element(ns + "include").Attribute("externalPath").Value,
                                    employee.Element(ns + "EmployeeSigImage").Element(ns + "include").Attribute("externalPath").Value,
                                    employee.Element(ns + "EmployeeVideo").Value,
                                    headers,
                                    content
                )).ToList();

Employee是我创建的一个类,它将此作为一个构造函数;

public Employee(string _employeeName, string _employeeTitle, string _employeeImage, string _employeeSigImage, string _employeeMovieUri, List<string> _employeeHeader, List<string[]> _employeeContent)

当我在上面的Linq语句中找到标题时,我需要它来完成并从当前员工创建一个标题列表,当它到达内容时我需要一个包含与之关联的EmployeeParagraphs的字符串数组的列表那个标题。所以header [1]将是内容[1]段落的标题。我不知道如何在Linq中执行此操作,我可以只将代码添加到上面显示的标题和内容以创建新列表,还是在进入此列表之前执行此操作?

也许有一种比我正在尝试的更直接的方法吗?

1 个答案:

答案 0 :(得分:1)

选择是你的朋友而List&lt;&gt;()有一个构造函数,它带有你可以利用的IEnumerable。

XDocument employeeXML = XDocument.Parse(e.Result); 

            employeeList = (from employee in employeeXML.Descendants(ns + "Employee") 
                            select new Employee(employee.Element(ns + "EmployeeName").Value, 
                                                employee.Element(ns + "EmployeeTitle").Value, 
                                                employee.Element(ns + "EmployeeImage").Element(ns + "include").Attribute("externalPath").Value, 
                                                employee.Element(ns + "EmployeeSigImage").Element(ns + "include").Attribute("externalPath").Value, 
                                                employee.Element(ns + "EmployeeVideo").Value, 
                                                New List<string>(employee.Descendants("EmployeeStoryHeaderParagraph").Select(e => e.Element("EmployeeHeader").Value)), 
                                                New List<string[]>(employee.Descendants("EmployeeStoryHeaderParagraph").Select(e => e.Descendants("EmployeeParagraphs").Select(ep => ep.Element("EmployeeParagraph").Value).ToArray()))
                            )).ToList();

但是,如果你使用Employee的构造函数,你可能会优化它。允许构造函数为头和内容传入IEnumerable,然后在类本身内解析。这样你可以“简化”LINQ:

            employeeList = (from employee in employeeXML.Descendants(ns + "Employee") 
                            select new Employee(employee.Element(ns + "EmployeeName").Value, 
                                                employee.Element(ns + "EmployeeTitle").Value, 
                                                employee.Element(ns + "EmployeeImage").Element(ns + "include").Attribute("externalPath").Value, 
                                                employee.Element(ns + "EmployeeSigImage").Element(ns + "include").Attribute("externalPath").Value, 
                                                employee.Element(ns + "EmployeeVideo").Value, 
                                                employee.Descendants("EmployeeStoryHeaderParagraph")
                            )).ToList();