繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> Asp.Net开发 >> Reading XML Files using XmlDocument(转)

Reading XML Files using XmlDocument(转)

2007-07-15 08:00:00  作者:  来源:互联网  浏览次数:0  文字大小:【】【】【
简介:Bulent Ozkir Suppose I have following XML fragment: John Doe Jane Eod Now, how can I loop through my collection of authors and for each author retrieve its first and last name and put them ...

Bulent Ozkir

Suppose I have following XML fragment:

John

Doe

Jane

Eod

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( @"

John

Doe

Jane

Eod

" );

}

} // 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

责任编辑:admin
相关文章