【发布时间】:2012-09-28 09:38:49
【问题描述】:
我是 .net 初学者。
我正在尝试使用 linq 更新我的 xml file。
我被困在它的第一点,即我无法使用 linq 从 xml 文件中获取值。
这些是我在代码中使用的控件:
cbBrandName -- combobox
cbProduct -- combobox
txtQuantity -- TextBox
我正在尝试以下代码:
XElement doc = XElement.Load(@"..\..\stock.xml");
string quantity = doc.Descendants("quantity")
.Select(y => y.Element("quantity").Value.Equals(txtQuantity.Text))
/*red scribbles to 'Element' in 'where'*/
.Where(x => x.Element("productname").Value.Equals(cbProduct.Text) &&
x.Element("brandname").Value.Equals(cbBrandName.Text)).ToString();
MessageBox.Show(quantity.ToString());
在这里,我试图将“数量”值存储在quantity 字符串中,以便稍后可以对其进行操作,然后再次更新到我的 xml 文件。
当我将.select 作为注释时,它没有显示任何错误,但是当我运行它时,它在 MessageBox 中显示一些 system.linq.Enumerable + ..... 而不是文本。
编辑:
当我最后给出.toString() 时。它显示错误 - “对象引用未设置为对象的实例。”当我运行它时。
请帮忙
提前致谢。
【问题讨论】:
-
错误:错误 1 'bool' 不包含 'Element' 的定义,并且找不到接受类型为 'bool' 的第一个参数的扩展方法 'Element'(您是否缺少 using 指令还是程序集参考?)
-
只需向我们展示 xml 格式并得到您想要的结果。
-
我在我的问题中给出了 xml 格式的链接。请检查
标签: c# xml linq c#-4.0 updates