阿宽

Nothing is more powerful than habit!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C# 讀取XML文檔

Posted on 2008-01-25 11:17  宽田  阅读(679)  评论(1编辑  收藏  举报

        /// <summary>
        
/// 讀取XML
        
/// </summary>
        
/// <param name="sFilePath">XML路徑包含文件名</param>
        
/// <param name="sNode">結點名稱</param>
        
/// <returns></returns>
        private List<string> readXml(string sFilePath, string sNode)
        {
            List
<string> lNodeList = new List<string>();
            
try
            {
                
if (File.Exists(sFilePath))
                {
                    XPathDocument xpdDocument 
= new XPathDocument(sFilePath);
                    XPathNavigator xpnNavigator 
= xpdDocument.CreateNavigator();
                    XPathNodeIterator xpiIterator 
= xpnNavigator.Select(sNode);
                    
while (xpiIterator.MoveNext())
                    {
                        lNodeList.Add(xpiIterator.Current.Value);
                    }
                }
            }
            
catch (Exception e) 
            {                
                
throw e;
            }
            
return lNodeList;
        }

參數sNode的值有兩種:
    一種以"//"表示結點
    一種以"@"開頭屬性