Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.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#获取不';没有明确的近标记_C#_.net_Xml - Fatal编程技术网

c#获取不';没有明确的近标记

c#获取不';没有明确的近标记,c#,.net,xml,C#,.net,Xml,这是我的xml <?xml version="1.0" encoding="utf-8" ?> <SMS> <DeliveryDate>6/27/2015 3:00:00 PM</DeliveryDate> <Status>DELIVRD</Status> <Error /> </SMS> 但我有一个例外: An unhandled exception of type

这是我的xml

  <?xml version="1.0" encoding="utf-8" ?> 
 <SMS>
  <DeliveryDate>6/27/2015 3:00:00 PM</DeliveryDate> 
  <Status>DELIVRD</Status> 
  <Error /> 
  </SMS>
但我有一个例外:

An unhandled exception of type 'System.NullReferenceException' occurred in TestStatus.exe

Additional information: Object reference not set to an instance of an object.
在这一行:

string errorTagBody = doc.SelectSingleNode("ERROR").InnerText;
并非如此,xml有时可能是这样的:

  <?xml version="1.0" encoding="utf-8" ?> 
 <SMS>
  <DeliveryDate>6/27/2015 3:00:00 PM</DeliveryDate> 
  <Error>Not formatted mobile number </ERROR>
  </SMS>

2015年6月27日下午3:00:00
未格式化的手机号码
您应该使用with XPath语法

doc.SelectSingleNode("//Error").InnerText

这里的问题不是它是一个自动关闭标签。问题在于您对节点的选择

这意味着在文档的根目录下给我一个名为
ERROR
的元素。没有,所以它返回null,如果调用
。如果为null,则会得到null ref异常

string errorTagBody = doc.SelectSingleNode("ERROR").InnerText;
相反,您可以这样做,这意味着从文档中的任何位置为我获取一个
Error
元素

string errorTagBody = doc.SelectSingleNode("//Error").InnerText;    
或者,这意味着在路径SMS中获取error元素,后跟error

string errorTagBody = doc.SelectSingleNode("SMS/Error").InnerText;   

还有,;案例与XML有关。您不能使用
Error
关闭
Error
,它无效

试着像这样选择它

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(responseMessage);
    var node  = doc.DocumentElement.SelectSingleNode("//Error");

    if (null != node && string.IsNullOrEmpty( node.InnerText ))
    {

    }

第一个XML区分大小写,所以如果使用“Error”标记,请搜索“Error”而不是“Error”

第二,SelectSingleNode使用XPath,这是一种引用xml节点的方法(您可以通过在google上搜索XPath找到更多关于XPath的信息)。在您的情况下,正确的XPath是“//Error”或“SMS/Error”(每种XPath都有不同的工作方式,再次在google上搜索有关XPath的信息,以了解您想要使用哪种XPath)

运行此代码以查看发生了什么

        XmlDocument doc = new XmlDocument();
        doc.LoadXml("[YUOR XML PATH]");
        XmlNode node1 = doc.SelectSingleNode("ERROR");
        XmlNode node2 = doc.SelectSingleNode("Error");
        XmlNode node3 = doc.SelectSingleNode("//ERROR");
        XmlNode node4 = doc.SelectSingleNode("//Error");
        XmlNode node5 = doc.SelectSingleNode("SMS/ERROR");
        XmlNode node6 = doc.SelectSingleNode("SMS/Error");

        if (node1 == null)
        { Console.WriteLine("Node 1 is null"); }

        if (node2 == null)
        { Console.WriteLine("Node 2 is null"); }


        if (node3 == null)
        { Console.WriteLine("Node 3 is null"); }


        if (node4 == null)
        { Console.WriteLine("Node 4 is null"); }


        if (node5 == null)
        { Console.WriteLine("Node 5 is null"); }


        if (node6 == null)
        { Console.WriteLine("Node 6 is null"); }

我只是想把它贴上去……)
        XmlDocument doc = new XmlDocument();
        doc.LoadXml("[YUOR XML PATH]");
        XmlNode node1 = doc.SelectSingleNode("ERROR");
        XmlNode node2 = doc.SelectSingleNode("Error");
        XmlNode node3 = doc.SelectSingleNode("//ERROR");
        XmlNode node4 = doc.SelectSingleNode("//Error");
        XmlNode node5 = doc.SelectSingleNode("SMS/ERROR");
        XmlNode node6 = doc.SelectSingleNode("SMS/Error");

        if (node1 == null)
        { Console.WriteLine("Node 1 is null"); }

        if (node2 == null)
        { Console.WriteLine("Node 2 is null"); }


        if (node3 == null)
        { Console.WriteLine("Node 3 is null"); }


        if (node4 == null)
        { Console.WriteLine("Node 4 is null"); }


        if (node5 == null)
        { Console.WriteLine("Node 5 is null"); }


        if (node6 == null)
        { Console.WriteLine("Node 6 is null"); }