Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>XPath in the version you are using matches nodes in order, which you cannot change.</p> <p>There are several ways to achieve it:</p> <h1>XSLT</h1> <p>One simple way is to perform an <a href="http://en.wikipedia.org/wiki/XSLT" rel="nofollow">XSL Transform</a></p> <p>Once you learn XSLT, it becomes really easy to do this sort of thing. Such as:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"&gt; &lt;xsl:template match="Operaciones"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="Operacion"&gt; &lt;xsl:sort select="index-of(('Alta','Media','Baja'), @Prioridad)"/&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="@*|node()"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="@*|node()"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>And then you can query all your <code>Operaciones</code> easily. If you do it the XSLT route, it means that you can upgrade your queries without having to recompile your software. It is probably the solution you wish to utilise.</p> <p><strong>Update: Mathias pointed out that we're not looking for an alphabetical sort. I will keep the solutions below for reference, but now I'd advise to go for the correct XSLT solution above.</strong></p> <h1>XPathDocument</h1> <p>The second way is more programatic, which utilises Microsoft's own features:</p> <p>Assuming <code>doc</code> is an <code>XmlDocument</code>, you can do the following to turn it into an <code>XPathDocument</code>:</p> <pre><code>XPathDocument xpathDoc = new XPathDocument(new XmlNodeReader(doc)); </code></pre> <p>Once you do that, you can run the following query:</p> <pre><code>XPathNavigator nav = xpathDoc.CreateNavigator(); XPathExpression expression = XPathExpression.Compile(@"//Operacion"); expression.AddSort(@"@Prioridad", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Text); XPathNodeIterator iterator = nav.Select(expression); foreach (XPathNavigator operation in iterator) { Console.WriteLine("Found priority '{0}'",operation.GetAttribute("Prioridad","")); } </code></pre> <h1>Linq</h1> <p>Third way, using Linq (you might want to check the syntax here):</p> <pre><code>XDocument xDoc = XDocument.Load(new XmlNodeReader(doc)); var operations = xDoc.Descendants("Operacion").OrderBy(s=&gt;(string)s.Attribute("Prioridad")); foreach(var operation in operations) { Console.WriteLine("hey -&gt; {0}", operation); } </code></pre> <p>I suggest you do it via Linq, but if you are transforming data, then go for XSLT every time.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload