0

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>

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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.
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: 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.
How will it know what data.descendants is though if I do not use this "from data in visionXML.Descendants(ns + "gcDiversityVision")"
I have updated the code above, is this what you were thinking? Am I on the right track?
|
0

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

5 Comments

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.
May i know, what is that error. Is that same as the one shown above?
Line: 56 Error: 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)
Can you just put a break point and check whether the error is originating from the above LINQ code ?
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)),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.