我有一个xml文档,如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<Projects>
<Project>
<ProjectNo>111</ProjectNo>
<Client/>
<Responsible/>
<Participants/>
<Date>13-11-2015</Date>
</Project>
<Transfers ID="1">
<SampleID>1</SampleID>
<SampleNo>222</SampleNo>
<ChamberNo>p111</ChamberNo>
</Transfers>
<Transfers ID="2">
<SampleID>2</SampleID>
<SampleNo>32</SampleNo>
<ChamberNo>p311</ChamberNo>
</Transfers>
<Transfers ID="3">
<SampleID>3</SampleID>
<SampleNo>42</SampleNo>
<ChamberNo>p211</ChamberNo>
</Transfers>
</Projects>
我正在尝试读取节点及其元素
XDocument xmlDocument = XDocument.Load(GlobalVariables.XmlPath + "\\" + GlobalVariables.ProjectID + ".xml");
var project = from p in xmlDocument.Descendants("Project")
select new Project
{
ProjectNo = p.Element("ProjectNo").Value,
Client = p.Element("Client").Value,
ProjectResponsible = p.Element("Responsible").Value,
Participants = p.Element("Participants").Value,
Date = Convert.ToDateTime(p.Element("Date").Value).Date,
};
var Samples = (from s in xmlDocument.Descendants("Transfers")
select new Sample
{
SampleID = Convert.ToInt32(s.Element("SampleID").Value),
SampleNo = s.Element("SampleNo").Value,
ChamberNo = s.Element("ChamberNo").Value,
}
);
int SamplesNo = Samples.Count();
CreateWordDocument(project, Samples, SamplesNo);
我能够在第一时间读取节点及其元素,但如果我在xml文件中编辑值并尝试再次读取,那么我收到错误&#34; NullreferenceException未处理&#34;对象引用未设置为对象的实例。
可能是什么问题以及如何纠正? 有什么建议吗?