思路: 声明一个类数组,用来存放通过dataset读取出来的xml的数据信息。
数据:xml——>dataset——>类数组
xml:
<?xml version="1.0" encoding="gb2312"?>
<user>
<student>
<name>LiMei</name>
<sex>Female</sex>
<age>20</age>
<birthdate>19911218</birthdate>
</student>
</user>
main:
class Student
{
public string name = "hehe";
}
Student[] s = new Student[5];
int i;
DataSet objDS = new DataSet();
objDS.ReadXml("UsInfo.xml");
int counts = objDS.Tables["user"].Rows.Count;
//程序没有提示错误和警告,只是运行到这里提示“未处理NullReferenceException:未将对象引用设置到对象的实例”
for (i = 0; i < counts; i++)
{
s[i].name = objDS.Tables["user"].Rows[i][0].ToString();
Console.Out.WriteLine(s[i].name);
}
数据:xml——>dataset——>类数组
xml:
<?xml version="1.0" encoding="gb2312"?>
<user>
<student>
<name>LiMei</name>
<sex>Female</sex>
<age>20</age>
<birthdate>19911218</birthdate>
</student>
</user>
main:
class Student
{
public string name = "hehe";
}
Student[] s = new Student[5];
int i;
DataSet objDS = new DataSet();
objDS.ReadXml("UsInfo.xml");
int counts = objDS.Tables["user"].Rows.Count;
//程序没有提示错误和警告,只是运行到这里提示“未处理NullReferenceException:未将对象引用设置到对象的实例”
for (i = 0; i < counts; i++)
{
s[i].name = objDS.Tables["user"].Rows[i][0].ToString();
Console.Out.WriteLine(s[i].name);
}
objDS.Tables["user"].Rows.Count;
改为
objDS.Tables[0].Rows.Count;试试。
你在创建DATASET对象时,并没有给它一个名为"user"的表名。