读取 XML 并填充列表

35 浏览
0 Comments

读取 XML 并填充列表

这个问题已经有了答案::

如何在C#中读取和解析XML文件?

我有一个学生类,并且想要读取一个包含学生信息的XML文件并将信息放入列表中。

我的代码:

internal class Student
{
    private string name = null;
    private string age = null;
    private string age = null;
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    public string Age
    {
        get { return age; }
        set { age = value; }
    }
}

我正在读取以下XML文件:



 
  Name_1
  1
 
 
  Name_2
  2
 
 
  Name_3
  3
 

这是我的主方法:

        string filePath = "C:\\StudentInfo.xml";
        XmlDocument doc = new XmlDocument();
        doc.Load(filePath);
        StreamReader reader = new StreamReader(filePath);
        string line = "";
        string xmlValue = null;
        Student stu = new Student();
        List stuList = new List();
        while ((line = reader.ReadLine()) != null)
        {
            if (line.Contains(""))
            {
                XmlNodeList elemList = doc.GetElementsByTagName("NAME");
                for (int i = 0; i < elemList.Count; i++)
                {
                    xmlValue = elemList[i].InnerXml;
                    stu.Name = xmlValue;
                    Console.WriteLine(xmlValue);
                }
            }
           stuList.add(stu);
        }

我需要读取XML并将stu对象放入stuList中。

我该怎么做?

更新:我使用了Pradip Nadar提到的LINQ语句

        XDocument xdoc = XDocument.Load("C:\\StudentInfo.xml");
        List lv1s = (from lv1 in xdoc.Descendants("STUDENT")
                              select new Student
                              {
                                  Name = lv1.Element("NAME").Value,
                                  Age = lv1.Element("AGE").Value
                              }).ToList();
        foreach (Student s in lv1s)
        {
            Console.WriteLine(s.Name);
            Console.WriteLine(s.Age);
        }

admin 更改状态以发布 2023年5月20日
0
0 Comments

有两个不同的类:学生类和学生容器类。您只需调用StudentContainer类的方法即可,例如保存、加载或从文本加载。

using System.Xml;
using System.Xml.Serialization;
 public class Student
 { 
    [XmlElement("NAME")]
    public string Name;
    [XmlElement("AGE")]
    public int Age;
 }
 using System.Collections.Generic;
 using System.Xml;
 using System.Xml.Serialization;
 using System.IO;
[XmlRoot("STUDENT_INFO")]
public class StudentContainer()
{
  [XmlArrayItem("STUDENT")]
  public List Stu = new List();
   public void Save(string path)
    {
       var serializer = new XmlSerializer(typeof(StudentContainer));
       using(var stream = new FileStream(path, FileMode.Create))
       {
           serializer.Serialize(stream, this);
       }
}
public static StudentContainer Load(string path)
{
    var serializer = new XmlSerializer(typeof(StudentContainer));
    using(var stream = new FileStream(path, FileMode.Open))
    {
        return serializer.Deserialize(stream) as StudentContainer;
    }
}
     //Loads the xml directly from the given string. Useful in combination with www.text.
     public static StudentContainer LoadFromText(string text) 
{
    var serializer = new XmlSerializer(typeof(StudentContainer));
    return serializer.Deserialize(new StringReader(text)) as StudentContainer;
   }
}

然后使用列表进行迭代。可以使用.Name或.Age。我猜你已经知道如何做了。

**UPDATE**
//To use this
StudentContainer myStudents;
myStudents = DictionaryList.Load("C: <- your path");

0
0 Comments

你可以直接使用 LINQ 操作 XML 文件

在你的情况下

XDocument xdoc = XDocument.Load("C:\\StudentInfo.xml");
        List lv1s = (from lv1 in xdoc.Descendants("STUDENT")
            select new Student
            {
                Name = lv1.Element("NAME").Value,
                Age = lv1.Element("AGE").Value
            }).ToList();

0