?
Solved

Xml Selectnodes not working

Posted on 2009-04-16
7
Medium Priority
?
418 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:nickmarshall
  • 4
  • 2
7 Comments
 

Expert Comment

by:ToL
ID: 24157634
At first sight, there is a mistake at lines 27 and 30

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

only one slash should be used "/Transaction/Transactions"
0
 

Expert Comment

by:ToL
ID: 24157657
sorry - another mistake: "/Transactions/Transaction" should be correct
0
 
LVL 1

Author Comment

by:nickmarshall
ID: 24157749
Hi,

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

Any ideas?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:nickmarshall
ID: 24157856
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.
0
 

Expert Comment

by:ToL
ID: 24158135
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"
0
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 1500 total points
ID: 24158151
your select node will not work without using the xml namespace to resolve the node name
have a look at the following experts exchange link
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_23017732.html?cid=236#a20462218
0
 

Expert Comment

by:ToL
ID: 24158179
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
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Screencast - Getting to Know the Pipeline
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question