Click here to Skip to main content
16,004,761 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to take an XML document that look it this.
My C# code.

public void loadXML()
{
	ProjectReferralTableControlRow rows = new ProjectReferralTableControlRow();



	System.Xml.XmlReader obj = System.Xml.XmlReader.Create(@"c:\temp\reg.xml");
	{
		obj.Read();
		obj.ReadStartElement("Registration");
		obj.ReadStartElement("BusinessName",  rows.SubConstructionManager1.Text);
		obj.ReadStartElement("OwnerName", rows.SubContactName1.Text);
		obj.ReadStartElement("Address", rows.SubAddress1.Text);
		obj.ReadStartElement("City", rows.SubCity1.Text);
		obj.ReadStartElement("State", rows.SubState1.Text);
		obj.ReadStartElement("Zipcode", rows.SubZip1.Text);
		obj.ReadStartElement("Phone", rows.SubPhone1.Text);
		obj.ReadStartElement("Fax", rows.SubFax1.Text);
		obj.ReadStartElement("EmailAddress", rows.SubEmail1.Text);

	}
	obj.ReadEndElement();								
}


getting the following error.

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error: 


Line 979:                obj.Read();
Line 980:                obj.ReadStartElement("Registration");
<big></big>Line 981:                obj.ReadStartElement("BusinessName",  rows.SubConstructionManager1.Text);
Line 982:                obj.ReadStartElement("OwnerName", rows.SubContactName1.Text);
Line 983:                obj.ReadStartElement("Address", rows.SubAddress1.Text);
Posted
Updated 24-Feb-11 5:06am
v2
Comments
Nish Nishant 24-Feb-11 11:08am    
Post the stack trace here.
postonoh 24-Feb-11 11:14am    
NullReferenceException: Object reference not set to an instance of an object.]
Test.UI.Controls.TestPage.BaseProjectReferralTableControl.loadXML() in f:\Test\Test\App_Code\Test Page\TestPage.Controls.cs:981
Test.UI.Controls.TestPage.BaseProjectReferralTableControl..ctor() in f:\Test\Test\App_Code\Test Page\TestPage.Controls.cs:754
Test.UI.Controls.TestPage.ProjectReferralTableControl..ctor() +43
ASP.project_referral_TestPage_aspx.__BuildControlProjectReferralTableControl() in f:\Test\Test\Test Page\TestPage.aspx:542
ASP.project_referral_TestPage_aspx.__BuildControl__control27(Control __ctrl) in f:\Test\Test\Test Page\TestPage.aspx:539
System.Web.UI.CompiledTemplateBuilder.InstantiateIn(Control container) +12
System.Web.UI.UpdatePanel.CreateContents() +118
System.Web.UI.UpdatePanel.set_ContentTemplate(ITemplate value) +222
ASP.project_referral_TestPage_aspx.__BuildControlProjectReferralTableControlUpdatePanel() in f:\Test\Test\Test Page\TestPage.aspx:539
ASP.project_referral_TestPage_aspx.__BuildControlForm1() in f:\Test\Test\Test Page\TestPage.aspx:25
ASP.project_referral_TestPage_aspx.__BuildControlBody1() in f:\Test\Test\Test Page\TestPage.aspx:24
ASP.project_referral_TestPage_aspx.__BuildControlTree(Test_Test_TestPage_aspx __ctrl) in f:\Test\Test\Test Page\TestPage.aspx:1
ASP.project_referral_TestPage_aspx.FrameworkInitialize() in f:\Test\Test\Test Page\TestPage.aspx.cs:912308
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +56
System.Web.UI.Page.ProcessRequest() +80
System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +21
System.Web.UI.Page.ProcessRequest(HttpContext context) +49
ASP.project_referral_TestPage_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\Test\964f5007\2c6ae9b9\App_Web_xmte9nts.0.cs:0
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
Nish Nishant 24-Feb-11 11:15am    
So doesn't it show the line in loadXML that throws the exception?
postonoh 24-Feb-11 11:20am    
Line 981: obj.ReadStartElement("BusinessName", rows.SubConstructionManager1.Text);
Nish Nishant 24-Feb-11 12:41pm    
It's most likely that rows.SubConstructionManager1 is null. So when you call Text on it, it will throw.

The whole approach is bad, because you make assumptions on XML contents. You also use hard-coded immediate string constants in you code, which is always bad, you should make it data-driven instead, for example you can use XSD as a source of valid elements.

So, instead of trying to read what you're expected, try to read all until you read the document to its end and try to match what you can see on the road. Start from the sample on this page: http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.read.aspx[^].

—SA
 
Share this answer
 
Comments
postonoh 24-Feb-11 15:35pm    
I tried something like this at first.

XmlTextReader obj = new XmlTextReader(@"c:\temp\reg.xml");
string sr= "";
while(obj.Read())


switch (obj.NodeType)
{
case XmlNodeType.Element:
sr = obj.Name;
break;
case XmlNodeType.Text:

//this.SubConstructionManagerLabel1.Text = obj.GetAttribute("BusinessName");
rows.SubConstructionManager1.Text = obj.GetAttribute("BusinessName");
//break;
rows.SubContactName1.Text = obj.GetAttribute("OwnerName");
//break;
rows.SubAddress1.Text = obj.GetAttribute("Address");
// break;
rows.SubCity1.Text = obj.GetAttribute("City");
//break;
rows.SubState1.Text = obj.GetAttribute("State");
// break;
rows.SubZip1.Text = obj.GetAttribute("ZipCode");
// break;
rows.SubPhone1.Text = obj.GetAttribute("Phone");
// break;
rows.SubFax1.Text = obj.GetAttribute("Fax");
//break;
rows.SubEmail1.Text = obj.GetAttribute("Email");
break;
}
postonoh 24-Feb-11 15:42pm    
Also I have page that is data driven and user search for companies when he select which companies he/she wants it saves the selection to XML file. They want to be able to recall them later use this file to populate other pages at a different time.
Sergey Alexandrovich Kryukov 24-Feb-11 17:22pm    
You're doing same thing, work based on assumption. Best it can come to is unsupportable code...
--SA
Espen Harlinn 24-Feb-11 16:52pm    
My 5 - Good point, it would be better to create something that supports binding ...
Sergey Alexandrovich Kryukov 24-Feb-11 17:22pm    
Thank you.
-SA
You could create an XML Schema[^] for your xml document - this wil also help to improve the integrity of your xml file.

Once you have a schema, you can use xsd[^] to create a dataset for your xml file.

Content can be loaded into the dataset using the DataSet.ReadXml[^].

Now that you have a dataset containing the data for your xml file, you can set up bindings between your dataset and the UI elements using the design features of Visual Studio.

Regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Feb-11 17:20pm    
Very good, 5.
--SA
Espen Harlinn 24-Feb-11 17:37pm    
Thank you SAKryukov!
postonoh 24-Feb-11 22:56pm    
I really appreciation this information. I new to using XML so thanks. I learning and want to do it right the first time. So thanks I used the XSD to create my XML Schema. Thanks again.
I believe that everyone needs a mentor to lead them into to success!
Sergey Alexandrovich Kryukov 25-Feb-11 0:38am    
You're welcome.
Thank you for accepting my Answer.
And well... no, not everyone needs a mentor in fact. Yes, I'm sure.
--SA
Espen Harlinn 25-Feb-11 3:24am    
Thanks Mo John!
Did you try to DEBUG and see what are the values around the lines?

Looks like this part has issues: rows.SubConstructionManager1.Text
Check if rows or rows.SubConstructionManager1 is not null
 
Share this answer
 
Comments
postonoh 24-Feb-11 12:15pm    
Yes it is null, I am trying to insert the data found in the xml document to the text boxes
Sandeep Mewara 24-Feb-11 12:18pm    
And thats the reason of error. Now, you need to correct your code and make sure that the object is populated and has the data as desired.
postonoh 24-Feb-11 12:29pm    
Thanks, I put this snippet of code out here so maybe someone can inform me what I am missing.
I will do some research on other forums.

Thanks for you insight,
Nish Nishant 24-Feb-11 12:42pm    
Voted 5, proposed as answer.
Sergey Alexandrovich Kryukov 24-Feb-11 15:29pm    
You are right, of cause, my 5, but the approach itself should be very different.
Please see my Answer.
--SA

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