我需要在不知道标签名称的情况下读取 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 中标签的名称。如果有人告诉我如何在不知道标签名称的情况下阅读标签并为我提供一种有效的方法,我将不胜感激。