Solved

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

Posted on 2009-04-09
3
210 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
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.

911 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

22 Experts available now in Live!

Get 1:1 Help Now