I used methods to set the values. This is my first class and I handled the problem this way. It is complaining about un-reachable code (Because I use return in the switch statement) but I think that it's unavoidable (Just a begininner so there probably is a better way). This is my completed class. What do you think.
using System; using System.Xml; using System.Xml.XPath; namespace Mandia { /// <summary> /// Summary description for EntryDB. Has methods to update danish xml files. /// </summary> public class DanishDB { public DanishDB() { // // TODO: Add constructor logic here // } public string AddToDocument(string entryType, string danishEntry, string englishEntry, string entryComment,string storyId, string xmlPath ) { // the checkResult either contains exists or doesn't exist. string checkResult = CheckDocument(entryType, danishEntry, xmlPath); if (checkResult == "exists") { return "I am sorry but your entry was not added as it already exists!"; } else // the entry doesn't exist so it can be added. { string xmlDocToAdd; // used to store the path to the correct xml doc XmlDocument doc = new XmlDocument();// used to hold the xml document XmlElement newEntry; // used to hold the new entry XmlElement newDanishEntry; // used to hold the danish entry XmlElement newEnglishEntry; // used to hold the english entry XmlElement newCommentEntry; // used to hold the comment entry XmlElement newStoryIdEntry; // used to hold the story id xmlDocToAdd = PathToDocument (entryType,xmlPath); // use the method to retrieve the path to the correct xml doc. // load up the right xml document doc.Load(xmlDocToAdd); // create a new entry newEntry = doc.CreateElement(entryType); // create a new danish element newDanishEntry = doc.CreateElement ("danish"); newDanishEntry.InnerText = danishEntry; newEntry.AppendChild(newDanishEntry); // create a new english element newEnglishEntry = doc.CreateElement ("english"); newEnglishEntry.InnerText = englishEntry; newEntry.AppendChild(newEnglishEntry); // create a new comment element newCommentEntry = doc.CreateElement ("comment"); newCommentEntry.InnerText = entryComment; newEntry.AppendChild(newCommentEntry); // create a new storyId element newStoryIdEntry = doc.CreateElement ("storyid"); newStoryIdEntry.InnerText = storyId; newEntry.AppendChild(newStoryIdEntry); // add to the current document doc.DocumentElement.AppendChild(newEntry); // update the correct xml document doc.Save(xmlDocToAdd); return "Your entry has been added successfully!"; } // end of else statement } private string CheckDocument(string entryType, string danishEntry, string xmlPath ) { XmlDocument checkdoc = new XmlDocument(); XPathNavigator nav; // the tool to conduct the search XPathNodeIterator xpi; // the result of the search string xmlDoc; // used to load up the xml document to check string xmlQuery; // query to see if entry already exists xmlDoc = PathToDocument(entryType,xmlPath); // use the method to retrieve the path to the correct xml doc. xmlQuery = QueryForDocument(entryType,danishEntry); // use the method to retrieve the correct XPath query // use case to ensure that you load up the right xml document checkdoc.Load(xmlDoc); // create the XPath Navigator nav = checkdoc.CreateNavigator(); // execute the search to try and retrieve an xml node xpi = nav.Select( xmlQuery ); // check to see if entry already exists in xml document if ( xpi.Count !=0 ) { // return a flag saying entry exists return "exists"; } else { // return a flag to say that it doesn't exist. In this case just the value. return xpi.Current.Value; } } // end of CheckDocument method private string PathToDocument(string entryType, string xmlPath) { string filePath; // used to store the path to the correct xml document //This method is used to find the complete path dependent on what type of entry it is and where the directly lies in relation to the user control that called it. switch(entryType) { case "keyword": // a keyword was entered filePath = xmlPath + "DanishKeyWords.xml"; return filePath; break; case "keyphrase": // a useful keyphrase was entered filePath = xmlPath + "DanishKeyPhrases.xml"; return filePath; break; case "keyquestion": // a useful keyphrase was entered filePath = xmlPath + "DanishKeyQuestions.xml"; return filePath; break; default: // handle all other entries //TODO: Add default error handler filePath = xmlPath + "DanishKeyWordsNot.xml"; return filePath; break; } // end switch } // end of PathToDocument method private string QueryForDocument(string entryType, string danishEntry) { string entrySearch; // used to store the XPATH query //This method is used to create a specific XPATH query depending on what type of entry it is. switch(entryType) { case "keyword": // a keyword was entered // create the XPath query to search the xml document entrySearch = "/keywords/keyword/danish[. =\"" + danishEntry + "\"]"; return entrySearch; break; case "keyphrase": // a useful keyphrase was entered // create the XPath query to search the xml document entrySearch = "/keyphrases/keyphrase/danish[. =\"" + danishEntry + "\"]"; return entrySearch; break; case "keyquestion": // a useful keyphrase was entered // create the XPath query to search the xml document entrySearch = "/keyquestions/keyquestion/danish[. =\"" + danishEntry + "\"]"; return entrySearch; break; default: // handle all other entries //TODO: Add default error handler entrySearch = "test"; return entrySearch; break; } // end switch } // end of QueryForDocument method } } You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.