Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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# 无法使用selectSingleNode方法获取节点_C#_Xml - Fatal编程技术网

C# 无法使用selectSingleNode方法获取节点

C# 无法使用selectSingleNode方法获取节点,c#,xml,C#,Xml,我的xml文件如下所示: <?xml version="1.0"?> <UpdateInboundim613Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" status="SUCCESS" message="Success" schemaRevisionDate="2016-07-19" schemaRevisi

我的xml文件如下所示:

<?xml version="1.0"?>
<UpdateInboundim613Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" status="SUCCESS" message="Success" schemaRevisionDate="2016-07-19" schemaRevisionLevel="0" returnCode="0" xmlns="http://schemas.hp.com/SM/7">
    <model>
        <keys>
            <TransactionID type="String">E-InHPSXIM1089779</TransactionID>
        </keys>
        <instance uniquequery="TransactionID=&quot;E-InHPSXIM1089779&quot;" recordid="E-InHPSXIM1089779">
            <TransactionDetailedTrace xsi:nil="true" />
            <TransactionMessage type="Array">
                <TransactionMessage type="String" />
            </TransactionMessage>
            <OVSCSolutionDescription type="Array">
                <OVSCSolutionDescription type="String">Issue: EL-BANK OUTSIDE WAREHOUSE EGYPT IMEA[2702]Interface[[E1 0/0/0]]|Router|ELBKCE1GW /pg-h-pg1252-256675160|143.34.213.18|Down Solution: As per update from Mai Shrief that the site has been suspended on 30th June. So no need of any investigation. Resolved By: BT NOC</OVSCSolutionDescription>
            </OVSCSolutionDescription>
            <OVSCTicketID type="String">E-IM004004076</OVSCTicketID>
            <RIMSImpact xsi:nil="true" />
            <attachments />
        </instance>
    </model>
    <messages>
        <message type="String" xmlns="http://schemas.hp.com/SM/7/Common">TransactionStatusDetail in $L.file is:IM Ticket: E-IM004004076 is valid for Update Subtype: Resolve</message>
    </messages>
</UpdateInboundim613Response>

您能告诉我问题是什么以及如何解决吗?

您的Xml文档使用默认名称空间“”。您需要使用XmlNamespaceManager在此命名空间下选择该节点

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"C:\zzx\Project\SM\R5.1\Harness\InBound.xml");
var namespaceManager = new XmlNamespaceManager(xmlDoc.NameTable);
namespaceManager.AddNamespace("ns", "http://schemas.hp.com/SM/7");
XmlNode sigNode = xmlDoc.SelectSingleNode("//ns:UpdateInboundim613Response//ns:model//ns:instance//ns:OVSCTicketID",namespaceManager);
if (sigNode != null)
{
    Console.WriteLine(sigNode.InnerText);
}

上述代码应该可以正常工作。

您的Xml文档使用默认名称空间“”。您需要使用XmlNamespaceManager在此命名空间下选择该节点

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"C:\zzx\Project\SM\R5.1\Harness\InBound.xml");
var namespaceManager = new XmlNamespaceManager(xmlDoc.NameTable);
namespaceManager.AddNamespace("ns", "http://schemas.hp.com/SM/7");
XmlNode sigNode = xmlDoc.SelectSingleNode("//ns:UpdateInboundim613Response//ns:model//ns:instance//ns:OVSCTicketID",namespaceManager);
if (sigNode != null)
{
    Console.WriteLine(sigNode.InnerText);
}
以上代码应该可以正常工作。

使用xml linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XNamespace defaultNs = ((XElement)doc.FirstNode).GetDefaultNamespace();
            string ovsCTicketID = (string)doc.Descendants(defaultNs + "OVSCTicketID").FirstOrDefault();
        }
    }
}
使用xml-linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XNamespace defaultNs = ((XElement)doc.FirstNode).GetDefaultNamespace();
            string ovsCTicketID = (string)doc.Descendants(defaultNs + "OVSCTicketID").FirstOrDefault();
        }
    }
}

sigNode总是空的,我不知道为什么sigNode总是空的,我不知道为什么,如何识别默认名称空间,或者如何知道默认名称空间是“”?实际上,在xml中识别默认名称空间非常容易,只需在根元素中搜索xmlns属性。因此,在根元素中,我可以看到这个定义:xmlns:xsi=“”,这是一个名称空间定义吗?我可以看到这样的元素:,我不知道xsi:nil的意思是什么,你能解释上面的两个问题吗?感谢如果任何节点以xsi:nil或ns:NodeName这样的名称开头,那么这意味着节点位于名称空间之下(这里xsi或ns只是名称空间的别名)。我建议您阅读这些文章,以便更好地理解-因此,如何识别默认名称空间,或者如何知道默认名称空间是“”?实际上,在xml中识别默认名称空间非常容易,只需在根元素中搜索xmlns属性即可。因此,在根元素中,我可以看到以下定义:xmlns:xsi=“”,这是名称空间定义吗?我可以看到这样的元素:,我不知道xsi:nil的意思是什么,你能解释上面的两个问题吗?感谢如果任何节点都以xsi:nil或ns:NodeName开头,那么这意味着节点位于名称空间之下(这里xsi或ns只是名称空间的别名)。为了更好地理解,我建议您阅读这些文章