-2

Could someone please tell me how to extract the Errors element from the below XML using XPath and C# under the .NET 3.5 Framework?

<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:do_OTA_VehAvailRateRQResponse xmlns:ns1="urn:vanguard-web-webservices-ota-IOTA" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<return xsi:type="xsd:string"><OTA_VehAvailRateRS TimeStamp="2013-04-03T18:16:00" TransactionIdentifier="215997103" SequenceNmbr="1" Target="Production" Version="2.0" xmlns="http://www.opentravel.org/OTA/2003/05">
  <Errors>
    <Error Type="1" Code="999">COMPANY NAME FIELD IS INVALID</Error>
  </Errors>
</OTA_VehAvailRateRS>
</return>
</ns1:do_OTA_VehAvailRateRQResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
rstuppi
  • 361
  • 1
  • 3
  • 11

4 Answers4

0

Use:

//*[name()='Errors']

This selects any element in the XML document, whose name is "Errors" -- regardless of the namespace of the element.

Or, if you want to be more precise and take account of the exact namespaces elements are in, you need to use an XmlNamespaceManager object and register any needed association between prefix and namespace using its AddNamespace() method.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • This is a poor recommendation because the path expression is dependent on the namespace prefixes used in the source. If you really want a namespace-independent query, use `//*[local-name()='Errors']` – Michael Kay Apr 04 '13 at 07:27
  • @MichaelKay, It may be a "poor recommendation" generally, but in this case the wanted `Errors` element has no prefix and the XPath expression uses this fact. **Selecting an element regardless of the prefix could in the general case select elements that have the same local name but belong to different namespaces -- *this* is a poor recommendation**! Also, you haven't read the answer to the end, or you would have seen the recommendation for an exact solution using registered namespaces -- with a link to documentation and code example. – Dimitre Novatchev Apr 04 '13 at 14:36
0

Try this code:

XmlDocument xmlDoc = new XmlDocument();

xmlDoc .LoadXml(xmlSting); //If u have a xml string, you can create xmlDocument like this, otherwise u can use file name to create xmlDocument.

String error = .SelectSingleNode("SOAP-ENV:Envelope/SOAP-ENV:Body/return/Errors/Error").Value;
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
0
XmlNodeList errorNodes=new XmlDocument().Load("xmlFilePath").GetElementsByTagName("Errors");
Victor Mukherjee
  • 10,487
  • 16
  • 54
  • 97
0

We can't tell where you went wrong if you don't show us your code.

But we can guess. Your Errors element is in a namespace, and failing to recognize this fact is such a common error among the inexperienced that I would happily bet this is your bug.

To find elements in a namespace you need a path such as //e:Errors where the prefix e is bound (at the C# API level) to the namespace http://www.opentravel.org/OTA/2003/05.

(Sorry, you don't say that you are inexperienced. I'm inferring this from the fact that if you were experienced, you would have posted your XPath code).

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • With all due respect, telling people that they are "inexperienced" in an answer and re-iterating this statement isn't something one should be proud of. We are all inexperienced -- in *any* area -- to a certain degree. So calling someone inexperienced doesn't give us even a single bit of new information. – Dimitre Novatchev Apr 04 '13 at 16:12
  • Yes, binding to a namespace was the problem. My confusion stemmed from the fact that the namespace http://www.opentravel.org/OTA/2003/05 was not prefixed in the XML and so I did not know what prefix to use in the path. The solution was simply to create a prefix such as "ota" (could I have named it "WTF"), bind the namespace and use the ota prefix in the XPAth. – rstuppi Apr 04 '13 at 17:06
  • @Dimitre Problem is, when people don't show us their code we have to make guesses, and part of the guesswork is to guess their level of experience, because that determines what kind of mistakes they are likely to be making. So I was simply describing my reasoning process, not implying any criticism. – Michael Kay Apr 04 '13 at 19:17
  • @Michael No offense taken. – rstuppi Apr 05 '13 at 03:07