Simplest of Things are Hard
-
Hi, My Question Is: with regards to XML in a C# context. For the record, I'm using VS 2002, running Win2000 dayly updated, cel400mhz,192ram,dotNET1.1 The Premices: Put simply, I am trying to use XML as a relationnal database for an application. I figured an XML schema, an XML file, and some code to plug it in and that was it. The application is to store
contacts
information and more types of info as we upgrade it. So what makes sense here is to have an XML file for each type of info. The user needs to be able to do the usual, such asview, add, modify, delete
entries. The XML files should reside in the main application folder. Now, this is part of what I tried: -please bare in mind I'm kinda new with xml...;) The Faulty Code:string item; XmlDocument myDoc = new XmlDocument(); myDoc.Load(Application.StartupPath + "\\Contact.xml"); XmlNode myNode = myDoc.FirstChild; item = myNode.FirstChild.InnerText.ToString(); txtFirstName.Text = item;
The Error Message: An unhandled exception of type 'System.NullReferenceException' occurred in Orchus_temp.exe Additional information: Object reference not set to an instance of an object. The Question Redux: " How do I add, modify, view and delete data that is stored in an XML file and rendered in a C# application? " I propose to write an article about the resolution of theses issues, since a lot of people have to resolve these kind of problems, I believe. So feel free to respond extensively as this will be the substace of my first article! :-D Thanks!:) Antoine Dubuc Montreal, Canada This by our hands that dream, "I shall find a way or make one!" -
Hi, My Question Is: with regards to XML in a C# context. For the record, I'm using VS 2002, running Win2000 dayly updated, cel400mhz,192ram,dotNET1.1 The Premices: Put simply, I am trying to use XML as a relationnal database for an application. I figured an XML schema, an XML file, and some code to plug it in and that was it. The application is to store
contacts
information and more types of info as we upgrade it. So what makes sense here is to have an XML file for each type of info. The user needs to be able to do the usual, such asview, add, modify, delete
entries. The XML files should reside in the main application folder. Now, this is part of what I tried: -please bare in mind I'm kinda new with xml...;) The Faulty Code:string item; XmlDocument myDoc = new XmlDocument(); myDoc.Load(Application.StartupPath + "\\Contact.xml"); XmlNode myNode = myDoc.FirstChild; item = myNode.FirstChild.InnerText.ToString(); txtFirstName.Text = item;
The Error Message: An unhandled exception of type 'System.NullReferenceException' occurred in Orchus_temp.exe Additional information: Object reference not set to an instance of an object. The Question Redux: " How do I add, modify, view and delete data that is stored in an XML file and rendered in a C# application? " I propose to write an article about the resolution of theses issues, since a lot of people have to resolve these kind of problems, I believe. So feel free to respond extensively as this will be the substace of my first article! :-D Thanks!:) Antoine Dubuc Montreal, Canada This by our hands that dream, "I shall find a way or make one!" -
Hi, My Question Is: with regards to XML in a C# context. For the record, I'm using VS 2002, running Win2000 dayly updated, cel400mhz,192ram,dotNET1.1 The Premices: Put simply, I am trying to use XML as a relationnal database for an application. I figured an XML schema, an XML file, and some code to plug it in and that was it. The application is to store
contacts
information and more types of info as we upgrade it. So what makes sense here is to have an XML file for each type of info. The user needs to be able to do the usual, such asview, add, modify, delete
entries. The XML files should reside in the main application folder. Now, this is part of what I tried: -please bare in mind I'm kinda new with xml...;) The Faulty Code:string item; XmlDocument myDoc = new XmlDocument(); myDoc.Load(Application.StartupPath + "\\Contact.xml"); XmlNode myNode = myDoc.FirstChild; item = myNode.FirstChild.InnerText.ToString(); txtFirstName.Text = item;
The Error Message: An unhandled exception of type 'System.NullReferenceException' occurred in Orchus_temp.exe Additional information: Object reference not set to an instance of an object. The Question Redux: " How do I add, modify, view and delete data that is stored in an XML file and rendered in a C# application? " I propose to write an article about the resolution of theses issues, since a lot of people have to resolve these kind of problems, I believe. So feel free to respond extensively as this will be the substace of my first article! :-D Thanks!:) Antoine Dubuc Montreal, Canada This by our hands that dream, "I shall find a way or make one!"myDoc.FirstChild;
is the problem. this is not a safe way to read an XML file - the first child could be a processing instruction... you are better of usingSelectSingleNode(_xpath_);
where xpath is the thing you want - in you case I'm guessing its "/contact/firstname" depending on you xml file structure...
"When the only tool you have is a hammer, a sore thumb you will have."
-
myDoc.FirstChild;
is the problem. this is not a safe way to read an XML file - the first child could be a processing instruction... you are better of usingSelectSingleNode(_xpath_);
where xpath is the thing you want - in you case I'm guessing its "/contact/firstname" depending on you xml file structure...
"When the only tool you have is a hammer, a sore thumb you will have."
Good. Xpath is something new to me. I'll check that out and try it. thank! Antoine This by our hands that dream, "I shall find a way or make one!"
-
myDoc.FirstChild;
is the problem. this is not a safe way to read an XML file - the first child could be a processing instruction... you are better of usingSelectSingleNode(_xpath_);
where xpath is the thing you want - in you case I'm guessing its "/contact/firstname" depending on you xml file structure...
"When the only tool you have is a hammer, a sore thumb you will have."
Hi Phillip, Ok. So I am trying to display the firstname of the first contact in the Contact.xml file...
XmlDocument myDoc = new XmlDocument(); myDoc.Load(Application.StartupPath + "\\Contact.xml"); txtfirstname.Text = myDoc.SelectSingleNode("//Contact[1]//firstname").Value;
What am I missing? I am trying to find a good XPath resources, but its hard. MSDN doen't do it for me up to now. thanks :) Antoine This by our hands that dream, "I shall find a way or make one!"