我正在尝试使用Linq和XML创建一个类的实例。我在我的代码中收到以下错误,不知道如何解决它。我的代码编辑器告诉我下面代码中的“select”是罪魁祸首。我对Linq很新,所以感谢任何帮助。
无法将类型'System.Collections.Generic.IEnumerable'隐式转换为'gcDiversityVision.VisionData'。存在显式转换(您是否错过了演员?)
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>
答案 0 :(得分:1)
您已告知它为文档中的每个 VisonData
元素创建一个新的gcDiversityVision
。因此,结果是一系列VisionData
个对象。
你究竟想做什么 - 创造一个,或创造多少?有很多选择 - 如果您向我们提供有关您尝试做什么的更多信息,我们可以为您提供更多帮助。
假设您拥有VisionData
类,我还建议您编写静态VisionData.FromXElement
方法(或将其放在其他位置),以便您的查询不是那么大。
XElement root = visionXml.Root;
VisionData newVisionData = new VisionData(...);
顺便说一句,如果您使用ToList
扩展方法而非new List<string>(...)
,则可能会使查询更简单一些。或者更改VisionData
构造函数以接受IEnumerable<string>
类型的参数而不是List<string>
,因此您无需在构造函数调用中进行转换
答案 1 :(得分:0)
我认为您可能需要修改您的查询,如:
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();