我需要在不知道标签名称的情况下读取XML文件。 目前我正在使用Xelement.I需要找到一个有效的方法来制作这个程序。 我的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中标签的名称。 如果有人告诉我如何在不知道姓名的情况下阅读标签并为我提供一种有效的方式,我将非常感激。
答案 0 :(得分: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);
}
}
结果是:
答案 1 :(得分:1)
我假设您具有Action
,Trigger
和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();