theDawckta
theDawckta

Reputation: 874

Help creating an instance of a class using Linq and XML

I am trying to create an instance of a class using Linq and XML. I am getting the following error in my code and do not know how to fix it. My code editor is telling me the "select" in the following code is the culprit. I am very new to Linq so any help is appreciated.

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'gcDiversityVision.VisionData'. An explicit conversion exists (are you missing a cast?)

XElement visionXML = XElement.Parse(e.Result);

            VisionData newVisionData = new VisionData(new List<string>(visionXML.Descendants(ns + "Employee").Select(f => f.Element(ns + "EmployeeName").Value)),
                                                        new List<string>(visionXML.Descendants(ns + "Employee").Select(f => f.Element(ns + "EmployeeTitle").Value)),
                                                        new List<string>(visionXML.Descendants(ns + "Employee").Select(f => f.Element(ns + "include").Attribute("externalPath").Value)),
                                                        new List<string>(visionXML.Descendants(ns + "Employee").Select(f => f.Element(ns + "EmployeeVideo").Value)),
                                                        visionXML.Element(ns + "LinkHeaderText").Value,
                                                        visionXML.Element(ns + "ButtonLinkText").Value,
                                                        visionXML.Element(ns + "ButtonLinkURL").Value,
                                                        new List<string>(visionXML.Descendants(ns + "Links").Select(f => f.Element(ns + "LinkURL").Value)),
                                                        new List<string>(visionXML.Descendants(ns + "Links").Select(f => f.Element(ns + "LinkText").Value)),
                                                        new List<string>(visionXML.Descendants(ns + "EmployeeStoryHeaderParagraph").Select(f => f.Element(ns + "EmployeeHeader").Value)),
                                                        new List<string[]>(visionXML.Descendants(ns + "EmployeeStoryHeaderParagraph").Select(f => f.Descendants(ns + "EmployeeParagraphs").Select(ep => ep.Element(ns + "EmployeeParagraph").Value).ToArray()))
                                                    );
<c:gcDiversityVision>
<c:Employee>
  <c:EmployeeName>Employee Name 1</c:EmployeeName>
  <c:EmployeeTitle>EmployeeTitle 1</c:EmployeeTitle>
  <c:EmployeeIconImage>
    <c:include type="Image" resolve="false" sourcedFrom="local" externalPath="/global/hrit/Careers/PublishingImages/down_carat.gif" height="7" width="12" query="">/hrit/Careers/PublishingImages/down_carat.gif</c:include>
  </c:EmployeeIconImage>
  <c:EmployeeVideo>mms://msnvidweb.wmod.msecnd.net/a10026/e1/ds/us/CMG_US/CMG_Microsoft/8F036573-ADAD-40B3-B2A2-A070E6C970B2.wmv</c:EmployeeVideo>
</c:Employee>
<c:Employee>
  <c:EmployeeName>Employee Name 2</c:EmployeeName>
  <c:EmployeeTitle>Employee Title 2</c:EmployeeTitle>
  <c:EmployeeIconImage>
    <c:include type="Image" resolve="false" sourcedFrom="local" externalPath="/global/hrit/Careers/PublishingImages/2nav_bg.png" height="29" width="2" query="">/hrit/Careers/PublishingImages/2nav_bg.png</c:include>
  </c:EmployeeIconImage>
  <c:EmployeeVideo>mms://msnvidweb.wmod.msecnd.net/a10026/e1/ds/us/CMG_US/CMG_Microsoft/BE4A3DF0-15FB-4610-A478-F681FCBE2DFA.wmv</c:EmployeeVideo>
</c:Employee>
<c:VisionParagraph>
  <c:VisionHeaderParagraph>
    <c:VisionHeader>Vision Header 1</c:VisionHeader>
    <c:VisionParagraphs>
      <c:VisionParagraph>Vision Paragraph 1.1</c:VisionParagraph>
    </c:VisionParagraphs>
    <c:VisionParagraphs>
      <c:VisionParagraph>Vision Paragraph 1.2</c:VisionParagraph>
    </c:VisionParagraphs>
  </c:VisionHeaderParagraph>
  <c:VisionHeaderParagraph>
    <c:VisionHeader>Vision Header 2</c:VisionHeader>
    <c:VisionParagraphs>
      <c:VisionParagraph>Vision Paragraph 2.1</c:VisionParagraph>
    </c:VisionParagraphs>
    <c:VisionParagraphs>
      <c:VisionParagraph>Vision Paragraph 2.2</c:VisionParagraph>
    </c:VisionParagraphs>
  </c:VisionHeaderParagraph>
</c:VisionParagraph>
<c:Footer>
  <c:Button>
    <c:ButtonLinkText>Button Link Text</c:ButtonLinkText>
    <c:ButtonLinkURL>http://www.bing.com/</c:ButtonLinkURL>
  </c:Button>
  <c:LinkHeaderText>Link Text 2</c:LinkHeaderText>
  <c:Links>
    <c:LinkText>Link Text 1</c:LinkText>
    <c:LinkURL>http://www.bing.com/</c:LinkURL>
  </c:Links>
  <c:Links>
    <c:LinkText>Link Text 2</c:LinkText>
    <c:LinkURL>http://www.bong.com/</c:LinkURL>
  </c:Links>
  <c:Links>
    <c:LinkText>Link Text 3</c:LinkText>
    <c:LinkURL>http://www.bing.com/</c:LinkURL>
  </c:Links>
  <c:Links>
    <c:LinkText>Link Text 4</c:LinkText>
    <c:LinkURL>http://www.bong.com/</c:LinkURL>
  </c:Links>
</c:Footer>

Upvotes: 0

Views: 609

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500525

You've told it to create a new VisonData for each gcDiversityVision element in your document. The result is therefore a sequence of VisionData objects.

What are you actually trying to do - create one, or create many? There are lots of options available - if you give us more information about what you're trying to do, we can help you more.

Assuming you own the VisionData class, I would also suggest that you write a static VisionData.FromXElement method (or put it somewhere else) so that your query isn't quite so huge.

EDIT: Okay, based on the comment, it sounds like you don't want a LINQ query at all. Just:

XElement root = visionXml.Root;
VisionData newVisionData = new VisionData(...);

By the way, if you use the ToList extension method rather than new List<string>(...) it's likely to make the query a bit simpler. Or change the VisionData constructor to accept parameters of type IEnumerable<string> instead of List<string>, so you don't need to do the conversion within your constructor call at all.

Upvotes: 1

Siva Gopal
Siva Gopal

Reputation: 3502

I think you may need to modify your query like :

List<VisionData> newVisionData = (from data in visionXML.Descendants(ns + "gcDiversityVision")
                            select new VisionData(new List<string>(data.Descendants(ns + "Employee").Select(f => f.Element(ns + "EmployeeName").Value)),
                                            new List<string>(data.Descendants(ns + "Employee").Select(f => f.Element(ns + "EmployeeTitle").Value)),
                                            new List<string>(data.Descendants(ns + "Employee").Select(f => f.Element(ns + "include").Attribute("externalPath").Value)),
                                            new List<string>(data.Descendants(ns + "Employee").Select(f => f.Element(ns + "EmployeeVideo").Value)),
                                            data.Element(ns + "LinkHeaderText").Value,
                                            new List<string>(data.Descendants(ns + "Links").Select(f => f.Element(ns + "LinkURL").Value)),
                                            new List<string>(data.Descendants(ns + "Links").Select(f => f.Element(ns + "LinkText").Value)),
                                            new List<string>(data.Descendants(ns + "EmployeeStoryHeaderParagraph").Select(f => f.Element(ns + "EmployeeHeader").Value)),
                                            new List<string[]>(data.Descendants(ns + "EmployeeStoryHeaderParagraph").Select(f => f.Descendants(ns + "EmployeeParagraphs").Select(ep => ep.Element(ns + "EmployeeParagraph").Value).ToArray()))
                        )).ToList();

Upvotes: 0

Related Questions