zoukankan      html  css  js  c++  java
  • XmlReader在未知元素的名称和属性的名称的情况下读取属性

    经过昨天到今天的努力以及博问上好心人的帮助,终于解决了XmlReader在未知元素的名称和属性的名称的情况下读取属性的方法。

    在没有解决前,我的代码如下:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Xml;
     6 
     7 namespace ReadAttribute
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             string path = @"e:	estfilemyMail.xml";
    14             
    15             #region 读取属性的第一种方法
    16             //string date;
    17             //try
    18             //{
    19             //    XmlReader xr = XmlReader.Create(path);
    20             //    xr.ReadToFollowing("mail");
    21             //    date = xr.GetAttribute("date");
    22             //    Console.Write("信件的日期为:");
    23             //    Console.WriteLine(date);
    24             //}
    25             //catch (Exception ex)
    26             //{
    27             //    Console.WriteLine(ex.Message);
    28             //} 
    29             #endregion
    30 
    31             #region 读取属性的第二种方法
    32             try
    33             {
    34                 XmlReader xr = XmlReader.Create(path);
    35                 while (xr.Read())
    36                 {
    37                     if (xr.HasAttributes)
    38                     {
    39                         //Console.WriteLine("<" + xr.Name + ">的属性:");
    40                         //for (int i = 0; i < xr.AttributeCount; i++)
    41                         //{                            
    42                         //xr.MoveToAttribute(i);                            
    43                         Console.WriteLine("<" + xr.Name + ">的属性:");
    44                         Console.WriteLine("{0}={1}", xr.Name, xr.Value);
    45                         //}                                                                  
    46                     }
    47                 }
    48             }
    49             catch (Exception ex)
    50             {
    51                 Console.WriteLine(ex.Message);
    52             } 
    53             #endregion
    54             Console.ReadKey();
    55         }
    56     }
    57 }
    View Code

    解决后,我的代码如下:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Xml;
     6 
     7 namespace ReadAttribute
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             string path = @"e:	estfilemyMail.xml";
    14             
    15             #region 读取属性的第一种方法
    16             //string date;
    17             //try
    18             //{
    19             //    XmlReader xr = XmlReader.Create(path);
    20             //    xr.ReadToFollowing("mail");
    21             //    date = xr.GetAttribute("date");
    22             //    Console.Write("信件的日期为:");
    23             //    Console.WriteLine(date);
    24             //}
    25             //catch (Exception ex)
    26             //{
    27             //    Console.WriteLine(ex.Message);
    28             //} 
    29             #endregion
    30 
    31             #region 读取属性的第二种方法
    32             try
    33             {
    34                 XmlReader xr = XmlReader.Create(path);
    35                 while (xr.Read())
    36                 {
    37                     if (xr.HasAttributes)
    38                     {
    39                         Console.WriteLine("<" + xr.Name + ">的属性:");
    40                         for (int i = 0; i < xr.AttributeCount; i++)
    41                         {                            
    42                         xr.MoveToAttribute(i);                            
    43                         //Console.WriteLine("<" + xr.Name + ">的属性:");
    44                         Console.WriteLine("{0}={1}", xr.Name, xr.Value);
    45                         }                                                                  
    46                     }
    47                 }
    48             }
    49             catch (Exception ex)
    50             {
    51                 Console.WriteLine(ex.Message);
    52             } 
    53             #endregion
    54             Console.ReadKey();
    55         }
    56     }
    57 }
    View Code

    解决后,上面的代码可以不用知道元素的名称和属性的名称的情况下读取XML文件中的所有属性名及其值。这种方法有点像迭代器遍历数组,只不过这里的元素变成了XML的节点和元素,而且这里可以把XML的属性也可以看成是XML的节点(”可以看成XML元素的子节点“),这样元素看成是节点,属性也看成是节点,都当做节点处理,也就是说这个遍历可以看成是遍历XML的节点。这样也就可以解释上面代码中的 xr.Name、xr.Value了,他们分别可以看成是XML的节点名和节点值(即属性名和属性值)。如此,这样的方法便不难理解了!

    如果你无法简洁的表达你的想法,那只说明你还不够了解它。
  • 相关阅读:
    POJ 1700 过河坐船最短时间问题
    C++继承与派生上机记录
    POJ 1007 DNA Sorting
    大一C++语言程序设计6-20上机作业
    POJ 1006 Biorhythms
    对“C++添加一个头文件和extern以及全局变量和局部变量问题”的解释
    C++添加一个头文件和extern以及全局变量和局部变量问题(16.3.19上机的一小题)
    Node 中的模块化(module对象中的exports以及导入require方法)
    http 模块
    path 路径模块
  • 原文地址:https://www.cnblogs.com/zhuochangjing/p/3902664.html
Copyright © 2011-2022 走看看