/ / Comment trier XmlNodeList lorsque la hiérarchie est différente en C # - c #, xml, xpath

Comment trier XmlNodeList lorsque la hiérarchie est différente en C # - c #, xml, xpath

1) Pouvez-vous s'il vous plaît me dire comment trier la valeur de la balise de code selon la valeur d'attribut donnée.

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Exemple >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> Contribution:

<category>
<code type="pub">e00001</code>
<title>cat1</title>
<ranking>0</ranking>
</category>
<category>
<span>
<code type="pub">e00004</code>
</span>
<title>cat2</title>
<ranking>1</ranking>
</category>
<category>
<code type="pub">e00003</code>
<title>awe</title>
<ranking>10</ranking>
</category>
<category>
<span>
<code type="pub">e00002</code>
</span>
<title>zrt</title>
<ranking>6</ranking>
</category>

Sortie:

<category>
<code type="pub">e00001</code>
<title>cat1</title>
<ranking>0</ranking>
</category>
<category>
<span>
<code type="pub">e00002</code>
</span>
<title>zrt</title>
<ranking>6</ranking>
</category>
<category>
<code type="pub">e00003</code>
<title>awe</title>
<ranking>10</ranking>
</category>
<category>
<span>
<code type="pub">e00004</code>
</span>
<title>cat2</title>
<ranking>1</ranking>
</category>

2) une autre condition:

Est-ce possible pour ces données XML?

s'il vous plaît vérifier ci-dessous exemple et laissez-moi savoir comment trier ces données XML

<root>
<category>
<code type="pub">e00001</code>
<title>cat1</title>
<ranking>0</ranking>
</category>
<category>
<span>
<code type="pub">e00004</code>
</span>
<title>cat2</title>
<ranking>1</ranking>
</category>
<Product>
<category>
<span>
<code type="pub">e00002</code>
</span>
<title>cat3</title>
<ranking>2</ranking>
</category>
<category>
<code type="pub">e00005</code>
<title>awe3</title>
<ranking>11</ranking>
</category>
</Product>
<Product>
<category>
<code type="pub">e00003</code>
<title>awe</title>
<ranking>10</ranking>
</category>
</Product>
</root>

Sortie:

<root>
<category>
<code type="pub">e00001</code>
<title>cat1</title>
<ranking>0</ranking>
</category>
<Product>
<category>
<span>
<code type="pub">e00002</code>
</span>
<title>cat3</title>
<ranking>2</ranking>
</category>
</Product>
<Product>
<category>
<code type="pub">e00003</code>
<title>awe</title>
<ranking>10</ranking>
</category>
</Product>
<category>
<span>
<code type="pub">e00004</code>
</span>
<title>cat2</title>
<ranking>1</ranking>
</category>
<Product>
<category>
<code type="pub">e00005</code>
<title>awe3</title>
<ranking>11</ranking>
</category>
</Product>
</root>

3) condition:

Salut, Veuillez vérifier ci-dessous les données XML d'entrée et laissez-moi savoir comment trier ces données XML.

Contribution:

<root>
<category>
<code type="pub">e00001</code>
<title>test334</title>
<ranking>240</ranking>
</category>
<category>
<span>
<code type="pub">e00007</code>
</span>
<title>test76</title>
<ranking>14</ranking>
</category>
<Product>
<Product-subject-title>
<subject>IN BRIEF</subject>
</Product-subject-title>
<category>
<span>
<code type="pub">e00004</code>
</span>
<title>test3</title>
<ranking>52</ranking>
</category>
<category>
<code type="pub">e00003</code>
<title>test6</title>
<ranking>117</ranking>
</category>
<category>
<code type="pub">e00006</code>
<title>test4</title>
<ranking>116</ranking>
</category>
</Product>
<Product>
<Product-subject-title>
<subject>BIOLOGY ARTICLE</subject>
</Product-subject-title>
<category>
<code type="pub">e00005</code>
<title>test28</title>
<ranking>10</ranking>
</category>
<category>
<code type="pub">e00002</code>
<title>test34</title>
<ranking>160</ranking>
</category>
</Product>

Sortie:

<root>
<category>
<code type="pub">e00001</code>
<title>test334</title>
<ranking>240</ranking>
</category>
<Product>
<Product-subject-title>
<subject>BIOLOGY ARTICLE</subject>
</Product-subject-title>
<category>
<code type="pub">e00002</code>
<title>test34</title>
<ranking>160</ranking>
</category>
</Product>
<Product>
<Product-subject-title>
<subject>IN BRIEF</subject>
</Product-subject-title>
<category>
<code type="pub">e00003</code>
<title>test6</title>
<ranking>117</ranking>
</category>
<category>
<span>
<code type="pub">e00004</code>
</span>
<title>test3</title>
<ranking>52</ranking>
</category>
</Product>
<Product>
<Product-subject-title>
<subject>BIOLOGY ARTICLE</subject>
</Product-subject-title>
<category>
<code type="pub">e00005</code>
<title>test28</title>
<ranking>10</ranking>
</category>
</Product>
<Product>
<Product-subject-title>
<subject>IN BRIEF</subject>
</Product-subject-title>
<category>
<code type="pub">e00006</code>
<title>test4</title>
<ranking>116</ranking>
</category>
</Product>
<category>
<span>
<code type="pub">e00007</code>
</span>
<title>test76</title>
<ranking>14</ranking>
</category>
</root>

Réponses:

0 pour la réponse № 1

Essayez xml linq. J'ai ajouté une balise Root pour permettre au tri de fonctionner

Option 1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication48
{
class Program
{
const string FILENAME = @"c:temptest.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);

XElement root = doc.Elements("Root").FirstOrDefault();

List<XElement> descendants = root.Elements().ToList();
descendants = descendants.OrderBy(x => (string)x.Descendants("code").FirstOrDefault()).ToList();

root.ReplaceNodes(descendants);
}
}
}

Option 2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication48
{
class Program
{
const string FILENAME = @"c:temptest.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);

XElement root = doc.Elements("root").FirstOrDefault();

List<XElement> products = root.Elements("Product").ToList();
for (int i = products.Count() - 1; i >= 0; i--)
{
XElement product = products[i];
List < XElement > newProducts = new List<XElement>();
if (product.Elements("category").Count() > 1)
{
foreach (XElement category in product.Elements("category"))
{
XElement newProduct = new XElement("Product", category);
newProducts.Add(newProduct);
}
product.ReplaceWith(newProducts);
}
}

List<XElement> descendants = root.Elements().ToList();
descendants = descendants.OrderBy(x => (string)x.Descendants("code").FirstOrDefault()).ToList();

root.ReplaceNodes(descendants);
}
}
}