Solved

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

Posted on 2009-04-09
3
208 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
Comment Utility
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
Comment Utility
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
Comment Utility
Forgot the namespace:
using System.Xml;

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
creating a flowchart from an algorithm 5 19
C# SQL BULK INSERT CLASS 5 34
Round a string to two digits 12 24
Image(2) 3 21
Foreword (May 2015) This web page has appeared at Google.  It's definitely worth considering! https://www.google.com/about/careers/students/guide-to-technical-development.html How to Know You are Making a Difference at EE In August, 2013, one …
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now