开发者

Reading from XML file

I have a XML file like this:

<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>开发者_C百科;

I need to get Name and Grade from XML file by supplying rollNo.


Well, to start with that's not a complete XML file. However, assuming you've actually got something like this:

<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>

you can use something like this:

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");


You can write something like this (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();
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜