Link to home
Start Free TrialLog in
Avatar of nickmarshall
nickmarshall

asked on

Xml Selectnodes not working

Experts,

I'm attempting to pull out data from various XML files within a directory (Code below), however I don't think the selectNodes is working.  Here's the document structure:

<?xml version="1.0"?>
<Report xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Transactions xmlns="http://www.payment.com">
    <Transaction>
      <UMerchantID>00000</UMerchantID>
      <TransactionDateTime>2009-03-05T10:01:09</TransactionDateTime>
      <TransactionType>SALE</TransactionType>
      <CardType>VD</CardType>
      <Amount>433432</Amount>
      <CurrencyCode>4343</CurrencyCode>
      <CountryCode>232</CountryCode>
      <ResponseCode>00</ResponseCode>
      <CrossReference>0990090909090909090</CrossReference>
      <Dispatch>NOW</Dispatch>
      <AuthorisationOnly>false</AuthorisationOnly>
      <Reversed>false</Reversed>
      <Name>Anthony SMith</Name>
      <Address>Business address</Address>
      <Telephone>01234 123123</Telephone>
      <EmailAddress>email1@emaio.com</EmailAddress>
      <Source>NET</Source>
    </Transaction>
    <Transaction>
      <UMerchantID>000000</UMerchantID>
      <TransactionDateTime>2009-03-05T10:01:22</TransactionDateTime>
      <TransactionType>SALE</TransactionType>
      <CardType>MC</CardType>
      <Amount>3232</Amount>
      <CurrencyCode>121</CurrencyCode>
      <CountryCode>812</CountryCode>
      <ResponseCode>00</ResponseCode>
      <CrossReference>0999009879723</CrossReference>
      <Dispatch>NOW</Dispatch>
      <AuthorisationOnly>false</AuthorisationOnly>
      <Reversed>false</Reversed>
      <Name>John Smith</Name>
      <Address>Business Addressl</Address>
      <Telephone>01234 123123</Telephone>
      <EmailAddress>email2@email.com</EmailAddress>
      <Source>NET</Source>
    </Transaction>
   </Transactions>
</Report>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Threading;
 
namespace xmlDataExtractor
{
    class Program
    {
        static void Main(string[] args)
        {
 
            // Get all xml files
            string[] files = Directory.GetFiles(@"C:\xml");
            
            // Iterate through each file in directory
            foreach (string file in files)
            {
                //XmlTextReader reader = new XmlTextReader(file);
                XmlDocument doc = new XmlDocument();
                doc.Load(file);
 
 
                Console.WriteLine("COUNT:" + doc.DocumentElement.SelectNodes("//Transaction/Transactions").Count.ToString());
 
                // Loop through all the elements
                foreach (XmlNode Transaction in doc.DocumentElement.SelectNodes("//Transaction/Transactions"))
                {
                    Console.WriteLine("TRANSACTION:" + Transaction.InnerText);
 
                    XmlNode tempNode;
 
                    tempNode = Transaction.SelectSingleNode("Name");
                    string Name = tempNode.InnerText.ToString();
                        
                    tempNode = Transaction.SelectSingleNode("Address");
                    string Address = tempNode.InnerText.ToString();
                       
                    tempNode = Transaction.SelectSingleNode("EmailAddress");
                    string EmailAddress = tempNode.InnerText.ToString();
 
                    Console.WriteLine("Name:" + Name + ",Address:" + Address + ",EmailAddress" + EmailAddress);
                }
                Thread.Sleep(1000);
                Console.WriteLine("Moving to next file");
            }
        }
    }
}

Open in new window

Avatar of ToL
ToL
Flag of Czechia image

At first sight, there is a mistake at lines 27 and 30

DocumentElement.SelectNodes("//Transaction/Transactions")

only one slash should be used "/Transaction/Transactions"
sorry - another mistake: "/Transactions/Transaction" should be correct
Avatar of nickmarshall
nickmarshall

ASKER

Hi,

Yes, I've tried that already.  Had a go with a few different combinations.  Still no luck though I'm affraid.

Any ideas?
Hi,

I've written, doc.InnerText to file which outputs the entire xml document's inner text to file, so I gather that the XML file is being loaded into memory correctly.

Just though this might help.
when I use xpath /Report/Transactions/Transaction then it works, but only when the <Transactions> element is used without the parameter xmlns="http://www.payment.com"
ASKER CERTIFIED SOLUTION
Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
xmlns parameter will probably be the problem, check for example this: http://www.devx.com/DevX/Tip/21337

If you need more help, let me know