我一直在玩 XDocument 和 LINQ。我设法得到一个文件来写:
<SchoolData storeName="mikveIsrael" location="mikve">
<employee>
<personalInfo>
<name>Ilan Berlinbluv</name>
<zip>58505</zip>
</personalInfo>
<employeeInfo>
<salary>5000</salary>
<id>1</id>
</employeeInfo>
</employee>
<employee>
<personalInfo>
<name>Noam Inbar</name>
<zip>58504</zip>
</personalInfo>
<employeeInfo>
<salary>4500</salary>
<id>2</id>
</employeeInfo>
</employee>
</SchoolData>
我一直在尝试使用以下代码读取值:
public void QueryDoc(XDocument doc)
{
var data = (from item in doc.Descendants("employee")
select new {
name = item.Element("personalInfo").Element("name").Value,
salary = item.Element("employeeInfo").Element("salary").Value,
ID = item.Element("employeeInfo").Element("ID").Value,
zip = item.Element("personalInfo").Element("zip").Value
});
foreach (var p in data)
{
Console.WriteLine(p.ToString());
}
}
但是,当我尝试运行代码时,它给了我一个例外:Object reference not set to an instance of an object.
我一直在关注本教程,并且在他们的屏幕上它可以工作,但在我的屏幕上却没有。