XML参考 :XmlReader 详解、实例(3)-- 读取XML节点和属性名称

.NET Framework 类库

XmlReader 类

 

表示提供对 XML 数据进行快速、非缓存、只进访问的读取器,即 对 XML 数据流的只进只读访问。XmlReader 类符合 W3C 可扩展标记语言 (XML) 1.0 和“XML 中的命名空间”建议。

 

三.读取XML节点和属性名称

 

class  XmlReaderDemo
ExpandedBlockStart.gifContractedBlock.gif    
{
        
private static string xmlFilePath = @"..\..\EmployeeInfo.xml";// @"D:\\EmployeeInfo.xml"

        
public static string ReadXml()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
string result = "";
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
using (XmlReader reader = XmlReader.Create(xmlFilePath))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
while (reader.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
if (reader.NodeType == XmlNodeType.Element)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            
for (int count = 0; count < reader.Depth; count++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
{
                                result 
+= "";
                            }

                            result 
+= "->" + reader.Name;
                            
if (reader.HasAttributes)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
{
                                result 
+= "(";
                                
for (int count = 0; count < reader.AttributeCount; count++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
{
                                    reader.MoveToAttribute(count);
                                    result 
+= reader.Name + ",";
                                }

                                result 
+= ")";
                            }

                            result 
+= "\n";
                        }

                    }

                }

            }

            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                result 
+= "An Exception occured: " + ex.Message;
            }

            
return result;
        }

    }


    
class  Program
ExpandedBlockStart.gifContractedBlock.gif    
{
        
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
string strReturn = XmlReaderDemo.ReadXml();
            Console.WriteLine(strReturn);
            Console.ReadLine();
        }

    }

 

效果图如下所示:

转载于:https://www.cnblogs.com/Dlonghow/archive/2008/07/28/1254290.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值