[英]DataBinding: 'System.Xml.linq.XElement' does not contain a property with the name 'colorName'
Having a hard time following other examples since they don't seem to the follow XML. 由于很难遵循其他示例,因此很难遵循其他示例。 Basically I am trying to bind a list of data I am querying (LINQ to XML) to a repeater object.
基本上,我试图将要查询的数据列表(LINQ to XML)绑定到转发器对象。 Here is my xml file of focus
这是我关注的xml文件
<?xml version="1.0" encoding="utf-8" ?>
<colors>
<color>
<id>1</id>
<colorName>Red</colorName>
</color>
<color>
<id>2</id>
<colorName>Orange</colorName>
</color>
<color>
<id>3</id>
<colorName>Yellow</colorName>
</color>
<color>
<id>4</id>
<colorName>Green</colorName>
</color>
<color>
<id>5</id>
<colorName>Blue</colorName>
</color>
<color>
<id>6</id>
<colorName>Indigo</colorName>
</color>
<color>
<id>7</id>
<colorName>Violet</colorName>
</color>
</colors>
Here is my code that will respond off the event of a dropdown selection: 这是我的代码,将响应下拉选择的事件:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//Grab data from selected index
string color = DropDownList1.SelectedValue;
string inputUrl = "C:/Users/JJ/Documents/Visual Studio 2012/Projects/Colors/Colors/Xml/Colors.xml";
using (XmlReader reader = XmlReader.Create(inputUrl))
{
//Read through xml file
while (reader.Read())
{
//Creating the xml document object for use of the xml file
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(reader);
//Creating a xDocument object for querying purposes
XDocument colors = XDocument.Load(inputUrl);
//List that will contain all the colors of the xml file
XmlNodeList nameList = xmlDoc.GetElementsByTagName("colorName");
int colorIndex = Convert.ToInt32(color)-1;
//Will display the output in a header based off the dropdown selection
switch (color)
{
case "1":
outputLabel1.Text = "My favorite color is " + nameList[colorIndex].InnerXml;
break;
case "2":
outputLabel1.Text = "My favorite color is " + nameList[colorIndex].InnerXml;
break;
case "3":
outputLabel1.Text = "My favorite color is " + nameList[colorIndex].InnerXml;
break;
case "4":
outputLabel1.Text = "My favorite color is " + nameList[colorIndex].InnerXml;
break;
case "5":
outputLabel1.Text = "My favorite color is " + nameList[colorIndex].InnerXml;
break;
case "6":
outputLabel1.Text = "My favorite color is " + nameList[colorIndex].InnerXml;
break;
case "7":
outputLabel1.Text = "My favorite color is " + nameList[colorIndex].InnerXml;
break;
default:
outputLabel1.Text = "Please choose a color.";
break;
}
outputLabel2.Text = "List of colors remaining: ";
var colorsLeft = from _id in colors.Descendants("colorName")
//where _id.Element("id").Value != color
select _id.Value;
Repeater1.DataSource = colorsLeft;
Repeater1.DataBind();
}
}
}
What am I missing for my XElement not to know of the "colorName" property? 我的XElement不知道“ colorName”属性时会缺少什么? It's in the xml file.
它在xml文件中。 Any suggestions for help/or better understanding will be appreciated.
任何帮助/更好理解的建议将不胜感激。
UPDATE: I am adding the piece for my repeater control that is suppose to bind the data. 更新:我为我的转发器控件添加了一部分,该控件应该绑定数据。
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label runat="server" ID="Label1" Text='<%# Bind("colorName") %>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
UPDATE: 更新:
I think I know what's going on. 我想我知道发生了什么事。 Haven't had a good chance to get back at this.
还没有很好的机会来解决这个问题。 I am binding two different things in my repeater control.
我在转发器控件中绑定了两个不同的东西。 The first time I bind the entire XML File as a dataset and the binding recognizes the colorName property in the file.
我第一次将整个XML文件作为数据集进行绑定,并且绑定可以识别文件中的colorName属性。 However in my functionality where I select a different color from the dropdown list, and my query selects the remaining colors, the resultant is an IEnumerable String.
但是,在我的功能中,从下拉列表中选择其他颜色,然后查询选择其余颜色,结果是IEnumerable String。 This IEnumerable only contains the text values I queried so there's no knowledge of the colorName property when I try to bind the new data.
该IEnumerable仅包含我查询的文本值,因此当我尝试绑定新数据时不了解colorName属性。 I think this is why I'm getting the error I'm dealing with.
我认为这就是为什么我遇到正在处理的错误。 I could be wrong so please let me know otherwise.
我可能是错的,否则请让我知道。
So now I have to find a way to set up binding two different things. 所以现在我必须找到一种方法来设置绑定两个不同的东西。 If what I have found is the problem anyone have any suggestions?
如果我发现问题所在,那么有人有什么建议吗?
According to your comment, the problem is in the LINQ-to-XML. 根据您的评论,问题出在LINQ-to-XML中。 So I'd suggest to try this LINQ :
所以我建议尝试一下LINQ:
var colorsLeft = from c in colors.Descendants("color")
where (string)c.Element("id") != color
select (string)c.Element("colorName");
Above LINQ will select all color names string those aren't selected in the dropdownlist. 在LINQ上方将选择所有未在下拉列表中选择的颜色名称字符串。
Mmm.. but I don't really understand how this relates to the error message you posted as the question title.. 嗯..但是我不太了解这与您以问题标题发布的错误消息有何关系。
UPDATE : 更新:
Turned out that you have another problem which I think is in your binding statement. 原来您有另一个问题,我认为这是您的约束性声明中的问题。 The binding expect binding source is a collection of object having property named
colorName
. 绑定期望绑定源是具有名为
colorName
属性的对象的集合。 You can either change your binding or change the LINQ to fix the error. 您可以更改绑定或更改LINQ来修复错误。
Since I can't see your binding, I can only give example for the latter : 由于看不到您的绑定,因此我只能举一个例子:
var colorsLeft = from c in colors.Descendants("color")
where (string)c.Element("id") != color
select new { colorName = (string)c.Element("colorName") };
Above LINQ return collection of anonymous object having single property, colorName
. 在LINQ之上,返回具有单个属性
colorName
的匿名对象的集合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.