从XML文件中读取

时间:2010-06-29 07:44:20

标签: c# xml .net-2.0

我有一个像这样的XML文件:

<students>
<student rollNo="1" Name ="A" Grade="A"/>
<student rollNo="2" Name ="B" Grade="A"/>
<student rollNo="3" Name ="C" Grade="A"/>
<student rollNo="4" Name ="D" Grade="A"/>
</students>

我需要通过提供Name从XML文件中获取GraderollNo

2 个答案:

答案 0 :(得分:3)

嗯,首先,这不是一个完整的XML文件。但是,假设你实际得到了这样的东西:

<students>
  <student rollNo="1" Name ="A" Grade="A"/>
  <student rollNo="2" Name ="B" Grade="A"/>
  <student rollNo="3" Name ="C" Grade="A"/>
  <student rollNo="4" Name ="D" Grade="A"/>
</students>

你可以使用这样的东西:

XDocument doc = XDocument.Load(filename);
XElement element = doc.Root
                      .Elements("student")
                      .Where(x => (int?) x.Attribute("rollNo") == rollNo);
string name = (string) x.Attribute("Name");
string grade = (string) x.Attribute("Grade");

答案 1 :(得分:1)

你可以写这样的东西(C#2):

using System;
using System.Xml;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"D:\stack.xml";
            string[] result = ParseXmlFile(path, 2);
        }

        private static string[] ParseXmlFile(string path, int rollNo)
        {
            XmlReaderSettings xtrs = new XmlReaderSettings();
            xtrs.IgnoreComments = true;
            xtrs.IgnoreProcessingInstructions = true;
            xtrs.IgnoreWhitespace = true;

            using (XmlReader xtr = XmlReader.Create(path, xtrs))
            {
                while (xtr.Read())
                {
                    if (xtr.Name == "students")
                    {
                        return ParseStudent(xtr, rollNo);
                    }
                }
            }

            throw new ArgumentException();
        }

        private static string[] ParseStudent(XmlReader reader, int rollNo)
        {
            while (reader.Read())
            {
                if (reader.Name == "student")
                {
                    string _name = string.Empty;
                    string _grade = string.Empty;
                    int _rollNo = 0;

                    while (reader.MoveToNextAttribute())
                    {
                        switch (reader.Name)
                        {
                            case "Name":
                                _name = reader.Value;
                                break;
                            case "Grade":
                                _grade = reader.Value;
                                break;
                            case "rollNo":
                                _rollNo = Convert.ToInt32(reader.Value);
                                break;
                            default:
                                break;
                        }
                    }

                    if (_rollNo == rollNo)
                        return new string[] { _name, _grade };
                }
            }
            throw new ArgumentException();
        }
    }
}