我有一个要解析的XML文件。它比我将在此处发布的内容更长,但是只有这一部分使我对这个问题感兴趣:
<Team>
<TeamId>1187457</TeamId>
<TeamName>Zanardi Redwings</TeamName>
<Arena>
<ArenaId>1184019</ArenaId>
<ArenaName>Evolution</ArenaName>
</Arena>
<League>
<LeagueId>37</LeagueId>
<LeagueName>România</LeagueName>
</League>
<Country>
<CountryId>36</CountryId>
<CountryName>România</CountryName>
</Country>
<LeagueLevelUnit>
<LeagueLevelUnitId>4109</LeagueLevelUnitId>
<LeagueLevelUnitName>V.171</LeagueLevelUnitName>
</LeagueLevelUnit>
<Region>
<RegionId>799</RegionId>
<RegionName>Prahova</RegionName>
</Region>
<YouthTeam>
<YouthTeamId>2337461</YouthTeamId>
<YouthTeamName>Little Redwings</YouthTeamName>
<YouthLeague>
<YouthLeagueId>436902</YouthLeagueId>
<YouthLeagueName>Normandie Ligue des jeunes</YouthLeagueName>
</YouthLeague>
</YouthTeam>
</Team>
在上面的部分中,我只需要从TeamId
和TeamName
子节点读取数据。为此,我编写了以下代码:
Nodes = Node.SelectNodes("Team");
foreach (XmlNode j in Nodes)
{
XmlNodeList TeamDetails = j.SelectNodes("*");
foreach (XmlNode k in TeamDetails)
{
switch (k.Name)
{
case "TeamName":
{
UserTeamNames[counter] = k.InnerXml;
break;
}
case "TeamId":
{
if (!int.TryParse(k.InnerXml, out UserTeamIDs[counter]))
{
ShowErrorMessageBox("Parsing TeamID from XML file failed!"); //A function which sets some parameters for MessageBox.Show() then calls it
}
break;
}
}
}
}
在上面的代码中,counter
是一个int
变量,我在代码的其他部分中需要。
代码运行完美,但是当我正在处理的节点不是TeamName
或TeamID
时,我想消除无用的循环和测试。
我怀疑我要寻找的答案与XPath表达式有关,但我不确定。
如何仅从 个节点读取数据,而没有任何无用的操作?
答案 0 :(得分:1)
您可以使用XmlDocument.GetElementsByTagName方法。 对于您的示例,我将执行以下操作:
XmlNodeList elemList = doc.GetElementsByTagName("TeamName");
XmlNodeList elemList = doc.GetElementsByTagName("TeamId");
结果是一个XmlNodeList,其中包含所有匹配节点的列表。如果没有节点与名称匹配,则返回的集合将为空。
答案 1 :(得分:1)
严格按照xpath,以下表达式应选择您的节点:
/Team/*[self::TeamId or self::TeamName]
答案 2 :(得分:1)
您可以使用linq过滤xml文档,然后使用过滤后的数据。
using System;
using System.Linq;
using System.Xml.Linq;
using System.IO;
using System.Xml;
static void Main(string[] args)
{
var document = new XDocument();
// use path to your xml file
using (FileStream fs = File.OpenRead("examp.xml"))
{
using (XmlTextReader reader = new XmlTextReader(fs))
{
document = XDocument.Load(reader);
}
}
int value;
var query = (from element in document.Element("Base").Elements("Team")
where int.TryParse(element.Element("TeamId").Value.ToString(), out value)
select new
{
TeamName = element.Element("TeamName").Value,
TeamId = element.Element("TeamId").Value
}).ToList();
// do further processing with filtered data
foreach (var item in query)
{
Console.WriteLine($"{item.TeamName}: {item.TeamId}");
}
Console.ReadKey();
}
答案 3 :(得分:1)
这是使用LINQ to XML的另一种选择,首先创建您自己的类,该类将保存您要查找的数据,如下所示:
public class TeamInfo
{
public string TeamName { get; set; }
public int TeamId { get; set; }
}
然后,您将xml解析为这样的对象列表:
var data =
"<Team><TeamId>1187457</TeamId><TeamName>Zanardi Redwings</TeamName><Arena><ArenaId>1184019</ArenaId><ArenaName>Evolution</ArenaName></Arena><League><LeagueId>37</LeagueId><LeagueName>România</LeagueName></League><Country><CountryId>36</CountryId><CountryName>România</CountryName></Country><LeagueLevelUnit><LeagueLevelUnitId>4109</LeagueLevelUnitId><LeagueLevelUnitName>V.171</LeagueLevelUnitName></LeagueLevelUnit><Region><RegionId>799</RegionId><RegionName>Prahova</RegionName></Region><YouthTeam><YouthTeamId>2337461</YouthTeamId><YouthTeamName>Little Redwings</YouthTeamName><YouthLeague><YouthLeagueId>436902</YouthLeagueId><YouthLeagueName>Normandie Ligue des jeunes</YouthLeagueName></YouthLeague></YouthTeam></Team>";
var elm = new XElement("Base",data);
var decoded = System.Web.HttpUtility.HtmlDecode(elm.ToString());//this is to remove any formatting issues when we call .ToString()
var doc = XDocument.Parse(decoded);
var result = doc.Root.Descendants("Team")
.Select(y => new TeamInfo
{
TeamId = Convert.ToInt32(y.Element("TeamId").Value),
TeamName = y.Element("TeamName").Value
}).ToList();
}