Blue Theme Orange Theme Green Theme Red Theme
 
HeaderAd
Home | Forums | Videos | Photos | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
New MS SQL 2008 Available - DiscountASP.NET
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » XML .NET » Reading XML File using XmlDocument

Reading XML File using XmlDocument

In this article Reading XML File using XmlDocument.

Author Rank:
Technologies: .NET 1.0/1.1,Visual C# .NET
Total downloads : 376
Total page views :  82626
Rating :
 5/5
This article has been rated :  1 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
ReadingXMLFilesUsingXMLDocument.zip
 
ArticleAd
Become a Sponsor



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


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.
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
ReadingXMLFilesUsingXMLDocument.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
ArticleAd
Become a Sponsor
Latest Comments:
Subject Posted By Posted On
ThanksMohamed2/14/2008
Works great! Thanks a million.
Reply | Email | Delete | Modify | 
Here is a more simple way of reading and writing xml file using array list poster10/23/2008
i came across this while googling
http://probedeep.blogspot.com/2008/10/program-to-read-and-write-xml-files-in.html
Reply | Email | Delete | Modify | 
Additional HelpScott5/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 | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved