Linq查询对象引用未设置为对象的实例.2.
本文关键字:对象 实例 设置 查询 对象引用 Linq | 更新日期: 2023-09-27 18:25:41
这返回了正确的Element原因,在next读取的intel意义上像这样的"脱机",我正试图将"脱机"值更改为"就绪"。
public void ChangeConnectionStatus(string SelectedFile)
{
System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(@"C:'Users'Rick'Documents'Visual Studio 2010'Projects'Server'server.config'DC_Classes'");
//Getting All file names from the directory info
System.IO.FileInfo[] fileNames = dirInfo.GetFiles(SelectedFile + "*.xml*");
//Foreach itterator
foreach (System.IO.FileInfo fi in fileNames)
{
XElement main = XElement.Load(fi.FullName);
IEnumerable<XElement> Nongroups = from nexp in main.XPathSelectElements("Network/Posted_Status")
where nexp.Element("Posted_Status").Value == "Offline"
select nexp;
////Handle the process here
foreach (XElement nexp in Nongroups)
{
DialogResult Yes = MessageBox.Show("This Will Online This Group Are You Sure You Want To Do This","System Info",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
if (Yes == DialogResult.Yes)
{
nexp.SetValue("Ready");
}
}
}
}
如果您不确定对象的类型,那么只需使用var
foreach (var nexp in Nongroups)
并以这种方式分配值:
if (Yes == DialogResult.Yes)
{
nexp.Element("Posted_Status").Value = "Ready";
}
我的猜测是,正如您在这个SO线程中看到的那样,您缺少了名称空间。所以我希望nexp
在以下行被设置为空:
来自main中的nextp。XPathSelectElements("网络/发布状态")
您也可以阅读此MSDN线程。
试试看,希望有帮助!