I'm working on an xml file. I want to an xml document that contains words (dictionnary), searching on the document I have the solution for that bat my issue is on inserting an new word into this document,
The insertion works as follows:
1- start read the document
2- if the first char is found continue the iteration while the second following char is found and increment the frequence value for each node (here the issue)
3- else: insert a new element (done)
the following is the code
public void addNode1()
{
string str = ""; the word to be inserted
int pos = 0;
fileName = "actionsW.xml";
try
{
// check if the first char from str is an existing element in xml data file
if (isAFirstElement(str.Substring(0, 1)))
{
XmlTextReader reader = new XmlTextReader(fileName);
XmlDocument doc = new XmlDocument();
doc.Load(reader);
reader.Close();
string expression = "/attaqScenarios/action1";
expr = nav.Compile(expression + "[name='" + str.Substring(0, 1) + "']");
iterator = nav.Select(expr);
XPathNavigator nodesNav = iterator.Current;
XPathNodeIterator nodesText = nodesNav.SelectDescendants(XPathNodeType.Element, false);
int index = 1;
while (nodesText.MoveNext())
{
XmlElement root = doc.DocumentElement;
XPathNavigator navlocal = nodesText.Current.Clone();
int freq = 0;
if (navlocal.Value.ToString() == str.Substring(index -1, 1))// check while the current navigator value has the same valeu of the chosen substring, then kepp going and update the frequence value by incrementing by 1
{
navlocal .MoveToNext();//To get the frequence value
freq = navlocal.ValueAsInt + 1;
navlocal.SetValue((freq + 1).ToString());
nodesText.Current.SetValue(freq.ToString());
navlocal.MoveToParent();
doc.Save(fileName);
//tmpstr = navlocal.LocalName.ToString();
//bPos = Int32.Parse(tmpstr.Substring(6, tmpstr.Length - 6)) + 1;
index++;
expression = expression + "/action" + index;
}
else // if it's not the case then update the current node by adding the <action" + index + ">" element
{
//insertNode(str.Substring(bPos, str.Length - bPos),bPos , str.Length);
}
index++;
}
}
else //if the forst char from the string doesn't exist then insert a new element
{
insertNode(str, 1, str.Length); // this part works fine
}//end if
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
}
}// end addNode1
// insert new action1 element
public void insertNode(string str, int beginPos, int endPos)
{
try
{
XmlTextReader reader = new XmlTextReader(fileName);
XmlDocument doc = new XmlDocument();
doc.Load(reader);
reader.Close();
XmlNode currNode;
XmlDocumentFragment docFrag = doc.CreateDocumentFragment();
docFrag.InnerXml = "";
int i = str.Length - 1;
while (endPos >= beginPos)
{
docFrag.InnerXml = "<action" + endPos + " act=\"" + str.Substring (i,1) + "\">" +
"<name>" + str.Substring(i, 1) + "</name>" +
"<frequence>1</frequence>" +
docFrag.InnerXml +
"</action" + endPos + ">";
endPos = endPos - 1;
i = i - 1;
}
// insert the availability node into the document
currNode = doc.DocumentElement;
currNode.InsertAfter(docFrag, currNode.LastChild);
//save the output to a file
doc.Save(fileName);
this.DialogResult = DialogResult.OK;
}
catch (Exception ex)
{
MessageBox.Show (ex.ToString());
this.DialogResult = DialogResult.Cancel;
}
}//end insertNode
<?xml version="1.0" encoding="ISO-8859-1" ?>
- <actions>
- <action1 act="H">
<name>H</name>
<frequence>1</frequence>
- <action2 act="A">
<name>A</name>
<frequence>1</frequence>
- <action3 act="T">
<name>T</name>
<frequence>1</frequence>
- <action4 act="E">
<name>E</name>
<frequence>1</frequence>
- <action5 act="M">
<name>M</name>
<frequence>1</frequence>
</action5>
</action4>
</action3>
</action2>
</action1>
- <action1 act="k">
<name>k</name>
<frequence>1</frequence>
- <action2 act="a">
<name>a</name>
<frequence>1</frequence>
- <action3 act="m">
<name>m</name>
<frequence>1</frequence>
- <action4 act="e">
<name>e</name>
<frequence>1</frequence>
- <action5 act="l">
<name>l</name>
<frequence>1</frequence>
</action5>
</action4>
</action3>
</action2>
</action1>
- <action1 act="q">
<name>q</name>
<frequence>2</frequence>
- <action2 act="u">
<name>u</name>
<frequence>1</frequence>
- <action3 act="e">
<name>e</name>
<frequence>1</frequence>
- <action4 act="b">
<name>b</name>
<frequence>1</frequence>
- <action5 act="e">
<name>e</name>
<frequence>1</frequence>
- <action6 act="c">
<name>c</name>
<frequence>1</frequence>
</action6>
</action5>
</action4>
</action3>
</action2>
</action1>
</actions>

New Topic/Question
Reply




MultiQuote






|