我正在尝试找出获取两个元素(城市和州)值的最佳方法,以便以后使用它们。我正在遍历站点下的每个站点。每次我浏览它时,我都需要拉出 City 和 State 然后将它们保存为字符串。我让它工作(有点),但我认为我正在以错误的方式解决这个问题。
XML:
<Sites>
<Site>
<City>Miami</City>
<State>FL</State>
<Machines>
<Machine>
<MachineName>1950-16-CORE</MachineName>
<ServerRoleType>CoreServer</ServerRoleType>
<ClientRoles>Core</ClientRoles>
<Features>
<Feature>
<FeatureName>CoreProcess</FeatureName>
<FeatureEnabled>true</FeatureEnabled>
</Feature>
<Feature>
<FeatureName>Antivirus</FeatureName>
<FeatureEnabled>true</FeatureEnabled>
</Feature>
</Machine>
<Machine>
<MachineName>1950-16-COREX</MachineName>
<ServerRoleType>CoreExpansionServer</ServerRoleType>
<ClientRoles>CoreEx</ClientRoles>
<Features>
<Feature>
代码:
foreach (XElement xEleSite in siteRows)
{
IEnumerable<XElement> siteLists = from siteList in xEleSite.Descendants("Site")
select siteList;
IEnumerable<XElement> siteCity = siteLists.Descendants("City");
foreach (XElement c in siteCity)
{
string cityElement = c.Value.ToString();
}
}