C# 在不知道元素顺序的情况下自上而下解析XML?

C# 在不知道元素顺序的情况下自上而下解析XML?,c#,xml,visual-studio-2012,C#,Xml,Visual Studio 2012,我正试图将XML解析为我正在开发的配置文件系统的可运行C#代码。以下是XML代码: <?xml version="1.0" encoding="utf-8"?> <MCBuddy> <ProfileName>Profile Test 2</ProfileName> <MCScript> <Log>HELLO</Log> <Wait>100</Wai

我正试图将XML解析为我正在开发的配置文件系统的可运行C#代码。以下是XML代码:

<?xml version="1.0" encoding="utf-8"?>
<MCBuddy>
    <ProfileName>Profile Test 2</ProfileName>
    <MCScript>
        <Log>HELLO</Log>
        <Wait>100</Wait>
        <Status>Walking forward...</Status>
        <CameraMove X="-10" Y="-10" />
        <CameraMove X="-20" Y="10" />
        <CameraMove X="6" Y="15" />
        <CameraMove X="-10" Y="-10" />
        <CameraMove X="-20" Y="10" />
        <CameraMove X="6" Y="15" />
        <CameraMove X="-10" Y="-10" />
        <PressKey>D1</PressKey>
        <CameraMove X="-20" Y="10" />
        <CameraMove X="6" Y="15" />
        <CameraMove X="-10" Y="-10" />
        <CameraMove X="-20" Y="10" />
        <CameraMove X="6" Y="15" />
        <MoveLeft>5000</MoveLeft>
        <MoveForward>1000</MoveForward>
        <EndProfile />
    </MCScript>
</MCBuddy>

剖面测试2
你好
100
向前走。。。
D1
5000
1000
C#代码:

while(时间n.Name==“MoveRight”))
{
PlayerActions.MoveRight(转换为32(mrRight.Value));
}
foreach(doc.subjects()中的var mforward,其中(n=>n.Name==“MoveForward”))
{
PlayerActions.MoveForward(转换为32(mforward.Value));
}
foreach(doc.subjects()中的var mback,其中(n=>n.Name==“MoveBack”))
{
PlayerActions.MoveBack(转换为32(mback.Value));
}
foreach(doc.subscriptions()中的var mleft,其中(n=>n.Name==“MoveLeft”))
{
PlayerActions.MoveLeft(转换为32(mleft.Value));
}
foreach(var wait in doc.subjections()。其中(n=>n.Name==“wait”))
{
Task.Delay(Convert.ToInt32(wait.Value)).wait();
}
foreach(doc.subjects()中的var log,其中(n=>n.Name==“log”))
{
Function.Log(Log.Value,1,mb_日志框);
}
foreach(doc.subjects()中的变量状态,其中(n=>n.Name==“Status”))
{
mb_statusLabel.Text=state.Value;
}
foreach(doc.subjects()中的var键,其中(n=>n.Name==“PressKey”))
{
按key((Keys)Enum.Parse(typeof(Keys),key.Value));
}
foreach(var msLeftClick in doc.subscriptions()。其中(n=>n.Name==“MouseLeftClick”))
{
PlayerActions.MouseLeftClick();
}
foreach(var msRightClick in doc.subjects()。其中(n=>n.Name==“MouseRightClick”))
{
PlayerActions.MouseRightClick();
}
foreach(doc.subjects()中的变量msLeftHold,其中(n=>n.Name==“MouseLeftHold”))
{
PlayerActions.MouseLeftHold(转换为32(msLeftHold.Value));
}
foreach(doc.subjects()中的var msRightHold,其中(n=>n.Name==“MouseRightHold”))
{
PlayerActions.MouseRightHold(转换为32(msRightHold.Value));
}
foreach(var camMove in doc.subscriptions()。其中(n=>n.Name==“CameraMove”))
{
点移动=新点(Cursor.Position.X+Convert.ToInt32(camMove.Attribute(“X”).Value),Cursor.Position.Y+Convert.ToInt32(camMove.Attribute(“Y”).Value));
TimeSpan moveTime=新的TimeSpan(0,0,0,1);
线性平滑移动(移动,移动时间);
}
}
然而,我遇到了一个问题,即无论它以什么XML元素开始,都无法找到一种方法来解析它。如果我保持现在的方式,它将按照这个顺序解析它。我尝试过多种方法,但都不管用。有什么想法吗?

您可以将其转换为查找


您可以在MCScript下迭代元素,并根据元素的名称执行适当的操作:

        long time=0;
        foreach (var node in xd.Document
            .Element("MCBuddy")
            .Element("MCScript")
            .Elements())
        {
            if (time > mb_runningTime.Value)
            {
                 break;
            }
            time++;
            var ele = (XElement) node;
            switch (ele.Name.ToString())
            {
                case "Status":
                    mb_statusLabel.Text = ele.Value;
                    break;
                case "CameraMove":
                    var move = new Point(
                        Cursor.Position.X + Convert.ToInt32(ele.Attribute("X").Value),
                        Cursor.Position.Y + Convert.ToInt32(ele.Attribute("Y").Value));
                    TimeSpan moveTime = new TimeSpan(0, 0, 0, 1);
                    LinearSmoothMove(move, moveTime);
                break;
                    // others go here
                default:
                    Trace.WriteLine(ele.Name);
                    break;
            }
        }
var xDoc = XDocument.Parse(xmlstring);
var lookup = xDoc.Descendants("MCScript")
                .First()
                .Elements()
                .ToLookup(x => x.Name.LocalName);
        long time=0;
        foreach (var node in xd.Document
            .Element("MCBuddy")
            .Element("MCScript")
            .Elements())
        {
            if (time > mb_runningTime.Value)
            {
                 break;
            }
            time++;
            var ele = (XElement) node;
            switch (ele.Name.ToString())
            {
                case "Status":
                    mb_statusLabel.Text = ele.Value;
                    break;
                case "CameraMove":
                    var move = new Point(
                        Cursor.Position.X + Convert.ToInt32(ele.Attribute("X").Value),
                        Cursor.Position.Y + Convert.ToInt32(ele.Attribute("Y").Value));
                    TimeSpan moveTime = new TimeSpan(0, 0, 0, 1);
                    LinearSmoothMove(move, moveTime);
                break;
                    // others go here
                default:
                    Trace.WriteLine(ele.Name);
                    break;
            }
        }