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</UMerch antID>
<TransactionDateTime>2009- 03-05T10:0 1:09</Tran sactionDat eTime>
<TransactionType>SALE</Tra nsactionTy pe>
<CardType>VD</CardType>
<Amount>433432</Amount>
<CurrencyCode>4343</Curren cyCode>
<CountryCode>232</CountryC ode>
<ResponseCode>00</Response Code>
<CrossReference>0990090909 090909090< /CrossRefe rence>
<Dispatch>NOW</Dispatch>
<AuthorisationOnly>false</ Authorisat ionOnly>
<Reversed>false</Reversed>
<Name>Anthony SMith</Name>
<Address>Business address</Address>
<Telephone>01234 123123</Telephone>
<EmailAddress>email1@emaio .com</Emai lAddress>
<Source>NET</Source>
</Transaction>
<Transaction>
<UMerchantID>000000</UMerc hantID>
<TransactionDateTime>2009- 03-05T10:0 1:22</Tran sactionDat eTime>
<TransactionType>SALE</Tra nsactionTy pe>
<CardType>MC</CardType>
<Amount>3232</Amount>
<CurrencyCode>121</Currenc yCode>
<CountryCode>812</CountryC ode>
<ResponseCode>00</Response Code>
<CrossReference>0999009879 723</Cross Reference>
<Dispatch>NOW</Dispatch>
<AuthorisationOnly>false</ Authorisat ionOnly>
<Reversed>false</Reversed>
<Name>John Smith</Name>
<Address>Business Addressl</Address>
<Telephone>01234 123123</Telephone>
<EmailAddress>email2@email .com</Emai lAddress>
<Source>NET</Source>
</Transaction>
</Transactions>
</Report>
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</UMerch
<TransactionDateTime>2009-
<TransactionType>SALE</Tra
<CardType>VD</CardType>
<Amount>433432</Amount>
<CurrencyCode>4343</Curren
<CountryCode>232</CountryC
<ResponseCode>00</Response
<CrossReference>0990090909
<Dispatch>NOW</Dispatch>
<AuthorisationOnly>false</
<Reversed>false</Reversed>
<Name>Anthony SMith</Name>
<Address>Business address</Address>
<Telephone>01234 123123</Telephone>
<EmailAddress>email1@emaio
<Source>NET</Source>
</Transaction>
<Transaction>
<UMerchantID>000000</UMerc
<TransactionDateTime>2009-
<TransactionType>SALE</Tra
<CardType>MC</CardType>
<Amount>3232</Amount>
<CurrencyCode>121</Currenc
<CountryCode>812</CountryC
<ResponseCode>00</Response
<CrossReference>0999009879
<Dispatch>NOW</Dispatch>
<AuthorisationOnly>false</
<Reversed>false</Reversed>
<Name>John Smith</Name>
<Address>Business Addressl</Address>
<Telephone>01234 123123</Telephone>
<EmailAddress>email2@email
<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");
}
}
}
}
sorry - another mistake: "/Transactions/Transaction" should be correct
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?
Yes, I've tried that already. Had a go with a few different combinations. Still no luck though I'm affraid.
Any ideas?
ASKER
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.
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/Trans action then it works, but only when the <Transactions> element is used without the parameter xmlns="http://www.payment.com"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
If you need more help, let me know
DocumentElement.SelectNode
only one slash should be used "/Transaction/Transactions