XElement XPathEvaluate

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am getting two different xpath query results depending on if I put the XPathDocument stream into a file or hold it in memory.<br/>
Here is my code
write to file method:

<div style="color:Black;background-color:White; <pre>
WriteXpathDocument(xml, <span style="color:#A31515; "c:\temp\xpathdoc.xml");
<span style="color:Blue; var doc = XDocument.Load(<span style="color:#A31515; "c:\temp\xpathdoc.xml");
<span style="color:Blue; var count = doc.XPathSelectElements(<span style="color:#A31515; "//Test/tAsOf/value").Count();
<span style="color:Blue; var o = doc.XPathEvaluate(<span style="color:#A31515; "count(Test/tAsOf/value)");

<span style="color:Blue; private <span style="color:Blue; static <span style="color:Blue; void WriteXpathDocument(IXPathNavigable xpathDoc, <span style="color:Blue; string filename)
        {
            <span style="color:Green; // Create XpathNavigator instances from XpathDoc instance.
            <span style="color:Blue; var objXPathNav = xpathDoc.CreateNavigator();

            <span style="color:Green; // Create XmlWriter settings instance.
            <span style="color:Blue; var objXmlWriterSettings = <span style="color:Blue; new XmlWriterSettings {Indent = <span style="color:Blue; true};

            <span style="color:Green; // Create disposable XmlWriter and write XML to file.
            <span style="color:Blue; using (<span style="color:Blue; var objXmlWriter = XmlWriter.Create(filename, objXmlWriterSettings))

            {

                objXPathNav.WriteSubtree(objXmlWriter);

                objXmlWriter.Close();

            }

        }
[/code]
in the debugger count is 1 and o is 1.0.
memorystream version

<pre>XPathDocument xml = ....

var ms = new MemoryStream();
var nav = xml.CreateNavigator();
var xw = XmlWriter.Create(ms);
nav.WriteSubtree(xw);
xw.Flush();
ms.Position = 0;
var xr = XmlReader.Create(ms);
xw.Close();
var doc = XElement.Load(xr);
var count = doc.XPathSelectElements("//Test/tAsOf/value").Count();
var o = doc.XPathEvaluate("count(Test/tAsOf/value)");[/code]
here count and o is both 0!<br/>
<br/>

my xml looks like this

<div style="color:Black;background-color:White; <pre>
<Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://ww
w.w3.org/2001/XMLSchema
<span style="color:Blue; <<span style="color:#A31515; tAsOf<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; value<span style="color:Blue; >634476672000000000<span style="color:Blue; </<span style="color:#A31515; value<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; tAsOf<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; Test<span style="color:Blue; >
[/code]
<br/>
<br/>









View the full article
 
Back
Top