Solved

I need help using with an XML document in my C# application.

Posted on 2009-04-09
3
211 Views
Last Modified: 2013-12-17
Hi all,
I have written a C#.Net application that consumes a web service that returns an XML document.  When it returns the document I populate my form.  This is working fine.  But there are times that it returns the following error:

<?xml version="1.0" ?><XMLGetCustomerShipmentDetailError>No records found per requested criteria</XMLGetCustomerShipmentDetailError>

Is there anyway to tell my application that if the element <XMLGetCustomerShipmentDetailError> is found then exit the function?  How can I do this?  If you could provide a code sample that would be great.

thanks in advance,
mainrotor
0
Comment
Question by:mainrotor
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
CuteBug earned 250 total points
ID: 24109263
Use LINQ. It makes it easy.


using System.Xml.Linq;
 
XDocument xDoc = XDocument.Load(xmlString);
 
if (xDoc.Element("XMLGetCustomerShipmentDetailError") != null)
    return;

Open in new window

0
 
LVL 5

Assisted Solution

by:Buginator
Buginator earned 250 total points
ID: 24109399
Hello mainrotor,

Here is another solution that I personally prefer over LINQ which I use in my own projects:
XmlDocument xmlDoc = new XmlDocument();
 
//Just an example string, load your own XML here
String XML = "<?xml version=\"1.0\" ?><XMLGetCustomerShipmentDetailError>No records found per requested criteria</XMLGetCustomerShipmentDetailError>";
 
xmlDoc.LoadXml(XML);
XmlNode xmlNode = xmlDoc.SelectSingleNode("/XMLGetCustomerShipmentDetailError");
 
if (xmlNode != null)
            return;

Open in new window

0
 
LVL 5

Expert Comment

by:Buginator
ID: 24109440
Forgot the namespace:
using System.Xml;

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foolproof security solutions has become one of the key necessities of every e-commerce or Internet banking website. If you too own an online shopping site then its vital for you to equip your web portal with customer security features that can allow…
Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question