private void button1_Click(object sender, EventArgs e) { OpenFileDialog openShipDia = new OpenFileDialog(); openShipDia.DefaultExt = ".xml"; openShipDia.Filter = "XML ship file (*.xml)|*.xml|All files|*.*"; openShipDia.ShowDialog(); HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument(); // There are various options, set as needed htmlDoc.OptionFixNestedTags = true; // filePath is a path to a file containing the html htmlDoc.Load(openShipDia.FileName); // Use: htmlDoc.LoadXML(xmlString); to load from a string // ParseErrors is an ArrayList containing any errors from the Load statement if (htmlDoc.ParseErrors != null && htmlDoc.ParseErrors.Count() > 0) { // Handle any parse errors as required foreach (HtmlParseError error in htmlDoc.ParseErrors) { Console.WriteLine(error.ToString()); } } if (htmlDoc.DocumentNode != null) { HtmlNodeCollection weps = htmlDoc.DocumentNode.SelectNodes("//weaponblueprint");// SelectSingleNode("//body"); if (weps != null) { // Do something with bodyNode foreach (HtmlNode node in weps) { WeaponBlueprints tempWep = new WeaponBlueprints(); tempWep.name = node.GetAttributeValue("name","#NOT FOUND~"); if (tempWep.name == "#NOT FOUND~") continue; tempWep.type = node.SelectSingleNode(".//type").InnerText;//reader.ReadElementContentAsString("type", ""); tempWep.title = node.SelectSingleNode(".//title").InnerText; if( node.SelectSingleNode(".//short") != null) tempWep.shortname = node.SelectSingleNode(".//short").InnerText; if (node.SelectSingleNode(".//desc") != null) tempWep.desc = node.SelectSingleNode(".//desc").InnerText; Game.weapons.Add(tempWep); } } } foreach (WeaponBlueprints wep in Game.weapons) { wep1.Items.Add(wep); wep2.Items.Add(wep); wep3.Items.Add(wep); wep4.Items.Add(wep); } }
public void LoadBlueprints(string path) { HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument(); htmlDoc.OptionFixNestedTags = true; htmlDoc.Load(path);//OptionsForm.dataPath + "\\data\\blueprints.xml"); // ParseErrors is an ArrayList containing any errors from the Load statement if (htmlDoc.ParseErrors != null && htmlDoc.ParseErrors.Count() > 0) { // Handle any parse errors as required foreach (HtmlParseError error in htmlDoc.ParseErrors) { Console.WriteLine(error.ToString()); } } if (htmlDoc.DocumentNode != null) { HtmlNodeCollection weps = htmlDoc.DocumentNode.SelectNodes("//weaponblueprint");// SelectSingleNode("//body"); if (weps != null) { // Do something with bodyNode foreach (HtmlNode node in weps) { WeaponBlueprints tempWep = new WeaponBlueprints(); tempWep.name = node.GetAttributeValue("name", "#NOT FOUND~"); if (tempWep.name == "#NOT FOUND~") continue; tempWep.type = node.SelectSingleNode(".//type").InnerText; tempWep.title = node.SelectSingleNode(".//title").InnerText; if (node.SelectSingleNode(".//short") != null) tempWep.shortname = node.SelectSingleNode(".//short").InnerText; if (node.SelectSingleNode(".//desc") != null) tempWep.desc = node.SelectSingleNode(".//desc").InnerText; Game.weapons.Add(tempWep); } } } if (htmlDoc.DocumentNode != null) { HtmlNodeCollection augs = htmlDoc.DocumentNode.SelectNodes("//augblueprint"); if (augs != null) { // Do something with bodyNode foreach (HtmlNode node in augs) { AugmentBlueprint tempAug = new AugmentBlueprint(); tempAug.name = node.GetAttributeValue("name", "#NOT FOUND~"); if (tempAug.name == "#NOT FOUND~") continue; tempAug.title = node.SelectSingleNode(".//title").InnerText; if (node.SelectSingleNode(".//desc") != null) tempAug.desc = node.SelectSingleNode(".//desc").InnerText; Game.augments.Add(tempAug); } } } if (htmlDoc.DocumentNode != null) { HtmlNodeCollection drones = htmlDoc.DocumentNode.SelectNodes("//droneblueprint"); if (drones != null) { // Do something with bodyNode foreach (HtmlNode node in drones) { DroneBlueprint tempDrone = new DroneBlueprint(); tempDrone.name = node.GetAttributeValue("name", "#NOT FOUND~"); if (tempDrone.name == "#NOT FOUND~") continue; tempDrone.type = node.SelectSingleNode(".//type").InnerText;//reader.ReadElementContentAsString("type", ""); tempDrone.title = node.SelectSingleNode(".//title").InnerText; if (node.SelectSingleNode(".//short") != null) tempDrone.shortname = node.SelectSingleNode(".//short").InnerText; if (node.SelectSingleNode(".//desc") != null) tempDrone.desc = node.SelectSingleNode(".//desc").InnerText; Game.drones.Add(tempDrone); } } } foreach (AugmentBlueprint aug in Game.augments) { Game.optWnd.aug1.Items.Add(aug); Game.optWnd.aug2.Items.Add(aug); Game.optWnd.aug3.Items.Add(aug); } foreach (DroneBlueprint wep in Game.drones) { Game.optWnd.drone1.Items.Add(wep); Game.optWnd.drone2.Items.Add(wep); Game.optWnd.drone3.Items.Add(wep); Game.optWnd.drone4.Items.Add(wep); } foreach (WeaponBlueprints wep in Game.weapons) { Game.optWnd.wep1.Items.Add(wep); Game.optWnd.wep2.Items.Add(wep); Game.optWnd.wep3.Items.Add(wep); Game.optWnd.wep4.Items.Add(wep); } }