?
Solved

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

Posted on 2009-04-09
3
Medium Priority
?
216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
CuteBug earned 1000 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 1000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
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.
Suggested Courses

801 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