0

I want to get the success value from the xml below:

<PIEngineResponse xmlns="http://www.pervasive.com/">
  <success>TRUE</success>
  <proxyId>10.10.90.15_test_user{3312564C-6A06-533B-52F0-C8F97C413137}</proxyId>
  <engineName>PIEngineController_Engine_245</engineName>
  <response>
    <NOSDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:NOSDocument" xsi:schemaLocation="NewNosXML.xsd">

If I use xpath, I get:

/tns:PIEngineResponse/tns:success 

Which causes the error:

Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.

What can I do to get the success value to avoid this error? Or how could I avoid the original error?

EDIT: Still no luck with code as:

using (TextReader r1 = File.OpenText(localFilePath))
{

    XmlReader r = XmlReader.Create(r1);
    XDocument doc = XDocument.Load(r1, LoadOptions.None);
    XNamespace ns = "http://www.pervasive.com/";
    string successValue = (string) doc.Root.Element(ns + "success");
}

ignore naming conventions for now.

Harry
  • 87,580
  • 25
  • 202
  • 214
GurdeepS
  • 65,107
  • 109
  • 251
  • 387

1 Answers1

0

I would personally use LINQ to XML to get it to start with:

XDocument doc = ...;
XNamespace ns = "http://www.pervasive.com/";
string successValue = (string) doc.Root.Element(ns + "success");

If you really want to use XPath, you need a namespace manager (or an XsltContext, but it doesn't sound like you're using XSLT), just as the exception suggests - see this answer for an example.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194