Skip to content

Instantly share code, notes, and snippets.

@compil3
Last active December 27, 2015 12:19
Show Gist options
  • Select an option

  • Save compil3/7324471 to your computer and use it in GitHub Desktop.

Select an option

Save compil3/7324471 to your computer and use it in GitHub Desktop.
public void ParseTeams()
{
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(Source);
var teamName = htmlDoc.DocumentNode
.SelectNodes("//select[@id='team']//option[position() > 1]")
.Select(node => node.InnerText.Trim())
.ToList();
var abbr = htmlDoc.DocumentNode
.SelectNodes( "//select[@id='team']//option[position() > 1]/@value" )
.Select( node => node.Attributes["value"].Value )
.ToList();
var elementNames = new[] { "Name","Short" };
var xmlDoc = new XElement( "NHL",
new XAttribute( "Updated", DateTime.Now.ToString() ) );
XElement iteratingElement = null;
var length = elementNames.Length;
for (int i = 0; i < teamName.Count; i++)
{
if (i % ((i == 0) ? 1 : length) == 0)
{
iteratingElement = new XElement("Team",
new XAttribute("Name", teamName[i]),
new XElement(elementNames[(i % length)], abbr[i]));
xmlDoc.Add(iteratingElement);
}
else
{
iteratingElement.Add(new XElement(elementNames[(i % length) - 1], abbr[i]));
}
}
xmlDoc.Save("teams.xml");
}
// Wrong Formating
//<NHL Updated="11/5/2013 1:52:59 PM">
// <Team Name="Anaheim Ducks">
// <Name>ANA</Name>
// <Name>BOS</Name> //this shouldn't be here
// </Team>
// <Team Name="Buffalo Sabres">
// <Name>BUF</Name>
// <Name>CGY</Name>
// </Team>
//Formating I need
//<NHL Updated="11/5/2013 1:52:59 PM">
// <Team Name="Anaheim Ducks">
// <Name>ANA</Name>
// </Team>
// <Team Name="Buffalo Sabres">
// <Name>BUF</Name>
// </Team>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment