這是在intel-sense中返回正確的元素原因nexp讀取 這樣離線我試圖將離線值的元素更改爲就緒。Linq查詢對象引用未設置爲對象的實例。 2
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");
}
}
}
}
看起來像「Posted_Status」元素可能不存在,因此'.Value'會拋出。您可以使用顯式轉換運算符而不是'.Value'像這樣'(string)nexp.Element(「Posted_Status」)' – Pawel
您的XML看起來像什麼? –