C# parsing XML document

Need to parse XML document including wildcards in the text between the

Page 1 of 1

4 Replies - 10050 Views - Last Post: 20 December 2009 - 03:58 AM Rate Topic: -----

#1 iliali16   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 19-December 09

C# parsing XML document

Post icon  Posted 19 December 2009 - 10:56 AM

Hello people, I am doing a chatbot that reads a string from keyboard and answers by compearing the input with a <pattern> in the XML file and return the <template> for the paricular <pattern>. Now my problem is that at the moment I can chat with the bot and it will answer back perfectly but the chatter will have to use proper english and if he make a mistake the chatbot will not understand. That is why i desided to extend it to work with wildcards atleast to make it use the * so that it will check some words from the keyboard input and it will match it to the right <pattern> in the XML file. Bellow I have put some of my code that is doing the chat

static void parser(InstantMessage im)
		{
			string answer1 = null; //Used to handle answers that are known. In case of unknown answer it answers the default message.
			
			XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.
			xmlDoc.Load(@"D:\University Notes\Honers\Connecting Web Science\test.xml"); //* load the XML document from the specified file.
			//* Get elements.
			XmlNodeList category = xmlDoc.GetElementsByTagName("category");
			XmlNodeList pattern = xmlDoc.GetElementsByTagName("pattern");
			XmlNodeList template = xmlDoc.GetElementsByTagName("template");
			
			for (int i = 0; i < pattern.Count; i++)
			{
				if (im.Message == pattern[i].InnerText)
				{
					answer1 = template[i].InnerText;
					answer = answer1;
				}
			}
				if (answer1 == null)
				{
					answer = "I am sorry I didn't understand what you mean. Do you want me to teach you how to play Texas Hold'em Poker";
				}
		}




The XML file has been attached and it has one small example. Thanks for any help!

Attached File(s)

  • Attached File  test.xml (1.17K)
    Number of downloads: 428


Is This A Good Question/Topic? 1
  • +

Replies To: C# parsing XML document

#2 FlashM   User is offline

  • D.I.C Lover
  • member icon

Reputation: 383
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: C# parsing XML document

Posted 19 December 2009 - 12:57 PM

1. solution: Use LINQ to XML (really perfect way to achieve what you are trying)

2. solution and more complicated: Create a SOUNDEX tool that can correct users type mistakes.
Was This Post Helpful? 1
  • +
  • -

#3 iliali16   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 19-December 09

Re: C# parsing XML document

Posted 19 December 2009 - 02:37 PM

View PostFlashM, on 19 Dec, 2009 - 11:57 AM, said:

1. solution: Use LINQ to XML (really perfect way to achieve what you are trying)

2. solution and more complicated: Create a SOUNDEX tool that can correct users type mistakes.


I have been looking till now about LINQ but what is the idea I can't find something usefull.
Was This Post Helpful? 0
  • +
  • -

#4 FlashM   User is offline

  • D.I.C Lover
  • member icon

Reputation: 383
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: C# parsing XML document

Posted 20 December 2009 - 02:55 AM

Well, you might have been looking in the wrong place :-) LINQ is really powerful because it provides a way to query objects just like you would query database with SQL. For example you could do this: Select all the objects from xml document where some attribute's name start with S. You can use group by clause, sum, avg, you can order returned results by ascending or descending order. You can even join entities and so on.

I'm sure there are plenty of good example and tutorials on how to use LINQ to XML. Just try googling it. I could post you some sample later today.

This post has been edited by FlashM: 20 December 2009 - 02:56 AM

Was This Post Helpful? 0
  • +
  • -

#5 iliali16   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 19-December 09

Re: C# parsing XML document

Posted 20 December 2009 - 03:58 AM

View PostFlashM, on 20 Dec, 2009 - 01:55 AM, said:

Well, you might have been looking in the wrong place :-) LINQ is really powerful because it provides a way to query objects just like you would query database with SQL. For example you could do this: Select all the objects from xml document where some attribute's name start with S. You can use group by clause, sum, avg, you can order returned results by ascending or descending order. You can even join entities and so on.

I'm sure there are plenty of good example and tutorials on how to use LINQ to XML. Just try googling it. I could post you some sample later today.


Ok I think the problem is that I want to, for example, place the following query: What is your name
The code should query the XML file and find that text include in between tags something like that <pattern> * name *</pattern> willbe mtched because it doesn't matter what is before "name" as long as program will match it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1