晚上好, 我有一个这样的XML模板:
<Pedestrian Name='Kid'>
<Initial_Position In_X='2' In_Y='2' />
<Final_Position Fin_X='3' Fin_Y='3' Time='10' />
<Final_Position Fin_X='4' Fin_Y='4' Time='12' />
</Pedestrian>
<Pedestrian Name='Mother'>
<Initial_Position In_X='3' In_Y='3' />
<Final_Position Fin_X='3' Fin_Y='3' Time='10' />
</Pedestrian>
其中“Final_Position”出现在随机数的时间内。
我对代码的想法如下:
foreach (XElement pedestrian in doc.Root.Elements("Pedestrian"))
{
XElement initialPosition = pedestrian.Element("Initial_Position");
name = (string)pedestrian.Attribute("Name");
X1 = (int)initialPosition.Attribute("In_X");
Y1 = (int)initialPosition.Attribute("In_Y");
MessageBox.Show(name);
int i = 0;
int x = doc.Root.Elements("Pedestrian").Elements("Final_Position").Count();// nodeList.Count();
MessageBox.Show("x = " +x.ToString());
int[] X_fin = new int[x];
int[] Y_fin = new int[x];
int[] Time_fin = new int[x];
foreach (XElement finpos in doc.Root.Elements("Pedestrian").Elements("Final_Position"))
{
X_fin[i] = (int)finpos.Attribute("Fin_X");
Y_fin[i] = (int)finpos.Attribute("Fin_Y");
Time_fin[i] = (int)finpos.Attribute("Time");
i++;
}
}
为了能够在最后创建一个对象数组 Pedestrian ,它取名,Initial_Position(X1,Y1)和Final_Position(X_Fin,Y_Fin,Time_Fin)。
问题在于,如果我放置一些MessageBox.Show()以了解程序是如何工作的,那么输出是这样的:
Kid
2 2
3 3 10 //
4 4 12 //
3 3 10 //final position of Kid + Mother
Mother
3 3
3 3 10 //
4 4 12 //
3 3 10 //
此外,数组X_Fin的大小为3(我文件中所有Final_Position的总和)。
我怎样才能纠正它,以便在 X_Fin 中获得给定pedetrian的所有位置?我的想法是在第二个 foreach
之后创建对象答案 0 :(得分:0)
试试这个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication97
{
class Program
{
static void Main(string[] args)
{
string xml =
"<Root>" +
"<Pedestrian Name='Kid'>" +
"<Initial_Position In_X='2' In_Y='2' />" +
"<Final_Position Fin_X='3' Fin_Y='3' Time='10' />" +
"<Final_Position Fin_X='4' Fin_Y='4' Time='12' />" +
"</Pedestrian>" +
"<Pedestrian Name='Mother'>" +
"<Initial_Position In_X='3' In_Y='3' />" +
"<Final_Position Fin_X='3' Fin_Y='3' Time='10' />" +
"</Pedestrian>" +
"</Root>";
XElement root = XElement.Parse(xml);
var results = root.Descendants("Pedestrian").Select(x => new
{
name = (string)x.Attribute("Name"),
initialPosition = x.Elements("Initial_Position")
.Select(y => new { x = (int)y.Attribute("In_X"), y = (int)y.Attribute("In_Y") }).FirstOrDefault(),
finalPosition = x.Elements("Final_Position")
.Select(y => new { x = (int)y.Attribute("Fin_X"), y = (int)y.Attribute("Fin_Y"), Time = (int)y.Attribute("Time")}).ToList()
}).ToList();
}
}
}
答案 1 :(得分:0)
让我们创建一个行人数组,其中包含InitialPosition.X
&amp; .Y
和FinalPositions
数组,每个数组都有X,Y和时间。
XElement root = XElement.Load(file); // or use your doc.Root instead
var peds = root.Elements("Pedestrian")
.Select(p => new
{
Name = (string)p.Attribute("Name"),
InitialPosition = new
{
X = (int)p.Element("Initial_Position").Attribute("In_X"),
Y = (int)p.Element("Initial_Position").Attribute("In_Y")
},
FinalPositions = p.Elements("Final_Position")
.Select(f => new
{
X = (int)f.Attribute("Fin_X"),
Y = (int)f.Attribute("Fin_Y"),
Time = (int)f.Attribute("Time")
}).ToArray()
}).ToArray();
现在让我们输出行人数组和内部FinalPositions。
int totalFinals = 0;
foreach (var pedestrian in peds)
{
Console.WriteLine(string.Format("{0} has {1} final positions.", pedestrian.Name, pedestrian.FinalPositions.Length));
Console.WriteLine(string.Format("{0} {1}", pedestrian.InitialPosition.X, pedestrian.InitialPosition.Y));
foreach (var final in pedestrian.FinalPositions)
Console.WriteLine(string.Format("{0} {1} {2}", final.X, final.Y, final.Time));
totalFinals += pedestrian.FinalPositions.Length;
}
Console.WriteLine(string.Format("Total final positions = {0}", totalFinals));
输出正如我所想的那样
Kid has 2 final positions.
2 2
3 3 10
4 4 12
Mother has 1 final positions.
3 3
3 3 10
Total final positions = 3