开发者

Creating objects using LINQ

I have an xml file that looks like this;

<Employee>
     <EmployeeName>Burt Reynolds</EmployeeName>
     <EmployeeTitle>Bad Ass</EmployeeTitle>
     <EmployeeStory>
        <EmployeeStoryHeaderParagraph>
           <EmployeeHeader>Employee Header 1</EmployeeHeader>
           <EmployeeParagraphs>
              <EmployeeParagraph>Employee Paragraph 1.1</EmployeeParagraph>
     开发者_如何学Python      </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>

The Employee tag goes 1 to n. The EmployeeStoryHeaderParagraph tag goes 1 to n. The EmployeeParagraphs tag goes 1 to n.

I am trying to create an object and add it to a list using this data but I am stuck on grabbing the header and the paragraphs. Currently the code looks like this.

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 is a class I have created that takes this as a contructor;

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

When I get to headers in my Linq statement above I need it to go through and create a List of headers from the current employee it is on, when it gets to content I need a List of string arrays containing the EmployeeParagraphs associated with that header. So header[1] would be the header of content[1] string of paragraphs. I don't know how to do this in Linq, can I just add code to where header and content appears above to create a new list or do I do it before I get into this list?

Maybe there is a straight up better way to this than I am currently trying?


Select is your friend and List<>() has a constructor that takes an IEnumerable that you can leverage.

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();

You could probably optimize that if you played with your constructor for Employee, though. Allow the constructor to pass in an IEnumerable for both the headers and the content and then parse within the class itself. That way you can "simplify" the LINQ to:

            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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜