Connecting Tech Pros Worldwide Forums | Help | Site Map

C# updating xml element

chathura86's Avatar
Member
 
Join Date: May 2007
Location: Homagama, Sri Lanka
Posts: 92
#1: Dec 4 '07
hi, i want to update a xml element.i used the following code

Expand|Select|Wrap|Line Numbers
  1. System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
  2. System.Xml.XmlNode xmlUpdateNode;
  3.  
  4. xmlDoc.Load("XML//App_Var_Data.xml");
  5. xmlUpdateNode = xmlDoc.SelectSingleNode("NAME");
  6. xmlUpdateNode.InnerText = "chathura";
  7. xmlDoc.Save("XML//App_Var_Data.xml");
  8.  
when i execute i get the following exception
Object reference not set to an instance of an object.

please help pme to solve this problem

chathura bamunusinghe

Expert
 
Join Date: Aug 2007
Location: nj,usa
Posts: 299
#2: Dec 4 '07

re: C# updating xml element


The main cause for this type of error is
1. nOt declaring variable
2. Bad scope of the variable
3.Bad inits/constructs


Just check you ahve done all these correctly and also let us know which line is throwing the exception
Shashi Sadasivan's Avatar
Moderator
 
Join Date: Aug 2007
Location: Brisbane, Australia
Posts: 1,414
#3: Dec 4 '07

re: C# updating xml element


Hi,
Could you please provide which line throws that exception?
I would assume that the following line throws the exception

xmlUpdateNode.InnerText = "chathura";

are you sure that the element NAME exists in the xml document (case sensitive)
If it does is it the root element?

if not, what you need to search online is navigating through an xml document (xpath is your friend here)
chathura86's Avatar
Member
 
Join Date: May 2007
Location: Homagama, Sri Lanka
Posts: 92
#4: Dec 4 '07

re: C# updating xml element


Quote:

Originally Posted by Shashi Sadasivan

Hi,
Could you please provide which line throws that exception?
I would assume that the following line throws the exception

xmlUpdateNode.InnerText = "chathura";

are you sure that the element NAME exists in the xml document (case sensitive)
If it does is it the root element?

if not, what you need to search online is navigating through an xml document (xpath is your friend here)

Yes

xmlUpdateNode.InnerText = "chathura";
throws the exception.

i checked. element NAME exists(in upper case)
so please explain about xpath im new to c#

thanks

chathura bamunusinghe
Shashi Sadasivan's Avatar
Moderator
 
Join Date: Aug 2007
Location: Brisbane, Australia
Posts: 1,414
#5: Dec 4 '07

re: C# updating xml element


Hi,
Use Online search for tutorials Please....
XPath Tutorial
Manipulate XML data with XPath and XmlDocument
Reply