2016-11-08 26 views
0

我的XML文件,如下圖所示:不能使用selectSingleNode方法得到節點

<?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> 

,但我的代碼不能得到元素「OVSCTicketID」的價值:

 XmlDocument xmlDoc = new XmlDocument(); 
     xmlDoc.Load(@"C:\zzx\Project\SM\R5.1\Harness\InBound.xml"); 
     XmlNode sigNode = xmlDoc.SelectSingleNode("/UpdateInboundim613Response/model/instance/OVSCTicketID"); 
     if (sigNode != null) 
     { 
      Console.WriteLine(sigNode.InnerText); 
     } 

可以請你告訴我什麼是問題和如何解決它?

+0

的西格諾德總是空的,我不知道爲什麼這麼 – user2575502

回答

3

您的Xml文檔使用默認名稱空間「http://schemas.hp.com/SM/7」。您需要使用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); 
} 

上面的代碼應該可以正常工作。

+0

,如何識別默認的命名空間,或者你怎麼知道的默認命名空間爲「http://模式.hp.com/SM/7「? – user2575502

+0

實際上,只需在根元素中搜索xmlns屬性,就很容易在xml中標識默認名稱空間。 – Ajit

+0

所以,在根元素中,我可以看到這個定義:xmlns:xsi =「http://www.w3.org/2001/XMLSchema-instance」,這是一個名稱空間的定義嗎?我可以看到這樣的一些元素:,我不知道xsi:nil的含義是什麼,你能解釋2個以上的問題嗎?謝謝 – user2575502

1

使用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(); 
     } 
    } 
} 
相關問題