473,378 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Reading in XML file into 2D Array

I've been trying to play around with reading in an XML file that would look something like this into a 2D array with no luck:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" ?>
  2. <map index="0" name="Test" cellsx="2" cellsy="2" cellwidth="16" cellheight="16">
  3.     <layers>
  4.         <layer index="0" name="TopLayer" visible="1">
  5.             <cell x="0" y="0" img="24" />
  6.             <cell x="1" y="0" img="56" />
  7.             <cell x="0" y="1" img="23" />
  8.             <cell x="1" y="1" img="76" />
  9.         </layer>
  10.         <layer index="1" name="BottomLayer" visible="1">
  11.             <cell x="0" y="0" img="3" />
  12.             <cell x="1" y="0" img="3" />
  13.             <cell x="0" y="1" img="4" />
  14.             <cell x="1" y="1" img="4" />
  15.         </layer>
  16.     </layers>
  17. </map>
  18.  
I want to go through this XML document and put the data into a 2D array.
So if the program went through it correctly there would be two 2D arrays of ints of size 2x2 with the number under img"#'. This would be similar to:
Expand|Select|Wrap|Line Numbers
  1. int [,] TopLayer = {
  2. {24,56},
  3. {23,76}
  4. };
I've been reading many tutorials on using C#'s System.Xml to handle an XML file similar to this with no luck. Anyone else know how to do this, I thought it sounded simple but it has not been for me.

Any help will be appreciated.
Dec 5 '09 #1
1 4429
GaryTexmo
1,501 Expert 1GB
I don't know what you were looking at for XML tutorials, but look into the XmlDocument and XmlNode classes. You can read that entire block of XML into an XmlDocument object, then get the root node. After that, it's a hierarchical representation of the nodes, and fairly simple to parse using a foreach loop.

Some pseudo code to get you started, perhaps?

Expand|Select|Wrap|Line Numbers
  1. for every node in the root node
  2. {
  3.   if node is layers (ie, node.Name)
  4.   {
  5.     for every node in the layers node
  6.     {
  7.       if node is a layer node
  8.       {
  9.         read size data
  10.         instantiate a container object
  11.         for each node in the layer node
  12.         {
  13.           if node is a cell node
  14.           {
  15.             fill object with node data
  16.           }
  17.         }
  18.       }
  19.     }
  20.   }
  21. }
That should be a rough map of what you want to do, MSDN will help you find the specifics of syntax :)
Dec 6 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Foxy Kav | last post by:
Hi, another question from me again, i was just wondering if anyone could give me a quick example of reading data from a file then placing the data into an array for some manipulation then reading...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
10
by: T Cordon | last post by:
I am using a StreamReader to read text from an HTML file and display it as part of a page in a Label Control. Buy it is not displaying characters as: ñ, ó, ú, etc. Please Help. Thanks
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
29
by: yourmycaffiene | last post by:
Okay, this if my first post so go easy on me plus I've only been using C for a couple of weeks. I'm working on a program currently that requires me to read data from a .dat file into a 2d array and...
10
by: nuke1872 | last post by:
Hello guys, I have a file names network.txt which contains a matrix. I want to read this matrix as store it as an array. I am new to stuff like these...can anybody help me out !! Thanks nuke
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
2
by: GeoUK | last post by:
Hi All, New member here with a bit of problem. I have read the FAQ's and searched text books but still cannot solve the problem that I have. As part of a course I am doing at University I had...
0
by: Anish G | last post by:
Hi, I have an issue with reading CSV files. I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads...
21
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.