using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;namespace ReadAttribute
{
    class Program
    {
        static void Main(string[] args)
        {
            string mypath = @"F:\Learning\C# 3.0\Practice Examples\Chap12\Test2.xml";
            string date = string.Empty ;            try
            {                XmlReader myrd = XmlReader.Create(mypath);
                
                 date = myrd.GetAttribute("mail");
                Console.WriteLine("信件的日期为:");
                Console.WriteLine(date);    
            }
            catch (Exception ex)
            {                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
}请问这样为什么读取不到   XML里面 mail节点里的 时间的信息呢?
请问是什么原因呢?可以运行不报错但是读不出来
下面是我写的XML:<?xml version="1.0" encoding="utf-8"?>
<!-- Name:myXml.xml -->
<mail date = "10-30-2010"><to>Ma</to>
<from>Zhang</from>
<title>Hello</title>
<body>This is a letter</body>
</mail>请各位大虾帮帮忙啊。小弟还在学习中,谢谢

解决方案 »

  1.   


    XmlDocument doc = new XmlDocument();
                doc.LoadXml(System.IO.File.ReadAllText(Application.StartupPath + "//QuestionInfo.xml", Encoding.Default));            XmlNodeList list = doc.GetElementsByTagName("add");
      

  2.   

    试试!!
    XmlDocument doc = new XmlDocument();
    string mypath = @"F:\Learning\C# 3.0\Practice Examples\Chap12\Test2.xml";
    doc.Load(mypath);
    string sDate = doc.DocumentElement.GetAttribute("date");
    Console.WriteLine("信件的日期为:"+sDate);
      

  3.   

    XElement root = XElement.Load("F:\\a.xml");
    Console.WriteLine(root.Attribute("date").Value);
      

  4.   

    XmlReader myrd = XmlReader.Create(mypath);                    while (myrd.Read())
                        {
                            if (string.Compare(myrd.Name, "mail", true) == 0)
                            {
                                Console.WriteLine("信件的日期为:"+myrd.GetAttribute("date"));
                                break;
                            }
                        }