XML in c# app help

Need help with XML being used in a c# app

Page 1 of 1

3 Replies - 1407 Views - Last Post: 05 July 2009 - 05:20 PM Rate Topic: -----

#1 gryphin  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 128
  • Joined: 29-October 05

XML in c# app help

Posted 05 July 2009 - 03:34 PM

Ok, so I am new and do not know a lot about programming but know even less about c# (though I have taken the and passed basic c++ classes). Since my knowledge is limited in programming I am going to state what I am looking to do at this time, but I am open to suggestions that would improve the program. It is an app that basicly creates a type of vehicle for a paper and pencil rpg game. I am now working on the weapons (not sure if that is the best place, but the one that stands out the most to me). My choice right now has been to setup the selection of the weapons in the app as a treeview object. I have already put into the node positions the names of the items it is to allow to be selected. I am stuck right now using XML for providing the info to the weapons/equipment. I have 2 questions right now...

1. Is it possible to have more than one item in the XML file if I plan to use it as stated above? This is an example of 1 item that will be listed (and there will be at least 40+ items minimum). -- Here I also need to know how to place the name so it will be useable in the app (such as where it says <weapon> - would that not need to be more like <weapon name>?

<?xml version="1.0" encoding="UTF-8"?> 
<weapon> 
  <class>Energy</class> 
  <item>1</item> 
  <name>PPC</name> 
  <techBase>Inner Sphere</techBase> 
  <level>1</level> 
  <tonnage>10</tonnage> 
  <heat>10</heat> 
  <crits>3</crits> 
  <multiplelocations>false</multiplelocations> 
  <shotsPerTon>0</shotsPerTon> 
  <cost/> 
  <battleValue/> 
  <minRange>3</minRange> 
  <shortRange>6</shortRange> 
  <mediumRange>9</mediumRange> 
  <longRnage>12</longRnage> 
</weapon> 



2. How do I get it to associate with the treeview in the app? I need it to be that when the user of the app double clicks the item in the list that it gets selected and moved to a place where it will be able to be allocated (placed into a position like in a arm, leg, etc of the machine) - I do not have the selected equipment box picked out yet as I am not sure what type would work best (so I am open to suggestions there too)...

Thank you ahead of time for all your help in this.

Gryphin.

Is This A Good Question/Topic? 0
  • +

Replies To: XML in c# app help

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: XML in c# app help

Posted 05 July 2009 - 04:15 PM

Hmm well it's midnight so I can't really help with specifics.

But, to get you started, you can do this to loop through each key/value pair.

XmlDocument MyDoc = Xmldocument.Load("xmlfilename.xml");
foreach (XmlNode CurrNode in MyDoc.DocumentElement.ChildNodes) {
	// Key = CurrNode.Name
	// Value = CurrNode.InnerText
}


Maybe create a Dictionary (<string, string>) to store it all for easier access. Then once you've done that you can translate it to different parts of your TreeView.
Was This Post Helpful? 0
  • +
  • -

#3 gryphin  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 128
  • Joined: 29-October 05

Re: XML in c# app help

Posted 05 July 2009 - 04:51 PM

View PostMageUK, on 5 Jul, 2009 - 04:15 PM, said:

Hmm well it's midnight so I can't really help with specifics.

But, to get you started, you can do this to loop through each key/value pair.

XmlDocument MyDoc = Xmldocument.Load("xmlfilename.xml");
foreach (XmlNode CurrNode in MyDoc.DocumentElement.ChildNodes) {
	// Key = CurrNode.Name
	// Value = CurrNode.InnerText
}


Maybe create a Dictionary (<string, string>) to store it all for easier access. Then once you've done that you can translate it to different parts of your TreeView.


A couple questions... First, I do not understand the code you gave comepletely. Could I give you an example of my xml code and see if I am understanding it?

XML is PPC.XML
<?xml version="1.0" encoding="UTF-8"?> 
<weapon> 
  <class>Energy</class> 
  <item>1</item> 
  <name>PPC</name> 
  <techBase>Inner Sphere</techBase> 
  <level>1</level> 
  <tonnage>10</tonnage> 
  <heat>10</heat> 
  <crits>3</crits> 
  <multiplelocations>false</multiplelocations> 
  <shotsPerTon>0</shotsPerTon> 
  <cost/> 
  <battleValue/> 
  <minRange>3</minRange> 
  <shortRange>6</shortRange> 
  <mediumRange>9</mediumRange> 
  <longRnage>12</longRnage> 
</weapon> 



So this would make the code you gave me look like this:
XmlDocument MyDoc = Xmldocument.Load("ppc.xml");
foreach (XmlNode CurrNode in MyDoc.DocumentElement.ChildNodes) {
	// Key = CurrNode.Name
	// Value = CurrNode.InnerText
}



How would I get it to display the info into label areas I have in the visual setup? And I am sure I missed something in the code from the previous post... The name of the app is "Mech Editor".

How does one make the dictionary thing mentioned? I have no experience or knowledge here at all...

Gryphin.

This post has been edited by gryphin: 05 July 2009 - 04:52 PM

Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: XML in c# app help

Posted 05 July 2009 - 05:20 PM

If you're using .Net 3.5 of greater I would take a look at using LINQ to XML, it's an extremely efficient and easy way to work with XML.

Here's an example using your XML structure. First I would create a simple class to hold the elements of your file, say call is Weapons

using System;

/// <summary>
/// Summary description for Weapons
/// </summary>
public class Weapons
{
	public string weaponClass { get; set; }
	public int item { get; set; }
	public string name { get; set; }
	public string techBase { get; set; }

	//...add rest of items like this
}



Then in a separate class you can use LINQ to XML to populate a generic list of your Weapons class, something like this

/// <summary>
/// method using LINQ to XML for getting weapons and
/// attributes from an XML file
/// </summary>
/// <returns></returns>
public List<Weapons> GetWeapons()
{
	//Create an XDocument instance (http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx)
	XDocument doc = Xdocument.Load("PathToYourFIle");

	//generic list for holind our weapons
	List<Weapons> items = new List<Weapons>();

	//use LINQ to XML to get the data
	var weapons = from weapon in doc.Elements("Weapons").Elements("weapon")
				  select new Weapons
				  {
					  weaponClass = (string)weapon.Attribute("class"),
					  item = (int)weapon.Attribute("item"),
					  name = (string)weapon.Attribute("name"),
					  techBase = (string)weapon.Attribute("techBase")
					  //...rest of elements here
				  };

	//now loop through our items and populate our generic list
	foreach (var w in weapons)
		items.Add(w);

	//return the populated list
	return items;
}



Then you would have a Generic list holding all the weapons (with their attributes). This is just another option for you to think about if using 3.5 or higher :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1