Hi David,
Tim provided some suggestion about using XmlTextReader, that is quite good
for memory restricted condition since it avoid loading the envire XML
document into memory. If memory is not the problem, and you have ample
memory to hold the entire XMLdocument, you can consider load the file into
XmlDocument and than perform XPath query to get all the expected
elements/nodes. Here are some good articles introducing execute Xpath query
via .NET xml document or xpath navigator classes:
#.NET and XML: XPath Queries
http://www.developer.com/xml/article.php/3383961
#Easily navigate XML with VB.NET and XPath
http://articles.techrepublic.com.com...1-5035074.html
#INFO: Roadmap for Executing XPath Queries in .NET Applications
http://support.microsoft.com/kb/313828
for your sample xml file here, I've written a simple console app to
demonstrate how to query all the batch elements(and its inner tktamount and
all the items ...)
>>>>>>>>>>>>>>>>>>>>>>>>>
class Program
{
static void Main(string[] args)
{
Run_DOM();
}
static void Run_DOM()
{
XmlDocument doc = new XmlDocument();
doc.Load(@"..\..\data.xml");
//query all batches...
XmlNodeList batches = doc.SelectNodes("//Batches/Batch");
if(batches!= null)
foreach (XmlNode batch in batches)
{
//for each batch, get Nbr and TktAmount
XmlNode Nbr = batch.SelectSingleNode("./BatchNbr");
XmlNode Amount =
batch.SelectSingleNode("./BatchTktAmount");
Console.WriteLine("######BatchNbr:{0},
BatchTktAmount:{1}#######",
Nbr.InnerText, Amount.InnerText);
//query items of the batch
Console.WriteLine("\tItems:");
XmlNodeList items = batch.SelectNodes("./Items/Item");
foreach (XmlNode item in items)
{
XmlNode isn = item.SelectSingleNode("./ISN");
XmlNode amt = item.SelectSingleNode("./Amount");
Console.WriteLine("\t\tISN:{0}, Amount:{1}"
, isn.InnerText, amt.InnerText);
}
}
}
}
<<<<<<<<<<<<<<<<<<<<<<<<<<
Also, I saw that in Tim's new reply, he mentioned the X-LINQ
feature(provided in .NET 3.5). That is a new XML DOM based processing API
which can do the same thing as the above DOM code(but with different api
and syntax), if you have interests, you can also have a look at LINQ/LINQ
TO XML over MSDN:
#NET Language-Integrated Query for XML Data
http://msdn.microsoft.com/en-us/library/bb308960.aspx
Hope this also helps.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>From: =?Utf-8?B?RGF2aWRN?= <Da****@discussions.microsoft.com>
References: <6B**********************************@microsoft.co m>
<GZ******************************@eclipse.net.uk >
>Subject: Re: How to Parse XML File
Date: Mon, 16 Jun 2008 12:19:02 -0700
>
John -- thanks for your reply. I'm reading it now and like the DDJ
article.
>
"John Timney (MVP)" wrote:
>If you can convert it into a dataset, then you can easily bind to it and
do
>usual DB type stuff with its values
http://devpinoy.org/blogs/keithrull/...onvert-an-xml-
string-to-dataset.aspx
>>
http://www.ddj.com/windows/184416669
Regards
John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
"DavidM" <Da****@discussions.microsoft.comwrote in message
news:6B**********************************@microso ft.com...
Hello, I have an XML file generated from a third party application
that I
would like to parse. Ideally, I plan on having a windows service
setup to
scan various folders for XML files and parse the file, then spit out
totals.
Since I haven't worked with XML too much in C#, I'm trying to develop a
structured and easy-to-read way to parse the file.
Essentially, I would like to read the file and add the
"BatchTktAmount>
for
any <Batchelements that may exist. There could be 1 or more...
As a verification, I may also want to add the <BATCH\Itemselements,
there
could be 1 or more, and if <Genderis a credit "CR", accumulate
totals
for
credit items. If <Genderis a debit "DR", acculate totals for debit
items.
Total items should equal the total batches.
I guess if I had relatively good XML parsing code, I could figure out
how