Можно ли XML парсить циклом foreach не с начала в конец, а с конца в начало? - C#
Формулировка задачи:
Друзья всем привет ! возникла такая проблема вот хмл документ
Нужно сначала добраться д
<data> <id>1</id> <cityName>Бутырка</cityName> <!-- no data in 'description' --> <sympathyCitizens>309</sympathyCitizens> <darkSympathyCitizens>0</darkSympathyCitizens> <mastery>2</mastery> <darkMastery>1</darkMastery> <muddies> <simple> <!-- no data in 'id' --> <!-- no data in 'enemy' --> </simple> <rare> <!-- no data in 'id' --> <!-- no data in 'enemy' --> </rare> </muddies> <rewards> <reward repeated="1" type="money" slot="1">200</reward> <reward repeated="1" type="rating" slot="2">100</reward> <reward repeated="1" type="love" slot="3">100</reward> <reward repeated="1" type="guildAP" slot="4">190</reward> </rewards> <night_rewards> <reward repeated="1" type="sugar" slot="1">15</reward> <reward repeated="1" type="rating" slot="2">50</reward> <reward repeated="1" type="darklove" slot="3">50</reward> <reward repeated="1" type="guildAP" slot="4">340</reward> </night_rewards> <buildings> <build> <id>1</id> <x>180</x> <y>90</y> <status>2</status> <timeToCollectProfits>0</timeToCollectProfits> <level>2</level> <price>180</price> <currency>sigs</currency> <maxLevel>8</maxLevel> <currentProfit>10</currentProfit> <futureProfit>15</futureProfit> <typeOfBuild>2</typeOfBuild> <typeOfProfit>1</typeOfProfit> <nameOfBuild>Барыга</nameOfBuild> </build> <build> <id>2</id> <x>350</x> <y>150</y> <status>2</status> <timeToCollectProfits>0</timeToCollectProfits> <level>2</level> <price>180</price> <currency>sigs</currency> <maxLevel>8</maxLevel> <currentProfit>23</currentProfit> <futureProfit>41</futureProfit> <typeOfBuild>1</typeOfBuild> <typeOfProfit>3</typeOfProfit> <nameOfBuild>Лепник</nameOfBuild> </build> <build> <id>3</id> <x>360</x> <y>260</y> <status>2</status> <timeToCollectProfits>0</timeToCollectProfits> <level>2</level> <price>180</price> <currency>sigs</currency> <maxLevel>8</maxLevel> <currentProfit>10</currentProfit> <futureProfit>15</futureProfit> <typeOfBuild>3</typeOfBuild> <typeOfProfit>2</typeOfProfit> <nameOfBuild>Кольщик</nameOfBuild> </build> </buildings> <missions> <mission> <id>1</id> <x>130</x> <y>260</y> <missionText>Сделать фигурку из мякиша</missionText> <allTick>5</allTick> <completeTick>5</completeTick> <necessaryEnergy>1</necessaryEnergy> <requirements> <!-- no data in 'value' --> </requirements> <rewards> <reward repeated="2" type="money" slot="3">10</reward> <reward repeated="1" type="love" slot="2">1</reward> </rewards> </mission> <mission> <id>2</id> <x>120</x> <y>180</y> <missionText>Барыжить зубными щетками</missionText> <allTick>5</allTick> <completeTick>3</completeTick> <necessaryEnergy>3</necessaryEnergy> <requirements> <requirement type="build" slot="1">1</requirement> </requirements> <rewards> <reward repeated="2" type="money" slot="3">20</reward> <reward repeated="1" type="rating" slot="1">2</reward> <reward repeated="1" type="love" slot="2">3</reward> </rewards> </mission> <mission> <id>3</id> <x>280</x> <y>220</y> <missionText>Подогреть пацанов шахматами из мякиша</missionText> <allTick>5</allTick> <completeTick>0</completeTick> <necessaryEnergy>4</necessaryEnergy> <requirements> <requirement type="build" slot="1">2</requirement> </requirements> <rewards> <reward repeated="1" type="rating" slot="1">2</reward> <reward repeated="1" type="love" slot="2">4</reward> </rewards> </mission> <mission> <id>4</id> <x>290</x> <y>130</y> <missionText>Прессануть новичков на прогулке</missionText> <allTick>5</allTick> <completeTick>0</completeTick> <necessaryEnergy>5</necessaryEnergy> <requirements> <!-- no data in 'value' --> </requirements> <rewards> <reward repeated="2" type="money" slot="3">10</reward> <reward repeated="1" type="rating" slot="1">2</reward> <reward repeated="1" type="love" slot="2">5</reward> </rewards> </mission> <mission> <id>5</id> <x>460</x> <y>200</y> <missionText>Набить наколки сокамерникам</missionText> <allTick>5</allTick> <completeTick>0</completeTick> <necessaryEnergy>7</necessaryEnergy> <requirements> <requirement type="build" slot="1">3</requirement> </requirements> <rewards> <reward repeated="1" type="rating" slot="1">3</reward> <reward repeated="1" type="love" slot="3">7</reward> </rewards> </mission> <mission> <id>6</id> <x>480</x> <y>320</y> <missionText>Опустить беспредельщиков</missionText> <allTick>5</allTick> <completeTick>0</completeTick> <necessaryEnergy>8</necessaryEnergy> <requirements> <!-- no data in 'value' --> </requirements> <rewards> <reward repeated="1" type="rating" slot="1">3</reward> <reward repeated="1" type="love" slot="3">8</reward> </rewards> </mission> <mission> <id>7</id> <x>210</x> <y>280</y> <missionText>Подмять зону под себя</missionText> <allTick>5</allTick> <completeTick>0</completeTick> <necessaryEnergy>10</necessaryEnergy> <requirements> <requirement type="last" slot="0">0</requirement> </requirements> <rewards> <reward repeated="2" type="money" slot="3">10</reward> <reward repeated="1" type="rating" slot="1">3</reward> <reward repeated="1" type="love" slot="2">10</reward> </rewards> </mission> <mission> <id>8</id> <x>130</x> <y>260</y> <missionText>Кинуть в надзирателя бычек</missionText> <allTick>5</allTick> <completeTick>0</completeTick> <necessaryEnergy>5</necessaryEnergy> <requirements> <!-- no data in 'value' --> </requirements> <rewards> <reward repeated="1" type="darklove" slot="3">1</reward> </rewards> </mission> <mission> <id>9</id> <x>120</x> <y>180</y> <missionText>Поджечь мусор</missionText> <allTick>7</allTick> <completeTick>0</completeTick> <necessaryEnergy>5</necessaryEnergy> <requirements> <!-- no data in 'value' --> </requirements> <rewards> <reward repeated="1" type="rating" slot="1">2</reward> <reward repeated="1" type="darklove" slot="3">3</reward> </rewards> </mission> <mission> <id>10</id> <x>280</x> <y>220</y> <missionText>Спровоцировать приезд проверки</missionText> <allTick>7</allTick> <completeTick>0</completeTick> <necessaryEnergy>4</necessaryEnergy> <requirements> <!-- no data in 'value' --> </requirements> <rewards> <reward repeated="1" type="rating" slot="1">2</reward> <reward repeated="1" type="darklove" slot="3">4</reward> </rewards> </mission> <mission> <id>11</id> <x>290</x> <y>130</y> <missionText>Одеть форму охраны на опущенных</missionText> <allTick>7</allTick> <completeTick>0</completeTick> <necessaryEnergy>5</necessaryEnergy> <requirements> <!-- no data in 'value' --> </requirements> <rewards> <reward repeated="1" type="rating" slot="1">2</reward> <reward repeated="1" type="darklove" slot="3">5</reward> </rewards> </mission> <mission> <id>12</id> <x>460</x> <y>200</y> <missionText>Украсть на кухне хавки</missionText> <allTick>8</allTick> <completeTick>0</completeTick> <necessaryEnergy>7</necessaryEnergy> <requirements> <!-- no data in 'value' --> </requirements> <rewards> <reward repeated="1" type="rating" slot="1">3</reward> <reward repeated="1" type="darklove" slot="3">7</reward> </rewards> </mission> <mission> <id>13</id> <x>480</x> <y>320</y> <missionText>Перекрыть в зоне воду</missionText> <allTick>8</allTick> <completeTick>0</completeTick> <necessaryEnergy>8</necessaryEnergy> <requirements> <!-- no data in 'value' --> </requirements> <rewards> <reward repeated="1" type="rating" slot="1">3</reward> <reward repeated="1" type="darklove" slot="3">8</reward> </rewards> </mission> <mission> <id>14</id> <x>210</x> <y>280</y> <missionText>Устроить бунт</missionText> <allTick>10</allTick> <completeTick>0</completeTick> <necessaryEnergy>10</necessaryEnergy> <requirements> <requirement type="darklast" slot="0">0</requirement> </requirements> <rewards> <reward repeated="1" type="rating" slot="1">3</reward> <reward repeated="1" type="darklove" slot="3">10</reward> </rewards> </mission> </missions> </data>
Решение задачи: «Можно ли XML парсить циклом foreach не с начала в конец, а с конца в начало?»
textual
Листинг программы
using System; using System.Linq; using System.Xml.Linq; using System.Xml.XPath; internal class Program { private static void Main(string[] args) { var str = @"<data> <id>1</id> <cityName>Бутырка</cityName> <!-- no data in 'description' --> <sympathyCitizens>309</sympathyCitizens> <darkSympathyCitizens>0</darkSympathyCitizens> <mastery>2</mastery> <darkMastery>1</darkMastery> <muddies> <simple> <!-- no data in 'id' --> <!-- no data in 'enemy' --> </simple> <rare> <!-- no data in 'id' --> <!-- no data in 'enemy' --> </rare> </muddies> <rewards> <reward repeated='1' type='money' slot='1'>200</reward> <reward repeated='1' type='rating' slot='2'>100</reward> <reward repeated='1' type='love' slot='3'>100</reward> <reward repeated='1' type='guildAP' slot='4'>190</reward> </rewards> <night_rewards> <reward repeated='1' type='sugar' slot='1'>15</reward> <reward repeated='1' type='rating' slot='2'>50</reward> <reward repeated='1' type='darklove' slot='3'>50</reward> <reward repeated='1' type='guildAP' slot='4'>340</reward> </night_rewards> <buildings> <build> <id>1</id> <x>180</x> <y>90</y> <status>2</status> <timeToCollectProfits>0</timeToCollectProfits> <level>2</level> <price>180</price> <currency>sigs</currency> <maxLevel>8</maxLevel> <currentProfit>10</currentProfit> <futureProfit>15</futureProfit> <typeOfBuild>2</typeOfBuild> <typeOfProfit>1</typeOfProfit> <nameOfBuild>Барыга</nameOfBuild> </build> <build> <id>2</id> <x>350</x> <y>150</y> <status>2</status> <timeToCollectProfits>0</timeToCollectProfits> <level>2</level> <price>180</price> <currency>sigs</currency> <maxLevel>8</maxLevel> <currentProfit>23</currentProfit> <futureProfit>41</futureProfit> <typeOfBuild>1</typeOfBuild> <typeOfProfit>3</typeOfProfit> <nameOfBuild>Лепник</nameOfBuild> </build> <build> <id>3</id> <x>360</x> <y>260</y> <status>2</status> <timeToCollectProfits>0</timeToCollectProfits> <level>2</level> <price>180</price> <currency>sigs</currency> <maxLevel>8</maxLevel> <currentProfit>10</currentProfit> <futureProfit>15</futureProfit> <typeOfBuild>3</typeOfBuild> <typeOfProfit>2</typeOfProfit> <nameOfBuild>Кольщик</nameOfBuild> </build> </buildings> <missions> <mission> <id>1</id> <x>130</x> <y>260</y> <missionText>Сделать фигурку из мякиша</missionText> <allTick>5</allTick> <completeTick>5</completeTick> <necessaryEnergy>1</necessaryEnergy> <requirements> <!-- no data in 'value' --> </requirements> <rewards> <reward repeated='2' type='money' slot='3'>10</reward> <reward repeated='1' type='love' slot='2'>1</reward> </rewards> </mission> <mission> <id>2</id> <x>120</x> <y>180</y> <missionText>Барыжить зубными щетками</missionText> <allTick>5</allTick> <completeTick>3</completeTick> <necessaryEnergy>3</necessaryEnergy> <requirements> <requirement type='build' slot='1'>1</requirement> </requirements> <rewards> <reward repeated='2' type='money' slot='3'>20</reward> <reward repeated='1' type='rating' slot='1'>2</reward> <reward repeated='1' type='love' slot='2'>3</reward> </rewards> </mission> <mission> <id>3</id> <x>280</x> <y>220</y> <missionText>Подогреть пацанов шахматами из мякиша</missionText> <allTick>5</allTick> <completeTick>0</completeTick> <necessaryEnergy>4</necessaryEnergy> <requirements> <requirement type='build' slot='1'>2</requirement> </requirements> <rewards> <reward repeated='1' type='rating' slot='1'>2</reward> <reward repeated='1' type='love' slot='2'>4</reward> </rewards> </mission> <mission> <id>4</id> <x>290</x> <y>130</y> <missionText>Прессануть новичков на прогулке</missionText> <allTick>5</allTick> <completeTick>0</completeTick> <necessaryEnergy>5</necessaryEnergy> <requirements> <!-- no data in 'value' --> </requirements> <rewards> <reward repeated='2' type='money' slot='3'>10</reward> <reward repeated='1' type='rating' slot='1'>2</reward> <reward repeated='1' type='love' slot='2'>5</reward> </rewards> </mission> <mission> <id>5</id> <x>460</x> <y>200</y> <missionText>Набить наколки сокамерникам</missionText> <allTick>5</allTick> <completeTick>0</completeTick> <necessaryEnergy>7</necessaryEnergy> <requirements> <requirement type='build' slot='1'>3</requirement> </requirements> <rewards> <reward repeated='1' type='rating' slot='1'>3</reward> <reward repeated='1' type='love' slot='3'>7</reward> </rewards> </mission> <mission> <id>6</id> <x>480</x> <y>320</y> <missionText>Опустить беспредельщиков</missionText> <allTick>5</allTick> <completeTick>0</completeTick> <necessaryEnergy>8</necessaryEnergy> <requirements> <!-- no data in 'value' --> </requirements> <rewards> <reward repeated='1' type='rating' slot='1'>3</reward> <reward repeated='1' type='love' slot='3'>8</reward> </rewards> </mission> <mission> <id>7</id> <x>210</x> <y>280</y> <missionText>Подмять зону под себя</missionText> <allTick>5</allTick> <completeTick>0</completeTick> <necessaryEnergy>10</necessaryEnergy> <requirements> <requirement type='last' slot='0'>0</requirement> </requirements> <rewards> <reward repeated='2' type='money' slot='3'>10</reward> <reward repeated='1' type='rating' slot='1'>3</reward> <reward repeated='1' type='love' slot='2'>10</reward> </rewards> </mission> <mission> <id>8</id> <x>130</x> <y>260</y> <missionText>Кинуть в надзирателя бычек</missionText> <allTick>5</allTick> <completeTick>0</completeTick> <necessaryEnergy>5</necessaryEnergy> <requirements> <!-- no data in 'value' --> </requirements> <rewards> <reward repeated='1' type='darklove' slot='3'>1</reward> </rewards> </mission> <mission> <id>9</id> <x>120</x> <y>180</y> <missionText>Поджечь мусор</missionText> <allTick>7</allTick> <completeTick>0</completeTick> <necessaryEnergy>5</necessaryEnergy> <requirements> <!-- no data in 'value' --> </requirements> <rewards> <reward repeated='1' type='rating' slot='1'>2</reward> <reward repeated='1' type='darklove' slot='3'>3</reward> </rewards> </mission> <mission> <id>10</id> <x>280</x> <y>220</y> <missionText>Спровоцировать приезд проверки</missionText> <allTick>7</allTick> <completeTick>0</completeTick> <necessaryEnergy>4</necessaryEnergy> <requirements> <!-- no data in 'value' --> </requirements> <rewards> <reward repeated='1' type='rating' slot='1'>2</reward> <reward repeated='1' type='darklove' slot='3'>4</reward> </rewards> </mission> <mission> <id>11</id> <x>290</x> <y>130</y> <missionText>Одеть форму охраны на опущенных</missionText> <allTick>7</allTick> <completeTick>0</completeTick> <necessaryEnergy>5</necessaryEnergy> <requirements> <!-- no data in 'value' --> </requirements> <rewards> <reward repeated='1' type='rating' slot='1'>2</reward> <reward repeated='1' type='darklove' slot='3'>5</reward> </rewards> </mission> <mission> <id>12</id> <x>460</x> <y>200</y> <missionText>Украсть на кухне хавки</missionText> <allTick>8</allTick> <completeTick>0</completeTick> <necessaryEnergy>7</necessaryEnergy> <requirements> <!-- no data in 'value' --> </requirements> <rewards> <reward repeated='1' type='rating' slot='1'>3</reward> <reward repeated='1' type='darklove' slot='3'>7</reward> </rewards> </mission> <mission> <id>13</id> <x>480</x> <y>320</y> <missionText>Перекрыть в зоне воду</missionText> <allTick>8</allTick> <completeTick>0</completeTick> <necessaryEnergy>8</necessaryEnergy> <requirements> <!-- no data in 'value' --> </requirements> <rewards> <reward repeated='1' type='rating' slot='1'>3</reward> <reward repeated='1' type='darklove' slot='3'>8</reward> </rewards> </mission> <mission> <id>14</id> <x>210</x> <y>280</y> <missionText>Устроить бунт</missionText> <allTick>10</allTick> <completeTick>0</completeTick> <necessaryEnergy>10</necessaryEnergy> <requirements> <requirement type='darklast' slot='0'>0</requirement> </requirements> <rewards> <reward repeated='1' type='rating' slot='1'>3</reward> <reward repeated='1' type='darklove' slot='3'>10</reward> </rewards> </mission> </missions> </data>"; var doc = XDocument.Parse(str); var id8 = doc.Document.Root.XPathSelectElement("//mission[id=8]"); var last = doc.Document.Root.XPathSelectElement("//mission[last()]"); var q = doc.Document.Root.Descendants("mission").Reverse().TakeWhile(n => int.Parse(n.Element("id").Value) >= 8); foreach (var elemet in q) { Console.WriteLine(elemet); Console.WriteLine(); } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д