• 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的节点名和节点值(即属性名和属性值)。如此,这样的方法便不难理解了!

    如果你无法简洁的表达你的想法,那只说明你还不够了解它。
  • 相关阅读:
    [CF1439B] Graph Subset Problem
    [CF1439C] Greedy Shopping
    [CF1119F] Niyaz and Small Degrees
    [ARC101C] Ribbons On the Tree
    [CF1446C] Xor Tree
    11月24日 模拟赛 题解
    UOJ346
    [CF1229C] Konrad and Company Evaluation
    [CF1326F] Wise Men (Hard Version)
    学军联赛模拟 第二十七测 题解
  • 原文地址:https://www.cnblogs.com/zhuochangjing/p/3902664.html
Copyright © 2020-2023  润新知