2

I am currently having a problem with reading a XML file using XPath expression. I have used the XmlDocument class. When I try reading a particular node from the XML, I get an empty list. The node which I am trying to read is the ID below ProductionRequest.

Here is the XML file which I tried to read:

<?xml version="1.0" encoding="iso-8859-1"?>
<ProductionSchedule xmlns="http://www.wbf.org/xml/b2mml-v02"> 
  <ID>00000020000000</ID>
  <Location>
   <EquipmentID>8283</EquipmentID>
   <EquipmentElementLevel>Site</EquipmentElementLevel>
  <Location>
   <EquipmentID>0</EquipmentID>
   <EquipmentElementLevel>Area</EquipmentElementLevel>
   </Location>
  </Location>
 <ProductionRequest>
    <ID>0009300000000</ID>
    <ProductProductionRuleID>W001</ProductProductionRuleID>
    <StartTime>2017-04-20T23:57:20</StartTime>
    <EndTime>2017-04-20T24:00:00</EndTime>
    </ProductionRequest>
  </ProductionSchedule>

This is the code which I used to read the above XML

using System;
using System.Xml.Linq;
using System.Xml;
using System.Xml.XPath;

namespace XML
{
  class Program
  {
    static void Main(string[] args)
    {
     Console.WriteLine("Hello World!");
     string fullName = "F:\\Programming\\XML\\Example XML.xml";
     XmlDocument xreader = new XmlDocument();

     xreader.Load(fullName);
     XmlNode root = xreader.DocumentElement;
     XmlNodeList xnList1 = 
            xreader.SelectNodes("/ProductionSchedule/ProductionRequest/ID");


    }
  }
 }

I could not find the cause for this problem. Could anyone help me in this regard. Looking forward for valuable inputs.

qxotk
  • 2,384
  • 5
  • 24
  • 39
Tom92
  • 31
  • 1
  • 3
  • Possible duplicate of [How does XPath deal with XML namespaces?](https://stackoverflow.com/questions/40796231/how-does-xpath-deal-with-xml-namespaces) – Klaus Gütter Dec 28 '18 at 11:31
  • The mesa organization has a schema at https://services.mesa.org/ResourceLibrary/ShowResource/4cf7f45a-7b4f-4e3a-b600-43fe7b691f49. I would recommend using serialization to parse the xml. There are multiple schema that are very complex a would give better results if you need to parse entire xml. – jdweng Dec 28 '18 at 11:44

1 Answers1

2

Your xml contains namespace http://www.wbf.org/xml/b2mml-v02 at root level node <ProductionSchedule>

And you are using the XPath expression /ProductionSchedule/ProductionRequest/ID but this XPath expression is not suitable for this xml document and that's why you can't get any desired value.

You need to use the below XPath expression to get the id's of all <ProductionRequest> node.

XmlNodeList xnList1 = xreader.SelectNodes("//*[name()='ProductionSchedule']/*[name()='ProductionRequest']/*[name()='ID']");

OR you can add namespace manually like

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xreader.NameTable);
nsmgr.AddNamespace("x", "http://www.wbf.org/xml/b2mml-v02");
XmlNodeList xnList1 = xreader.SelectNodes("//x:ProductionSchedule/x:ProductionRequest/x:ID", nsmgr);

And finally, you can read id from any of the parent nodes in variable xnList1 like

foreach (XmlNode id in xnList1)
{
    Console.WriteLine(id.InnerText);
}

Output:

enter image description here

qxotk
  • 2,384
  • 5
  • 24
  • 39
er-sho
  • 9,581
  • 2
  • 13
  • 26