我有以下代码:
static void Main(string[] args)
{
XmlDocument xml = new XmlDocument();
xml.Load(@"C:\MR.xml");
XmlNodeList stations = xml.SelectNodes("//FileDump/Message/Attachment");
var Message_ID = xml.SelectSingleNode("//FileDump/Message/MsgID").InnerXml;
Console.WriteLine("Message ID is :{0}", Message_ID);
foreach (XmlNode station in stations)
{
var File_Name = station.SelectSingleNode("FileName").InnerXml;
var File_ID = station.SelectSingleNode("FileID").InnerXml;
}
}
FileID 和 FileName 并不总是存在于某些文件中。在这种情况下如何避免 NullReferenceExceptions?