簡體   English   中英

使用selectSingleNode方法無法獲取節點

[英]cannot get node by using selectSingleNode method

我的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>

但是我的代碼無法獲取元素“ 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);
        }

您能告訴我什么問題以及如何解決嗎?

您的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);
}

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

使用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();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM