![](/img/trans.png)
[英]System.Xml.Linq.XElement' does not contain a definition for 'XPathEvaluate
[英]DataBinding: 'System.Xml.linq.XElement' does not contain a property with the name 'colorName'
由於很難遵循其他示例,因此很難遵循其他示例。 基本上,我試圖將要查詢的數據列表(LINQ to XML)綁定到轉發器對象。 這是我關注的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>
這是我的代碼,將響應下拉選擇的事件:
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();
}
}
}
我的XElement不知道“ colorName”屬性時會缺少什么? 它在xml文件中。 任何幫助/更好理解的建議將不勝感激。
更新:我為我的轉發器控件添加了一部分,該控件應該綁定數據。
<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>
更新:
我想我知道發生了什么事。 還沒有很好的機會來解決這個問題。 我在轉發器控件中綁定了兩個不同的東西。 我第一次將整個XML文件作為數據集進行綁定,並且綁定可以識別文件中的colorName屬性。 但是,在我的功能中,從下拉列表中選擇其他顏色,然后查詢選擇其余顏色,結果是IEnumerable String。 該IEnumerable僅包含我查詢的文本值,因此當我嘗試綁定新數據時不了解colorName屬性。 我認為這就是為什么我遇到正在處理的錯誤。 我可能是錯的,否則請讓我知道。
所以現在我必須找到一種方法來設置綁定兩個不同的東西。 如果我發現問題所在,那么有人有什么建議嗎?
根據您的評論,問題出在LINQ-to-XML中。 所以我建議嘗試一下LINQ:
var colorsLeft = from c in colors.Descendants("color")
where (string)c.Element("id") != color
select (string)c.Element("colorName");
在LINQ上方將選擇所有未在下拉列表中選擇的顏色名稱字符串。
嗯..但是我不太了解這與您以問題標題發布的錯誤消息有何關系。
更新:
原來您有另一個問題,我認為這是您的約束性聲明中的問題。 綁定期望綁定源是具有名為colorName
屬性的對象的集合。 您可以更改綁定或更改LINQ來修復錯誤。
由於看不到您的綁定,因此我只能舉一個例子:
var colorsLeft = from c in colors.Descendants("color")
where (string)c.Element("id") != color
select new { colorName = (string)c.Element("colorName") };
在LINQ之上,返回具有單個屬性colorName
的匿名對象的集合。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.