1

my xml file is like below:

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

but my code cannot get value of element "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);
        }

can you please tell me what's the problem and how to fix it?

1
  • the sigNode is always null, I don't know why Commented Nov 8, 2016 at 5:43

2 Answers 2

3

Your Xml document uses the default namespace "http://schemas.hp.com/SM/7". You need to use the XmlNamespaceManager to select that node under this namespace.

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

Above code should work fine.

Sign up to request clarification or add additional context in comments.

5 Comments

so, how to identify the default namespace, or how do you know the default namespace is "schemas.hp.com/SM/7" ?
Actually it is very easy to identify default namespace in xml just search for xmlns attribute in root element.
so, in the root element, I can see this definition: xmlns:xsi="w3.org/2001/XMLSchema-instance", is this a namespace definition? and I can see some element like this : <RIMSImpact xsi:nil="true" />, I don't know what's the meaning of xsi:nil, can you explain above 2 questions? thanks
If any node start with something like xsi:nil or ns:NodeName then meaning of this is that node comes under the namespace(here xsi or ns is just the alias for the namespace).
I would suggest you to go though these articles for better understanding- en.wikipedia.org/wiki/XML_namespace and msdn.microsoft.com/en-us/library/aa468565.aspx
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.