I have an xml document like as shown below.
<?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>
I am trying to read nodes and its elements like this
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);
I am able to read nodes and its elements at first time but if i edit values in xml file and try to read again then i am getting error "NullreferenceException was unhandled" Object reference not set to an instance of an object.
What might be the problem and how to rectify it?
Any suggestions?