ARTICLE

Reading XML File using XmlDocument

Posted by Bulent Ozkir Articles | XML April 26, 2001
In this article Reading XML File using XmlDocument.
Reader Level:

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
post comment
     

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

Posted by Scott Arnold May 18, 2009
Posted by poster commneter Oct 23, 2008

Works great! Thanks a million.

Posted by Mohamed Kamal Feb 14, 2008
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter