Re: How to Parse XML File

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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/5100-10878_11-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:
msdnmg@xxxxxxxxxxxxxx

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.







--------------------
From: =?Utf-8?B?RGF2aWRN?= <DavidM@xxxxxxxxxxxxxxxxxxxxxxxxx>
References: <6BFCD91B-C8ED-417F-AB64-6911DD0A9EE9@xxxxxxxxxxxxx>
<GZ6dnYbKNqAVMMvVnZ2dnUVZ8vednZ2d@xxxxxxxxxxxxxx>
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/archive/2008/04/17/howto-convert-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" <DavidM@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:6BFCD91B-C8ED-417F-AB64-6911DD0A9EE9@xxxxxxxxxxxxxxxx
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 <Batch> elements that may exist. There could be 1 or more...

As a verification, I may also want to add the <BATCH\Items> elements,
there
could be 1 or more, and if <Gender> is a credit "CR", accumulate
totals
for
credit items. If <Gender> is 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

.



Relevant Pages

  • Re: Need help with PHP DOMXML - get_elements_by_tagname
    ... PHP as a supported platform. ... uses the built in XML handler CF MX provides. ... in newer versions of PHP and the method will *not* support xpath. ...
    (comp.lang.php)
  • Re: Garbage Collection Problems: Performance and Optimization for Web Service XmlDocument XPath quer
    ... XML, by its very nature, is slow. ... .Net memory model is severely flawed. ... >>call to the XmlDocument, and in the routine, before and after each XPath ...
    (microsoft.public.dotnet.general)
  • Re: How to Parse XML File
    ... How to Parse XML File ... for memory restricted condition since it avoid loading the envire XML ... Here are some good articles introducing execute Xpath ... demonstrate how to query all the batch elements(and its inner tktamount ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: TreeView Dynamic XML Binding
    ... had a quick look at the XmlDataSource through reflector. ... Microsoft MSDN Online Support Lead ... Subject: TreeView Dynamic XML Binding ... Expand the ReturnHeader node. ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: SQL2005, validation, & XQuery
    ... I think you are experiencing occurence issue, not namespace. ... your xpath syntax is correct. ... The xml in my sample columns only *had* one node: ... Microsoft Online Community Support ...
    (microsoft.public.sqlserver.xml)