Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
C#
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?
Posted
Comments
[no name] 7-Dec-15 9:19am    
At which line it is throwing error..
vebi1000 9-Dec-15 13:43pm    
var Samples = (from s in xmlDocument.Descendants("Transfers")
select new Sample

I am getting error here in reading sample data.
Joshi, Rushikesh 7-Dec-15 12:11pm    
Can you make sure when you are updating XML file you are not keeping it open, it may possible that due to file open that XMLDocument object remains null.

I am just guessing.

Thanks
Rushi
vebi1000 9-Dec-15 13:44pm    
Hi rushi,
I am closing the file as well. but still same error.
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900