1

我需要在不知道标签名称的情况下读取 XML 文件。目前我正在使用 Xelement。我需要找到一种有效的方法来制作这个程序。我的 XML:

<Actions>
<Action Name="Action1" ID="AE6ECD81-6CC1-4A05-B379-113D700BD1A9">
    <Trigger Name="alo">
        <And>
            <Checked ID="3897D8C6-873F-45AD-8D28-1A80B8AC5CBD" Parameter="Attribute" Value="Checked"/>
            <Checked ID="B1BA57CD-BFAF-4C52-8686-F7073F68EF71" Parameter="Attribute" Value="unchecked"/>
        </And>
    </Trigger>
    <Command>
        <True>
            <Do ID="F9129376-DCDE-4964-8B5C-DF0EF8886AB9" Parameter="Visible" Value="True"/>
            <Do ID="456627B0-8195-459C-981F-7450866CE635" Parameter="Visible" Value="True"/>
        </True>
    </Command>
</Action>
<Action Name="Action2" ID="5E7809FF-FEAB-48F2-9F18-0E67F514AFB2">
    <Trigger>
        <Or>
            <Checked ID="3897D8C6-873F-45AD-8D28-1A80B8AC5CBD" Parameter="Attribute" Value="Checked"/>
            <Checked ID="B1BA57CD-BFAF-4C52-8686-F7073F68EF71" Parameter="Attribute" Value="unchecked"/>
        </Or>
    </Trigger>
    <Command>
        <True>
            <Do ID="F9129376-DCDE-4964-8B5C-DF0EF8886AB9" Parameter="Visible" Value="True"/>
            <Do ID="456627B0-8195-459C-981F-7450866CE635" Parameter="Visible" Value="True"/>
        </True>
    </Command>
</Action>

我的 C# 代码:

    public override void LoadFromXml(string XmlFileAddress)
    {
        XDocument doc = XDocument.Load(XmlFileAddress);
        var actions = doc.Descendants("Action").ToList();
        foreach (var a in actions)
        {
            xmlstring = a.ToString();
            Atcion hta = new Atcion();
            hta.LoadFromXml(xmlstring);
            ActionList.Add(hta);
         }
     }

    List<Trigger> TriggerList = new List<Trigger>();
    List<Command> CommandList = new List<Command>();
    string actname;
    string actID;
    string trigname;
    string actype;
    public override void LoadFromXml(string XmlFileAddress)
    {
        string xmlstr = "";
        XElement elem = XElement.Parse(XmlFileAddress);            
        actID = elem.Attribute("ID").Value;
        actname = elem.Attribute("Name").Value;
        var triggers = elem.Descendants("Trigger").ToList();                        
        foreach (var t in triggers)
        {
            trigname = t.Attribute("Name").Value;
            actype = t.Element("And").Name.LocalName;
            var chaction = t.Descendants("And").ToList();                
            foreach (var c in chaction)
            {
                Trigger tri = new Trigger();
                xmlstr = c.ToString();
                tri.LoadFromXml(xmlstr);
                TriggerList.Add(tri);
            }
        }

我唯一的问题是我需要知道 XML 中标签的名称。如果有人告诉我如何在不知道标签名称的情况下阅读标签并为我提供一种有效的方法,我将不胜感激。

4

2 回答 2

1

我不知道在不知道语义的情况下您将如何解释 xml,但以下方法在不知道元素的情况下遍历整个文档。

public void ProcessXml(string XmlFileAddress)
{
    XDocument doc = XDocument.Load(XmlFileAddress);

    ReadXElement(doc.Root);
}

private void ReadXElement(XElement xElement)
{
    foreach (var attribute in xElement.Attributes())
    {
        Console.WriteLine(attribute.Name + " - " + attribute.Value);
    }

    foreach (var childElement in xElement.Elements())
    {
        ReadXElement(childElement);
    }
}

结果是:

在此处输入图像描述

于 2013-05-14T07:02:54.947 回答
1

我假设您具有 的先验知识ActionTrigger并且Command您希望动态解析这些操作。

var xDoc = XDocument.Load(filename);

var actions = xDoc.Descendants("Action")
                    .Select(action => new
                    {
                        Name = action.Attribute("Name").Value,
                        Trigger = new
                        {
                            Name = (string)action.Element("Trigger").Attribute("Name"),
                            Conditions = action.Element("Trigger")
                                            .Elements().First()
                                            .Elements()
                                            .Select(e => new{
                                                Name = e.Parent.Name.LocalName,
                                                Attributes = e.Attributes().ToDictionary(a=>a.Name.LocalName,a=>a.Value)
                                            })
                                            .ToList()

                        },
                        Command = new
                        {
                            Name = (string)action.Element("Trigger").Attribute("Name"),
                            Do = action.Element("Command")
                                       .Elements().First()
                                       .Elements()
                                       .Select(e => new
                                        {
                                            Name = e.Parent.Name.LocalName,
                                            Attributes = e.Attributes().ToDictionary(a => a.Name.LocalName, a => a.Value)
                                         })
                                        .ToList()

                        },
                    })
                    .ToList();
于 2013-05-14T07:23:30.020 回答