我是.net 初学者。
我正在尝试使用 linq更新我的xml 文件。
我被困在它的第一点,即我无法使用 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
发表评论时,它没有显示任何错误,但是当我运行它时,它system.linq.Enumerable + .....
在 MessageBox 中显示一些而不是文本。
编辑:
当我.toString()
最后给它的时候。它显示错误 - “对象引用未设置为对象的实例。” 当我运行它时。
请帮助
提前致谢。