Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » XML in C# » Reading XML File using XmlDocument

Reading XML File using XmlDocument

In this article Reading XML File using XmlDocument.

Author Rank :
Page Views : 209120
Downloads : 910
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
ReadingXMLFilesUsingXMLDocument.zip
 
 
Team Foundation Server Hosting
Become a Sponsor
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Suppose I have following XML fragment:

<Authors>
<Author>
<
FirstName>John</FirstName>
<LastName>Doe</LastName>
</Author>
<Author>
<FirstName>Jane</FirstName>
<LastName>Eod</LastName>
</Author>
</Authors>

Now, how can I loop through my collection of authors and for each author  retrieve its first and last name and put them in a variable strFirst and  strLast?

 - - - XMLApp.cs

using System;
using System.Xml;
public class XMLApp
{
public void YourMethod( String strFirst, String strLast)
{
// Do something with strFirst and strLast.
// ...
Console.WriteLine( "{0}, {1}", strLast, strFirst);
}
public void ProcessXML( String xmlText)
{
XmlDocument _doc =
new XmlDocument( );
_doc.LoadXml( xmlText);
// alternately, _doc.Load( _strFilename); to read from a file.
XmlNodeList _fnames = _doc.GetElementsByTagName( "FirstName" );
XmlNodeList _lnames = _doc.GetElementsByTagName( "LastName" );
// I'm assuming every FirstName has a LastName in this example, your requirements may vary. //
for ( int _i = 0; _i < _fnames.Count; ++_i )
{
YourMethod( _fnames[ _i].InnerText,
_lnames[ _i].InnerText );
}
public static void Main( String[] args)
{
XMLApp _app =
new XMLApp( );
// Passing XML text as a String, you can also use the
// XMLDocument::Load( ) method to read the XML from a file.
//
_app.ProcessXML( @" <Authors>
<
Author>
<
FirstName>John</FirstName>
<
LastName>Doe</LastName>
</
Author>
<
Author>
<
FirstName>Jane</FirstName>
<
LastName>Eod</LastName>
</
Author>
</
Authors> " );
}
}
// end XMLApp

 
- - - XMLApp.cs

 Remember to /reference the System.Xml.dll on the command-line  to build XMLApp.cs:
 csc.exe /r:System.Xml.dll XMLApp.cs

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Bulent Ozkir
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
Thanks by Mohamed On February 14, 2008
Works great! Thanks a million.
Reply | Email | Modify 
Here is a more simple way of reading and writing xml file using array list by poster On October 23, 2008
Reply | Email | Modify 
Additional Help by Scott On May 18, 2009
Hi,
Thanks this articles its good stuff.  Here is my question.  I'm new at c# and I have a web service that is being called from Oracle's BPEL process.  Now they are passing me purchase order header and line information.  I need to read and parse put the values of each element.  Now I could have multiple lines for each header.  Here is what I need to do is read the header information and string each value and then call another web service which, is already exists and check the status and if it fails then return back to Oracle's BPEL process with the error.  If no error then I need each line and perform the same process.  Here is an example of the XML:

<InitiateWalkerFundCheckApprovalInputVariable><part xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="payload"><PerformWalkerFundCheckApprovalRequest xmlns:ns1="http://xmlns.oracle.com/PerformWalkerFundCheckApprovalData" xmlns="http://xmlns.oracle.com/PerformWalkerFundCheckApprovalData">
   <ns1:ack>032306710005505</ns1:ack>
   <ns1:transactionid>KM4</ns1:transactionid>
   <ns1:action>A</ns1:action>
   <ns1:vendor>00000000100</ns1:vendor>
   <ns1:pok>7300000009</ns1:pok>
   <ns1:projectnbr>0000000015</ns1:projectnbr>
   <ns1:eck>01</ns1:eck>
   <ns1:orderdt>20090422</ns1:orderdt>
   <ns1:orderclass>R</ns1:orderclass>
   <ns1:markfor>142</ns1:markfor>
   <ns1:shipto>142</ns1:shipto>
   <ns1:termscode>98</ns1:termscode>
   <ns1:requisitionnbr/>
   <ns1:currency>USD</ns1:currency>
   <ns1:discountdays>14</ns1:discountdays>
   <ns1:discountpercent>0.15</ns1:discountpercent>
   <ns1:weight/>
   <ns1:cube/>
   <ns1:revision>0</ns1:revision>
   <ns1:aafesPoLinesIfStgCollection>
      <ns1:AafesPoLinesIfStg>
         <ns1:transactionid>KNR</ns1:transactionid>
         <ns1:action>A</ns1:action>
         <ns1:vendor>00000000100</ns1:vendor>
         <ns1:pok>7300000009</ns1:pok>
         <ns1:linenbr>0010</ns1:linenbr>
         <ns1:linecd>STD</ns1:linecd>
         <ns1:ack>032306710005505</ns1:ack>
         <ns1:lineamt>17600</ns1:lineamt>
         <ns1:qtyord>88</ns1:qtyord>
         <ns1:unitprc>200</ns1:unitprc>
         <ns1:uom>EA</ns1:uom>
         <ns1:promisedt>20090422</ns1:promisedt>
         <ns1:mck>test</ns1:mck>
         <ns1:requisitionnbr/>
         <ns1:requisitionline>0010</ns1:requisitionline>
         <ns1:partnbr/>
         <ns1:description>WIDGET, BLUE</ns1:description>
      </ns1:AafesPoLinesIfStg>
   </ns1:aafesPoLinesIfStgCollection>
</PerformWalkerFundCheckApprovalRequest>
</part></InitiateWalkerFundCheckApprovalInputVariable>

Thanks,
Scott
Reply | Email | Modify 
Discover the top 5 tips for understanding .NET Interop
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.