Hi All,
Trying to build a language user control where you can enter a danish entry,
followed by an english translation, a story id (People enter 0 when there
is no story) and comments (This is the plural version of this word etc).
They then select whether this entry is a keyword, phrase, question or part
of a story.
It calls a class DanishDB to perform the insert and sends the information
as parameters. I've tried extending this class by adding a Method to check
to see if it already exists (Only implemented for Keyword entry so far). I
thought it would work but it doesn't.
first I tried the query "keyword[danish='"+danishEntry+"']" but that
returned everything within the xml document and it just saw the entry as a
new entry?????
I've now tried this "danish='" + danishEntry + "'"; and it gives me the
error mentioned in the subject.
The class looks like the following:
using System;
using System.Xml;
using System.Xml.XPath;
namespace Mandia
{
/// <summary>
/// Summary description for EntryDB. This handles all the handling
of the xml files.
/// </summary>
public class DanishDB
{
public DanishDB()
{
//
// TODO: Add constructor logic here
//
}
string FilePath; // Used to point
the code to the correct XML file
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.
{
// I've returned early because I wanted to
see what was in it
return checkResult;
XmlDocument doc = new XmlDocument();
switch(entryType)
{
case "Keyword":
// a keyword was entered
FilePath = XmlPath
+ "DanishKeyWords.xml";
// use case to ensure that
you load up the right xml document
doc.Load(FilePath);
// create a new Keyword
entry
XmlElement newKeywordEntry
= doc.CreateElement("keyword");
// create a new danish
element
XmlElement newDanishKeyword
= doc.CreateElement("danish");
newDanishKeyword.InnerText
= danishEntry;
newKeywordEntry.AppendChild
(newDanishKeyword);
// create a new english
element
XmlElement
newEnglishKeyword = doc.CreateElement("english");
newEnglishKeyword.InnerText
= englishEntry;
newKeywordEntry.AppendChild
(newEnglishKeyword);
// create a new comment
element
XmlElement
newKeywordComment = doc.CreateElement("comment");
newKeywordComment.InnerText
= entryComment;
newKeywordEntry.AppendChild
(newKeywordComment);
// create a new StoryId
element
XmlElement
newKeywordStoryId = doc.CreateElement("storyid");
newKeywordStoryId.InnerText
= storyId;
newKeywordEntry.AppendChild
(newKeywordStoryId);
// add to the current
document
doc.DocumentElement.AppendChild(newKeywordEntry);
// update the correct xml
document
doc.Save(FilePath);
break;
case "Phrase":
// a useful keyphrase was entered
FilePath = XmlPath
+ "DanishKeyPhrases.xml";
// use case to ensure that
you load up the right xml document
doc.Load(FilePath);
// create a new KeyPhrase
entry
XmlElement
newKeyphraseEntry = doc.CreateElement("keyphrase");
// create a new danish
element
XmlElement
newDanishKeyphrase = doc.CreateElement("danish");
newDanishKeyphrase.InnerText = danishEntry;
newKeyphraseEntry.AppendChild(newDanishKeyphrase);
// create a new english
element
XmlElement
newEnglishKeyphrase = doc.CreateElement("english");
newEnglishKeyphrase.InnerText = englishEntry;
newKeyphraseEntry.AppendChild(newEnglishKeyphrase);
// create a new comment
element
XmlElement
newKeyphraseComment = doc.CreateElement("comment");
newKeyphraseComment.InnerText = entryComment;
newKeyphraseEntry.AppendChild(newKeyphraseComment);
// create a new StoryId
element
XmlElement
newKeyphraseStoryId = doc.CreateElement("storyid");
newKeyphraseStoryId.InnerText = storyId;
newKeyphraseEntry.AppendChild(newKeyphraseStoryId);
// add to the current
document
doc.DocumentElement.AppendChild(newKeyphraseEntry);
// update the correct xml
document
doc.Save(FilePath);
break;
case "Question":
// a useful keyphrase was entered
FilePath = XmlPath
+ "DanishKeyQuestions.xml";
// use case to ensure that
you load up the right xml document
doc.Load(FilePath);
// create a new KeyPhrase
entry
XmlElement
newKeyquestionEntry = doc.CreateElement("keyquestion");
// create a new danish
element
XmlElement
newDanishKeyquestion = doc.CreateElement("danish");
newDanishKeyquestion.InnerText = danishEntry;
newKeyquestionEntry.AppendChild(newDanishKeyquestion);
// create a new english
element
XmlElement
newEnglishKeyquestion = doc.CreateElement("english");
newEnglishKeyquestion.InnerText = englishEntry;
newKeyquestionEntry.AppendChild(newEnglishKeyquestion);
// create a new comment
element
XmlElement
newKeyquestionComment = doc.CreateElement("comment");
newKeyquestionComment.InnerText = entryComment;
newKeyquestionEntry.AppendChild(newKeyquestionComment);
// create a new StoryId
element
XmlElement
newKeyquestionStoryId = doc.CreateElement("storyid");
newKeyquestionStoryId.InnerText = storyId;
newKeyquestionEntry.AppendChild(newKeyquestionStoryId);
// add to the current
document
doc.DocumentElement.AppendChild(newKeyquestionEntry);
// update the correct xml
document
doc.Save(FilePath);
break;
default: //
handle all other entries
//TODO: Add default error
handler
break;
} // end switch
return "Your entry has been added
successfully!";
} // end of else statement
}
private string CheckDocument(string entryType, string
danishEntry, string XmlPath )
{
XmlDocument checkdoc = new XmlDocument();
switch(entryType)
{
case "Keyword":
// a keyword was entered
FilePath = XmlPath
+ "DanishKeyWords.xml";
// use case to ensure that you load
up the right xml document
checkdoc.Load(FilePath);
// create the XPath query to search
the xml document
string keywordSearch = "danish='" +
danishEntry + "'";
// create the XPath Navigator
XPathNavigator nav =
checkdoc.CreateNavigator();
// execute the search to try and
retrieve an xml node
XPathNodeIterator xpi = nav.Select(
keywordSearch );
// 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
return
xpi.Current.Value; //if it doesn't exist I wanted to see what it did have;
}
break;
case "Phrase": //
a useful keyphrase was entered
// return a flag saying entry exists
return "exists";
break;
case "Question":
// a useful keyphrase was entered
// return a flag saying entry exists
return "exists";
break;
default: // handle
all other entries
//TODO: Add default error handler
// return a flag saying entry exists
return "exists";
break;
} // end switch
}
}
}
I was wondering if someone could point out where I was going wrong. This
is my first real attempt at c#and .Net so the class isn't perfect.
The xml document looks like:
<?xml version="1.0" encoding="utf-8"?>
<keywords>
<keyword>
<danish>jeg</danish>
<english>I</english>
<comment>
</comment>
<storyid>1</storyid>
</keyword>
<keyword>
<danish>er</danish>
<english>am</english>
<comment>
</comment>
<storyid>1</storyid>
</keyword>
</keywords>
Any comments would be appreciated.
Thanks,
John
You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.