频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

Reading XML Files using XmlDocument(转)

来源: 作者: 出处:巧巧读书 2006-10-01 进入讨论组
  • 关 键 词:
  • nat

  Bulent Ozkir

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.csWeb: http://www.qqread.com/xml-soap/e227961.html 更多文章 更多内容请看XML详解专题,或进入讨论组讨论。
收藏此文】【 】【打印】【关闭
较早的文章:Native XML数据库技术详解

较新的文章:server.xml配置简介
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章