Results 1 to 8 of 8

Thread: [RESOLVED] Help Reading DataSet from XML

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2002
    Posts
    196

    Resolved [RESOLVED] Help Reading DataSet from XML

    I have the following XML file:

    Code:
    <map gsid="101">
    	<name> Dalian Plant </name>
    	<briefing locid="LOADINGSCREEN_MAPDESCRIPTION_dalianplant">map description for modders maps not localized (english only)</briefing>
    	<music> common/sound/menu/music/load_CH_music.ogg </music>
    	<modes>
    		<mode type="gpm_cq">
    			<maptype ai="1" players="16" type="doubleassault" locid="GAMEMODE_DESCRIPTION_doubleassault">Unlocalized test for this mode on this map</maptype>
    			<maptype players="32" type="headon" locid="GAMEMODE_DESCRIPTION_headon">Unlocalized test for this mode on this map</maptype>
    			<maptype players="64" type="headon" locid="GAMEMODE_DESCRIPTION_headon">Unlocalized test for this mode on this map</maptype>
    		</mode>
    
    		<mode type="gpm_coop">
    			<maptype ai="1" players="16" type="doubleassault" locid="GAMEMODE_DESCRIPTION_COOP_doubleassault">Unlocalized test for this mode on this map</maptype>
    		</mode>
    	</modes>
    </map>
    And this code:

    Code:
    			string gpm = "";
    			System.Data.DataSet ds = new System.Data.DataSet();
    			ds.ReadXml(MapFileName);
    
    			foreach(System.Data.DataRelation myRelation in ds.Tables["mode"].ChildRelations)
    			{
    				foreach(System.Data.DataRow myRow in ds.Tables["mode"].Rows)
    				{
    					foreach(System.Data.DataRow childrow in myRelation.ChildTable.Rows)
    					{
    						gpm = gpm + myRow["type"] + "," + childrow["players"] + "/";
    					}
    				}
    			}
    
    			return gpm;
    I need my output to be:

    "gpm_cq,16/gpm_cq,32/gpm_cq,64/gpm_coop,16"

    but it's not working the way I want.
    Brandon S Davids

  2. #2
    Hyperactive Member umilmi81's Avatar
    Join Date
    Sep 2005
    Location
    Sterling Heights, Mi.
    Posts
    335

    Re: Help Reading DataSet from XML

    Is there a reason you are accessing an XML file through a data source?

    The XML itself is fairly powerful.

    Code:
                XmlDocument xd = new XmlDocument();
                xd.Load(@"c:\temp\vbforums.xml");
    
                
                foreach (XmlElement xMode in xd.GetElementsByTagName("modes"))
                {
                    foreach(XmlElement xMapType in xMode.GetElementsByTagName("maptype"))
                    {
                        Console.WriteLine(xMapType.GetAttribute("players"));
                    }
                }

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2002
    Posts
    196

    Re: Help Reading DataSet from XML

    Thanks. I don't need a dataset, it was just an easy way to import the xml into a grid to see if it would help me visually.

    How do I reference the "gpm_cq" on the same line as your Console.Writeline?

    I tried pulling up all the intellisense properties on xMode and xMapType and I'm looking through the Locals window but I haven't figured it out yet.
    Brandon S Davids

  4. #4
    Hyperactive Member umilmi81's Avatar
    Join Date
    Sep 2005
    Location
    Sterling Heights, Mi.
    Posts
    335

    Re: Help Reading DataSet from XML

    Quote Originally Posted by brandoom
    Thanks. I don't need a dataset, it was just an easy way to import the xml into a grid to see if it would help me visually.

    How do I reference the "gpm_cq" on the same line as your Console.Writeline?

    I tried pulling up all the intellisense properties on xMode and xMapType and I'm looking through the Locals window but I haven't figured it out yet.
    Code:
                XmlDocument xd = new XmlDocument();
                xd.Load(@"c:\temp\vbforums.xml");
    
                
                foreach (XmlElement xMode in xd.GetElementsByTagName("modes"))
                {
                    foreach(XmlElement xMapType in xMode.GetElementsByTagName("maptype"))
                    {
                        Console.WriteLine(xMapType.GetAttribute("players"));
                        Console.WriteLine(xMode.GetAttribute("type"));
                    }
                }
    Rate my post if it helped you.
    Last edited by umilmi81; May 23rd, 2006 at 01:23 AM.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Dec 2002
    Posts
    196

    Re: Help Reading DataSet from XML

    I tried that already and it returns a blank string. Any other ideas?
    Brandon S Davids

  6. #6
    Hyperactive Member umilmi81's Avatar
    Join Date
    Sep 2005
    Location
    Sterling Heights, Mi.
    Posts
    335

    Re: Help Reading DataSet from XML

    Quote Originally Posted by brandoom
    I tried that already and it returns a blank string. Any other ideas?
    It should work. Remember it's case sensitive. So "type" is different from "Type" is different from "TYPE". Check your XML again.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Dec 2002
    Posts
    196

    Re: Help Reading DataSet from XML

    Sorry but it doesn't work. I know it's case sensitive.

    Code:
    ? xMode.GetAttribute("type")
    ""
    ? xMode.GetAttribute("Type")
    ""
    ? xMode.GetAttribute("TYPE")
    ""
    Brandon S Davids

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Dec 2002
    Posts
    196

    Re: Help Reading DataSet from XML

    Problem Solved.

    Notice the top line needs to be "mode" NOT "modes".

    Thanks for helping me out on this one. I have my head wrapped around the xml now.

    Code:
    			foreach (XmlElement xMode in xd.GetElementsByTagName("mode"))
    			{
    				foreach(XmlElement xMapType in xMode.GetElementsByTagName("maptype"))
    				{
    					Console.WriteLine(xMapType.GetAttribute("players"));
    					Console.WriteLine(xMode.GetAttribute("type"));
    				}
    Brandon S Davids

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width