【发布时间】:2020-05-12 13:36:33
【问题描述】:
我阅读了有关 Linq 和 xml 的信息,但我不知道我是否以正确的方式...
这里是我的 XML 配置文件:
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<Priorities>
<Priority Index="0">Zero</Priority>
<Priority Index="1">One</Priority>
<Priority Index="2">Two</Priority>
<Settings>
<Name>PriorityName</Name>
<DisplayName>PriorityDisplayName</DisplayName>
<Active>true</Active>
<Index>0</Index>
</Settings>
</Priorities>
<Workcenters>
<Workcenter Index="0">TEAM_0</Workcenter>
<Workcenter Index="1">TEAM_1</Workcenter>
<Workcenter Index="2">TEAM_2</Workcenter>
<Settings>
<Name>WorkcentersName</Name>
<DisplayName>WorkcentersDisplayName</DisplayName>
<Active>false</Active>
<Index>1</Index>
</Settings>
</Workcenters>
<ErrorPages>
<ErrorPage>
<Name>Error_1</Name>
<Type>Critical</Type>
</ErrorPage>
<ErrorPage>
<Name>Error_2</Name>
<Type>Normal</Type>
</ErrorPage>
</ErrorPages>
</Configuration>
现在我像这样访问所有优先级:
//Get Prios
XElement xelement = XElement.Load(xmlPath2);
IEnumerable<XElement> priorities = xelement.Descendants("Priorities");
var prios = priorities.Elements("Priority").ToList();
var settings = priorities.Elements("Settings");
foreach (XElement priority in prios)
{
Console.WriteLine(priority.Attribute("Index").Value);
Console.WriteLine(priority.Value);
}
foreach (XElement setting in settings)
{
Console.WriteLine(setting.Element("Name").Value);
Console.WriteLine(setting.Element("Active").Value);
Console.WriteLine(setting.Element("Index").Value);
Console.WriteLine(setting.Element("DisplayName").Value);
}
这是访问元素的正确方法吗? 我认为会有更好的阅读解决方案。
你能帮帮我吗?
谢谢
【问题讨论】: