• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

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

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
mainrotor
Asked:
mainrotor
  • 2
2 Solutions
 
CuteBugCommented:
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
 
BuginatorCommented:
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
 
BuginatorCommented:
Forgot the namespace:
using System.Xml;

Open in new window

0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now