xml

xml read and update

Page 1 of 1

12 Replies - 1456 Views - Last Post: 14 January 2010 - 08:42 PM Rate Topic: -----

#1 hatem@iegs.ca  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-December 09

xml

Post icon  Posted 14 January 2010 - 07:34 AM

Hi everybody
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>




Is This A Good Question/Topic? 0
  • +

Replies To: xml

#2 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: xml

Posted 14 January 2010 - 08:22 AM

I'm interested in this too. Simply, how can you insert something into an XML file?
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: xml

Posted 14 January 2010 - 08:28 AM

If you are using .net 3.5 SP1 or greater, then I would suggest using LINQ-To-XML.

Here is a good blog post on LINQ-to-XML. It includes changing data and saving the xml file.

http://www.4guysfrom...s/112509-1.aspx
Was This Post Helpful? 0
  • +
  • -

#4 hatem@iegs.ca  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-December 09

Re: xml

Posted 14 January 2010 - 08:47 AM

View Posteclipsed4utoo, on 14 Jan, 2010 - 07:28 AM, said:

If you are using .net 3.5 SP1 or greater, then I would suggest using LINQ-To-XML.

Here is a good blog post on LINQ-to-XML. It includes changing data and saving the xml file.

http://www.4guysfrom...s/112509-1.aspx



Hi eclipsed4utoo
Yes I'm using .net 3.5 but the things different in this case are, the depth of the xml that I work on it's not fixed, it depends on the length of the entered string, it will iterate the same xml fragment when found the input is different from the current node, then insert in this location (current node) a new child for the same parent node.
Was This Post Helpful? 0
  • +
  • -

#5 hatem@iegs.ca  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-December 09

Re: xml

Posted 14 January 2010 - 09:12 AM

View Postpapuccino1, on 14 Jan, 2010 - 07:22 AM, said:

I'm interested in this too. Simply, how can you insert something into an XML file?



Hi papuccino1
the 'insertNode1' function insert an element into the xml file that exists in code
Was This Post Helpful? 0
  • +
  • -

#6 DuoShin85  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 135
  • Joined: 18-December 09

Re: xml

Posted 14 January 2010 - 10:50 AM

If I'm reading your post correctly, you're using an XML file as a Dictionary, and you're trying to figure out a way to insert a new word into the dictionary. If so, I think the way you're doing it right now is a bit clunky. I'm thinking of a way to do it that would seem cleaner. I'll get back to you when I finish.
Was This Post Helpful? 0
  • +
  • -

#7 hatem@iegs.ca  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-December 09

Re: xml

Posted 14 January 2010 - 11:09 AM

View PostDuoShin85, on 14 Jan, 2010 - 09:50 AM, said:

If I'm reading your post correctly, you're using an XML file as a Dictionary, and you're trying to figure out a way to insert a new word into the dictionary. If so, I think the way you're doing it right now is a bit clunky. I'm thinking of a way to do it that would seem cleaner. I'll get back to you when I finish.



Hi
It's exactly that what I wanna do, for example in the xml file there is "quebec" as a word, if I want to insert "queue", the frequence on action1, action2 and action3 will be incremented by 1 after that insert a new element as following.
<action4 act = "e">
<name>b</name>
<frequence>1</frequence>
<action5 act = "e">
<name>u</name>
<frequence>1</frequence>
</action5>
</action4>

suppose I want to insert "quebec" again, only the frequence for each node will be incremented by 1
Was This Post Helpful? 0
  • +
  • -

#8 DuoShin85  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 135
  • Joined: 18-December 09

Re: xml

Posted 14 January 2010 - 11:11 AM

Well then, I'll bow out, and hope that someone else can help you. :)
Was This Post Helpful? 0
  • +
  • -

#9 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: xml

Posted 14 January 2010 - 12:55 PM

View Posthatem@iegs.ca, on 14 Jan, 2010 - 02:09 PM, said:

View PostDuoShin85, on 14 Jan, 2010 - 09:50 AM, said:

If I'm reading your post correctly, you're using an XML file as a Dictionary, and you're trying to figure out a way to insert a new word into the dictionary. If so, I think the way you're doing it right now is a bit clunky. I'm thinking of a way to do it that would seem cleaner. I'll get back to you when I finish.



Hi
It's exactly that what I wanna do, for example in the xml file there is "quebec" as a word, if I want to insert "queue", the frequence on action1, action2 and action3 will be incremented by 1 after that insert a new element as following.
<action4 act = "e">
<name>b</name>
<frequence>1</frequence>
<action5 act = "e">
<name>u</name>
<frequence>1</frequence>
</action5>
</action4>

suppose I want to insert "quebec" again, only the frequence for each node will be incremented by 1


what is all the "action", "name", and "frequence" for?
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • Joined: 16-October 07

Re: xml

Posted 14 January 2010 - 01:13 PM

Wow. Putting aside that the XML design is awful, how should you do it? Use the XmlDocument that's in memory. Just use intrinsic XPath. Never, ever, write your own XML freehand (e.g. InnerXml), use the XmlDocument's methods.

I put together the following as a test. It seems to do what you're looking for:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Diagnostics;

namespace ConsoleApplication1 {
	class Program {
		XmlDocument xmlDoc = new XmlDocument();

		public Program() { xmlDoc.AppendChild(xmlDoc.CreateElement("actions")); }
		
		public Program(string filename) { xmlDoc.Load(filename); }

		private XmlElement getNewActionNode(char ch, string nodeName) {
			XmlElement actNode = xmlDoc.CreateElement(nodeName);
			actNode.SetAttribute("act", ch.ToString());
			XmlElement nNode = xmlDoc.CreateElement("name");
			nNode.InnerText = ch.ToString();
			actNode.AppendChild( nNode );
			XmlElement fNode = xmlDoc.CreateElement("frequence");
			fNode.InnerText = "1";
			actNode.AppendChild( fNode );
			return actNode;
		}

		private void addWord(string s, XmlElement ele, int level) {
			if (s.Length==0) { return; }
			char ch = s[0];
			string nodeName = "action" + level;
			XmlElement foundEle = (XmlElement)ele.SelectSingleNode(nodeName + "[@act='" + ch + "']");
			if (foundEle==null) {
				foundEle = getNewActionNode(ch, nodeName);
				ele.AppendChild(foundEle);
			} else {
				XmlElement fNode = (XmlElement)foundEle.SelectSingleNode("frequence");
				fNode.InnerText = (int.Parse(fNode.InnerText) + 1).ToString();
			}
			addWord(s.Substring(1), foundEle, level+1);
		}

		public void addWord(string s) { addWord(s, xmlDoc.DocumentElement, 1); }

		static void Main(string[] args) {
			Program pgm = new Program();
			pgm.addWord("quebec");
			pgm.addWord("queue");
			using (StringWriter sw = new StringWriter()) {
				XmlTextWriter w = new XmlTextWriter(sw);
				w.Formatting = Formatting.Indented;
				pgm.xmlDoc.WriteTo(w);
				Debug.WriteLine(sw.ToString());
			}
		}
	}
}



Results:
<actions>
  <action1 act="q">
	<name>q</name>
	<frequence>2</frequence>
	<action2 act="u">
	  <name>u</name>
	  <frequence>2</frequence>
	  <action3 act="e">
		<name>e</name>
		<frequence>2</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>
		<action4 act="u">
		  <name>u</name>
		  <frequence>1</frequence>
		  <action5 act="e">
			<name>e</name>
			<frequence>1</frequence>
		  </action5>
		</action4>
	  </action3>
	</action2>
  </action1>
</actions>



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#11 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: xml

Posted 14 January 2010 - 03:31 PM

hello ppl stop using this xml code because its too long and it looks scarry use linq-xml because its nice and easies your task however in the beginning its tough but latter on it will be easier to work on ....:P
XElement prajay=new XElement("prajay",....);


go on adding your content to it
Was This Post Helpful? 0
  • +
  • -

#12 DuoShin85  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 135
  • Joined: 18-December 09

Re: xml

Posted 14 January 2010 - 07:31 PM

I just started using Linq to Xml this afternoon on my personal project. I have fallen in love with it, and I'm beating myself up for not looking at it earlier.
Was This Post Helpful? 0
  • +
  • -

#13 hatem@iegs.ca  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-December 09

Re: xml

Posted 14 January 2010 - 08:42 PM

View Postbaavgai, on 14 Jan, 2010 - 12:13 PM, said:

Wow. Putting aside that the XML design is awful, how should you do it? Use the XmlDocument that's in memory. Just use intrinsic XPath. Never, ever, write your own XML freehand (e.g. InnerXml), use the XmlDocument's methods.

I put together the following as a test. It seems to do what you're looking for:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Diagnostics;

namespace ConsoleApplication1 {
	class Program {
		XmlDocument xmlDoc = new XmlDocument();

		public Program() { xmlDoc.AppendChild(xmlDoc.CreateElement("actions")); }
		
		public Program(string filename) { xmlDoc.Load(filename); }

		private XmlElement getNewActionNode(char ch, string nodeName) {
			XmlElement actNode = xmlDoc.CreateElement(nodeName);
			actNode.SetAttribute("act", ch.ToString());
			XmlElement nNode = xmlDoc.CreateElement("name");
			nNode.InnerText = ch.ToString();
			actNode.AppendChild( nNode );
			XmlElement fNode = xmlDoc.CreateElement("frequence");
			fNode.InnerText = "1";
			actNode.AppendChild( fNode );
			return actNode;
		}

		private void addWord(string s, XmlElement ele, int level) {
			if (s.Length==0) { return; }
			char ch = s[0];
			string nodeName = "action" + level;
			XmlElement foundEle = (XmlElement)ele.SelectSingleNode(nodeName + "[@act='" + ch + "']");
			if (foundEle==null) {
				foundEle = getNewActionNode(ch, nodeName);
				ele.AppendChild(foundEle);
			} else {
				XmlElement fNode = (XmlElement)foundEle.SelectSingleNode("frequence");
				fNode.InnerText = (int.Parse(fNode.InnerText) + 1).ToString();
			}
			addWord(s.Substring(1), foundEle, level+1);
		}

		public void addWord(string s) { addWord(s, xmlDoc.DocumentElement, 1); }

		static void Main(string[] args) {
			Program pgm = new Program();
			pgm.addWord("quebec");
			pgm.addWord("queue");
			using (StringWriter sw = new StringWriter()) {
				XmlTextWriter w = new XmlTextWriter(sw);
				w.Formatting = Formatting.Indented;
				pgm.xmlDoc.WriteTo(w);
				Debug.WriteLine(sw.ToString());
			}
		}
	}
}



Results:
<actions>
  <action1 act="q">
	<name>q</name>
	<frequence>2</frequence>
	<action2 act="u">
	  <name>u</name>
	  <frequence>2</frequence>
	  <action3 act="e">
		<name>e</name>
		<frequence>2</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>
		<action4 act="u">
		  <name>u</name>
		  <frequence>1</frequence>
		  <action5 act="e">
			<name>e</name>
			<frequence>1</frequence>
		  </action5>
		</action4>
	  </action3>
	</action2>
  </action1>
</actions>



Hope this helps.



Thanks you very much, it's great it helped. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1