tags:

views:

71

answers:

2

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/&lt;/c:ButtonLinkURL&gt;
  </c:Button>
  <c:LinkHeaderText>Link Text 2</c:LinkHeaderText>
  <c:Links>
    <c:LinkText>Link Text 1</c:LinkText>
    <c:LinkURL>http://www.bing.com/&lt;/c:LinkURL&gt;
  </c:Links>
  <c:Links>
    <c:LinkText>Link Text 2</c:LinkText>
    <c:LinkURL>http://www.bong.com/&lt;/c:LinkURL&gt;
  </c:Links>
  <c:Links>
    <c:LinkText>Link Text 3</c:LinkText>
    <c:LinkURL>http://www.bing.com/&lt;/c:LinkURL&gt;
  </c:Links>
  <c:Links>
    <c:LinkText>Link Text 4</c:LinkText>
    <c:LinkURL>http://www.bong.com/&lt;/c:LinkURL&gt;
  </c:Links>
</c:Footer>

A: 

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.

Jon Skeet
I am trying to create a single VisionData. gcDiversityVision is actually my root element in my xml. I have added the xml to give a better idea.
theDawckta
@theDawckta: See my edit.
Jon Skeet
What would I use in the "..." to grab the parameters for my VisionData object, can you give me a small example I can start with?
theDawckta
@theDawckta: The same code you've already got. You know how to construct a `VisionData` object from an `XElement` - it's just you don't need to use a query.
Jon Skeet
How will it know what data.descendants is though if I do not use this "from data in visionXML.Descendants(ns + "gcDiversityVision")"
theDawckta
I have updated the code above, is this what you were thinking? Am I on the right track?
theDawckta
@theDawckta: Yes, that would pretty much do it. I'm not sure what you're asking about "if you don't use `from data in visionXML.Descendants(...)`" - it will just use the data in the root element you've just parsed.
Jon Skeet
I am now getting an error in this selection I am trying to make, not sure why new List<string>(root.Descendants(ns + "Employee").Select(f => f.Descendants(ns + "EmployeeIconImage").Select(ep => ep.Element(ns + "include").Attribute("externalPath").Value))),
theDawckta
@theDawckta: "I am now getting an error" isn't very precise. You need to say *what* error you're getting.
Jon Skeet
A: 

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();
Siva Gopal
I tried this but I am now gettign an exception, the exception occurs in the code you showed above I am not sure what is wrong with my Linq statement. I have included the xml in the original post.
theDawckta
May i know, what is that error. Is that same as the one shown above?
Siva Gopal
Line: 56Error: Unhandled Error in Silverlight Application Code: 4004 Category: ManagedRuntimeError Message: System.NullReferenceException: Object reference not set to an instance of an object. at gcDiversityVision.MainPage.xmlClient_DownloadStringCompleted(Object sender, DownloadStringCompletedEventArgs e) at System.Net.WebClient.OnDownloadStringCompleted(DownloadStringCompletedEventArgs e)
theDawckta
Can you just put a break point and check whether the error is originating from the above LINQ code ?
Siva Gopal
Here is the line that the error occurs, also I have updated the code above. new List<string>(visionXML.Descendants(ns + "Employee").Select(f => f.Element(ns + "include").Attribute("externalPath").Value)),
theDawckta