Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当节点不';我的XML文件中不存在吗?_C# - Fatal编程技术网

C# 当节点不';我的XML文件中不存在吗?

C# 当节点不';我的XML文件中不存在吗?,c#,C#,我有以下代码: 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/

我有以下代码:

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;
    }
}

文件ID和文件名并不总是存在于某些文件中。在这种情况下,如何避免NullReferenceException?

您可以执行以下操作:

string FileName= "";
string File_ID = "";

if (station.SelectSingleNode("FileName") != null)
    File_Name = station.SelectSingleNode("FileName").InnerXml;
if (station.SelectSingleNode("FileID") != null)       
    File_ID = station.SelectSingleNode("FileID").InnerXml;

如果变量不是空字符串,则继续处理。。。(“”)…

如果必须在很多地方进行检查,我会尝试这样做,以保持代码简单明了

public static class Helpers
{
   public static string GetInnerXml(this XmlNode node, string innerNodeName)
   {
     string innerXml = "";
     XmlNode innerNode = node.SelectSingleNode(innerNodeName);

     if (innerNode != null)
     {
       innerXml = innerNode.InnerXml;
     }

     return innerXml;
   }
}
像这样使用它

  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.GetInnerXml("//FileDump/Message/MsgID");

    Console.WriteLine("Message ID is :{0}", Message_ID);

    foreach (XmlNode station in stations)
    {
      var File_Name = station.GetInnerXml("FileName");
      var File_ID = station.GetInnerXml("FileID");
    }
  }
我通常使用

然后我可以执行
myElement.Attribute(“someAttribute”).GetValueIfNotNull()

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 fileNameNode = station.SelectSingleNode("FileName");
        var fileIdNode = station.SelectSingleNode("FileID");

        var File_Name = fileNameNode == null ? (string)null : fileNameNode.InnerXml;

        var File_ID = fileIdNode == null ? (string)null : fileIdNode.InnerXml;;
    }
}
public static string GetValueIfNotNull(this XmlAttribute xmlAttribute)
{
  if (xmlAttribute == null)
    {
      return null;
    }
  return xmlAttribute.Value;
}