Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract nodes from XmlNodeList, with namespace, and where same child appears at multiple levels

Tags:

c#

xml

I am trying to extract those subnodes, but I had got just headache so far...

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
  <supplyCrew xmlns="http://site.ddf.com">
     <login>
        <login>XXXX</login>
        <password>XXXX</password>
     </login>
     <flightInformation>
        <flights>
           <item>
              <arrivalDateTime>2010-11-08T22:48:00.000Z</arrivalDateTime>
              <arrivingCity>ORD</arrivingCity>
              <crewMembers>
                 <item>
                    <employeeId>020040</employeeId>
                    <isDepositor>Y</isDepositor>
                    <isTransmitter>N</isTransmitter>
                 </item>
                 <item>
                    <employeeId>09000</employeeId>
                    <isDepositor>N</isDepositor>
                    <isTransmitter>Y</isTransmitter>
                 </item>
              </crewMembers>
           </item>
           <item>
              <arrivalDateTime>2010-11-08T20:29:00.000Z</arrivalDateTime>
              <arrivingCity>JFK</arrivingCity>
              <crewMembers>
                 <item>
                    <employeeId>0538</employeeId>
                    <isDepositor>Y</isDepositor>
                    <isTransmitter>N</isTransmitter>
                 </item>
                 <item>
                    <employeeId>097790</employeeId>
                    <isDepositor>N</isDepositor>
                    <isTransmitter>Y</isTransmitter>
                 </item>

with the code I can get them, but I do not know how to select each one according to their tag name to insert them into a database.

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("C:/Crew_Request_Sample.xml"); 
XmlNodeList elemList = xmlDoc.GetElementsByTagName("item");
foreach (XmlNode node in elemList)
{
    Debug.WriteLine(node.InnerText);
}

I need some direction, please.

like image 966
Devester Avatar asked Oct 04 '12 13:10

Devester


3 Answers

The problem with using GetElementsByTagName("item") here is that there are 2 levels of item node - one as a child of flights and another item as a child of crewMembers.

Edit Now that the full xml is pasted, it is clear that there is also a namespace involved as well. To handle namespaces, make use of a namespace manager to define aliases for the namespaces, which you can then use in the xpath queries:

var nsm = new XmlNamespaceManager(xmlDoc.NameTable);
nsm.AddNamespace("s", "http://site.ddf.com");
var elemList = xmlDoc.SelectNodes("//s:crewMembers/s:item", nsm);
foreach (var node in elemList)
{
    Debug.WriteLine(node.SelectSingleNode("s:employeeId", nsm).InnerText);
    Debug.WriteLine(node.SelectSingleNode("s:isDepositor", nsm).InnerText);
    Debug.WriteLine(node.SelectSingleNode("s:isTransmitter", nsm).InnerText);
}
like image 199
StuartLC Avatar answered Nov 13 '22 18:11

StuartLC


You can do it using LINQ2XML..

XElement doc=XElement.Load("C:/Crew_Request_Sample.xml"); 
XNamespace e = "http://schemas.xmlsoap.org/soap/envelope/";
XNamespace s = "http://site.ddf.com";
//this would access the nodes of item->crewMembers->item and put it into an Anonymous Type

var yourList=doc.Descendants(e+"Body")
.Descendants(s+"supplyCrew")
.Descendants(s+"flightInformation")
.Descendants(s+"flights")
.Descendants(s+"item")
.Descendants(s+"crewMembers")
.Descendants(s+"item")
.Select(
x=>new
{
//Anonymous Type
employeeId=x.Element(s+"employeeId").Value,
isDepositor=x.Element(s+"isDepositor").Value,
isTransmitter=x.Element(s+"isTransmitter").Value
}
);

You can then access yourList using for-each loop

foreach(var item in yourList)
{

Console.WriteLine(item.employeeId);
Console.WriteLine(item.isDepositor);
Console.WriteLine(item.isTransmitter);
}
like image 26
Anirudha Avatar answered Nov 13 '22 16:11

Anirudha


I think you'll do it faster and easily using this technique

Linq To XML

There is a lot of examples in the site, so it 'll be easy to find what you want. Hope it helps.

like image 31
Vinicius Avatar answered Nov 13 '22 16:11

Vinicius