So I'm starting to use XML and I'm trying to use it as a database of sorts. I've managed to save it off and read the whole thing in, but now the problem is that I want to be able to search through, get an item, and make it.

My current XML setup is below.

<?xml version="1.0" encoding="utf-8"?>
<Items>
<Object>
  <Index>10</Index>
  <Damage>4650</Damage>
</Object>
<Object>
   <Index>8</Index>
   <Damage>2940</Damage>
</Object>
<SecondObject>
   <Index>10</Index>
   <HP>0</HP>
   <Name>Some Dude</Name>
</SecondObject>
</Items>

So I'd like to be able to call for a "Object" of a certain index then use those details (both the index and the damage) to make an item.

I've tried a few different things but I wasn't able to get anything to work right. Any ideas?

You can search for C# XPATH on Google for tons of information on this subject. I am not sure you will find exactly what you are looking for.

There may be a better alternative, or atleast worth your time to look at, and that is to use the existing power built into the DataSet class.

For example, you can create your own XML file based database by creating a DataSet with tables, and columns (the schema). You can load this dataset from a file, and make your changes, then save it back to the file.
Lets say you have a DataSet named Config that has several tables.
When your application starts, it can check to see if you have a file named MyConfig.xml, and if so then load it.

Config.ReadXml("MyConfig.xml");

Now your application has all of the tables for this dataset loaded with data and ready to be used. Later when you want to save your changes back to the file:

Config.AcceptChanges();
Config.WriteXml("MyConfig.xml",XmlWriteMode.WriteSchema);

Now because you have all of your data in the DataSet, you can use the standard (robust) features to search, filter, etc. this data.

foreach(DataRow data in Config.Tables["Items"].Select("Index=10"))
{
      MessageBox.Show( (string)data["Damage"] );
}

Hope this helps,
Jerry

DataSet is has a solution of your problem.

Consider the following code:

DataSet ds = new DataSet();
            // Read XML File
            ds.ReadXml(@"c:\test1\WindowsApplication1\a1.xml");

            // Assign Reference of Object - DataTable
           //   DataTable dt=ds.Tables["Object"];
            dataGridView1.DataSource = ds.Tables["Object"];
                      
            dataGridView2.DataSource = ds.Tables["SecondObject"];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.