I have an XML file that I want to parse. It is longer than what I shall post here, but only this part interests me in this question:
<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>
From the part above, I only need to read the data from the TeamId
and TeamName
child nodes. To achieve this, I wrote the following code:
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;
}
}
}
}
In the code above, counter
is an int
variable, which I need in other part of the code.
The code works perfectly, but I want to eliminate the useless looping and testing when the node I am working on is not either TeamName
or TeamID
.
I suspect the answer I am looking for has something to do with XPath expressions, but I am not sure.
How can I read the data from only the mentioned nodes, without any useless operations?
You can use XmlDocument.GetElementsByTagName method. For your example I will do something like this :
XmlNodeList elemList = doc.GetElementsByTagName("TeamName");
XmlNodeList elemList = doc.GetElementsByTagName("TeamId");
The result is An XmlNodeList containing a list of all matching nodes. If no nodes match name, the returned collection will be empty.
严格按照xpath而言,以下表达式应选择您的节点:
/Team/*[self::TeamId or self::TeamName]
You can filter the xml document using linq then work with your filtered data.
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();
}
Here is another option using LINQ to XML.First create your own class that will hold that data you are looking for like this:
public class TeamInfo
{
public string TeamName { get; set; }
public int TeamId { get; set; }
}
Then you would parse the xml into a list of objects like this:
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.