Solved

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

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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.
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…

838 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