Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 11th, 2007, 10:10 PM
Authorized User
 
Join Date: Apr 2006
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default Getting value of an element in an XML file

Hello. I have recently completed a program that uses a web service to define vocabulary words. Now I'm adding the ability to first check whether each word is spelled correctly before using the web service to define it.

So, I am using the following code to get an XML stream containing spelling suggestions:

================================================== =================
//Use web service to check spelling
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://ws.cdyne.com/SpellChecker/check.asmx/CheckTextBody");
                string postsourcedata;
                postsourcedata = "BodyText=" + word + "&LicenseKey=" + licenseKey;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = postsourcedata.Length;
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
                Stream writeStream = request.GetRequestStream();
                UTF8Encoding encoding = new UTF8Encoding();
                byte[] bytes = encoding.GetBytes(postsourcedata);
                writeStream.Write(bytes, 0, bytes.Length);
                writeStream.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8);
                string page = readStream.ReadToEnd();
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(page);
                XmlNode root = doc.DocumentElement;


                //PROBLEM HERE; HOW TO DISPLAY A SINGLE "SUGGESTIONS" ELEMENT
                MessageBox.Show(root. .ToString());

================================================== ====================
You can see a sample of the XML file returned at this address: http://ws.cdyne.com/SpellChecker/che...enseKey=string

For now, I'm just going to show all possible spelling suggestions, if any, in a MessageBox, before I worry about allowing the user to select the one they wish to use. However, as my comments suggest, how can I go about getting the value of a single <Suggestions> element? I know how to get the InnerText of the entire document (root.ParentNode["DocumentSummary"].InnerText.ToString()), but I need to be able to offer each suggestion one at a time! Please and thanks for your help!

Computers will never surpass the human brain; no computer will ever be able to replicate human stupidity.
__________________
If only computers could write programs themselves...
 
Old April 12th, 2007, 08:40 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Usually, you would use an XPath query to extract a particular node/attribute value:

node = doc.SelectSingleNode("<xpath query goes here>");
something = node.Value;

-Peter
 
Old April 12th, 2007, 03:33 PM
Authorized User
 
Join Date: Apr 2006
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've never worked with XPath's before. I looked them up on MSDN but cannot seem to get the hang of them after a lot of experimenting. Could you give me some sample code of how to display each <Suggestions> element in a MessageBox using, say, a foreach loop or something? Thanks.

Computers will never surpass the human brain; no computer will ever be able to replicate human stupidity.
 
Old April 12th, 2007, 08:16 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Something like this:

lstNodes = doc.SelectNodes("//Suggestions");
foreach(XmlNode objNode in lstNodes){
  MessageBox.Show(objNode.Value);
}

You'll probably need to play with that a bit, but that's the general idea.

-Peter
 
Old April 12th, 2007, 10:50 PM
Authorized User
 
Join Date: Apr 2006
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank-you for your help! After looking at your sample code, and re-reading the MSDN documentation, it started to make sense! Turns out I needed to use the correct namespace in the XPath Query. In case you would like to know, here is the final code:

================================================== ======================
//Loop through XML file and check if suggestions exist
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
                nsmgr.AddNamespace("sp", "http://ws.cdyne.com/");

                XmlNodeList lstNodes;

                lstNodes = doc.SelectNodes("/sp:DocumentSummary/sp:MisspelledWord/sp:Suggestions", nsmgr);
                foreach (XmlNode objNode in lstNodes)
                {
                    MessageBox.Show(objNode.InnerText.ToString());
                }

================================================== ====================

Once again thank-you for your help!

Computers will never surpass the human brain; no computer will ever be able to replicate human stupidity.
 
Old April 13th, 2007, 07:54 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Namespaces can be tricky, or perhaps it's just that I haven't worked with XML docs the contain namespaces very much.

Glad you got it sorted out.

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a XML element value chandler7272 C# 0 June 15th, 2006 02:55 AM
Read a XML element value chandler7272 C# 0 June 15th, 2006 02:54 AM
Converting XML to XML (making element mandatory) boondocksaint20 XSLT 8 April 28th, 2006 10:54 AM
Adding an Element with Attribute to XML file xergic Classic ASP XML 0 November 20th, 2004 08:26 AM
How to insert a new element in XML file? kmriyad C# 1 September 20th, 2004 09:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.