改寫舊專案時, 不支援LINQ, 只好寫一段程式記錄起來
<?xml version="1.0" encoding="UTF-8"?>
<StudentInfo>
<Student ID="Stu001">
<NAME>PETER</NAME>
<COLLEGE>法學院</COLLEGE>
<GENDER>男</GENDER>
<AGE>20</AGE>
<COUNTRY>TAIWAN</COUNTRY>
</Student>
<Student ID="Stu002">
<NAME>Nancy</NAME>
<COLLEGE>理學院</COLLEGE>
<GENDER>女</GENDER>
<AGE>21</AGE>
<COUNTRY>USA</COUNTRY>
</Student>
<Student ID="Stu003">
<NAME>JOHN</NAME>
<COLLEGE>醫學院</COLLEGE>
<GENDER>男</GENDER>
<AGE>19</AGE>
<COUNTRY>JAPAN</COUNTRY>
</Student>
</StudentInfo>
class Program
{
static void Main(string[] args)
{
try
{
List<StudnetInfo> studList = new List<StudnetInfo>();
XmlDocument xmlDoc = new XmlDocument();
string filename = "StudentInfo.xml";
xmlDoc.Load(filename);
for (int i = 0; i <= xmlDoc.SelectSingleNode("StudentInfo").ChildNodes.Count - 1; i++)
{
studList.Add(new StudnetInfo
{
ID = xmlDoc.SelectNodes("//Student")[i].Attributes["ID"].Value,
NAME = xmlDoc.SelectNodes("//Student/NAME")[i].InnerText,
COLLEGE = xmlDoc.SelectNodes("//Student/COLLEGE")[i].InnerText,
GENDER = xmlDoc.SelectNodes("//Student/GENDER")[i].InnerText,
AGE = xmlDoc.SelectNodes("//Student/AGE")[i].InnerText,
COUNTRY = xmlDoc.SelectNodes("//Student/COUNTRY")[i].InnerText
});
}
//output studList
Console.WriteLine("ID\tName\tCollege\tGender\tAge\tCountry");
Console.WriteLine("=================================================");
foreach (var item in studList)
{
Console.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", item.ID, item.NAME, item.COLLEGE, item.GENDER, item.AGE, item.COUNTRY));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadKey();
}
}
class StudnetInfo
{
public string ID { get; set; }
public string NAME { get; set; }
public string COLLEGE { get; set; }
public string GENDER { get; set; }
public string AGE { get; set; }
public string COUNTRY { get; set; }
}
output畫面