Introduction
This library is inspired on "Source Engine" (HL2SDK) KeyValues Class
http://developer.valvesoftware.com/wiki/KeyValues_class (SourceSDK KeyValues Class)
It was used to save data and for send data to a entity.
It can have unlimited keys and subkeys inside a KeyValue
SourceSDK KeyValues metods:
View
[^]
Diferences:
More Metods
More easy to loop all keys
Easy to update or add features
I decide make this library because i need something like that but XML was hard to me read in C#.
So i have make as alternative to XML, this is a easy to use library and very powerfull.
This also can be used as small databases, i have used KeyValues in "Counter-Strike Source" in my zombie mod to save each player status such:
damage taken, damage given, bullets spent, kills, deaths, name, steamid, and many others.
KeyValues Explorer:
This is a simple Application i do for explore, manage keyvalues, this also can generate source code for you use in your application.
Screenshot of KeyValues Explorer App
Background
This is my first project realease on CodeProject, so i wait people helpme improve article information and makeit clean and nice.
That class contain very metods and usefull things, fell free to explore it.
How its KeyValues file struct in disk?
"KeyValue Name"<br />
{<br />
"key" "value"<br />
"other key" "other Value"<br />
"many other key" "value to key :P"<br />
"an subkey name"<br />
{<br />
"key" "value"<br />
"my key" "my value"<br />
"2key" "2value"<br />
NoSpaceKey WithoutQuotes<br />
UsingNoQuotes ASpaceWillDetermineNextValue<br />
#include "C:\test_include.txt"<br />
"Look An Other SubKey"<br />
{<br />
"my parent was" "an subkey name"<br />
}<br />
}<br />
}<br />
Help wanted
My english is not very good, so if any one wants help, can fix some english in my class and fix XML documentation for each metod, and create summary for missing metods.
Thanks if you can help
Using the Code
A brief description of how to use the article or code. The class names, the
methods and properties, any tricks or tips.
Creating a simple KeyValue, and save it to disk:
KeyValues kv = new KeyValues("namespace");
kv.Comment = "Testing comments (Optional)";
kv.SetString("my_dinner", "chiken");
Console.WriteLine("My Dinner Was: {0}", kv.GetValue("my_dinner"));
KeyValues subKV = new KeyValues("my_friends", "best", "CodeProject.com", null);
kv.AddSubKey(subKV);
kv.SaveToFile(@"C:\kv_test.txt");
Load last KeyValue from Disk and read thier values:
if (!kv.LoadFromFile(@"C:\kv_test.txt")) return;
KeyValues subkv = kv.FindKey("my_friends");
if (subkv == null) return;
Console.WriteLine("My Dinner Was: {0} And my best friend is: {1}", kv.GetValue("my_dinner"), subkv.GetValue("best"));
kv.SetDateTime("lastLoadedTime", DateTime.Now);
Loop all Key, Values and SubKeys:
void foreachKVMetod1(KeyValues kv)
{
KeyValues fkv;
KeyValuesData kvData;
while ((kvData = kv.GetNextKeyValue()) != null)
{
if (!string.IsNullOrEmpty(kvData.Comment))
Console.WriteLine("// {0}", kvData.Comment);
Console.WriteLine("[{0}] = {1}", kvData.Key, kvData.Value);
}
while ((fkv = kv.GetNextSubKey()) != null)
{
if (!string.IsNullOrEmpty(fkv.Comment))
Console.WriteLine("// {0}", fkv.Comment);
Console.WriteLine("\"{0}\"", fkv.Name);
Console.WriteLine("{");
foreachKVMetod1(fkv);
}
Console.WriteLine("}");
}
void foreachKVMetod2(KeyValues kv)
{
for (int i = 0; i < kv.KeyNameValues.Count; i++)
{
if (!string.IsNullOrEmpty(kv.KeyNameValues[i].Comment))
Console.WriteLine("// {0}", kv.KeyNameValues[i].Comment);
Console.WriteLine("[{0}] = {1}", kv.KeyNameValues[i].Key, kv.KeyNameValues[i].Value);
}
for (int i = 0; i < kv.KeyChilds.Count; i++)
{
if (!string.IsNullOrEmpty(kv.KeyChilds[i].Comment))
Console.WriteLine("// {0}", kv.KeyChilds[i].Comment);
Console.WriteLine("\"{0}\"", kv.KeyChilds[i].Name);
Console.WriteLine("{");
foreachKVMetod1(kv.KeyChilds[i]);
}
Console.WriteLine("}");
}
foreachKVMetod1(kv);
foreachKVMetod2(kv);
Variable or class names should be wrapped in <code> tags like this
.
Points of Interest
Math Operators in KeyValues:
kv += kv1
This will AddSubKey kv1 to Main kv
kv -= kv1
This will RemoveSubKey kv1 from Main kv
kv += "string"
This will AddSubKey string_name to Main kv
kv -= "string"
This will RemoveSubKey string_name from Main kv
kv += KeyValuesData
This will Set KeyValuesData key and value to Main kv
kv -= KeyValuesData
This will Remove KeyValuesData key and value from Main kv
Inside a file for load using KeyValues library you can include other keyvalues files:
"Root"<br />
{<br />
#include "C:/filename.txt"<br /><br />
"me" "you :P"<br />
"bool test" "True"<br />
"byte test" "1"<br />
"char test" "A"<br />
"decimal test" "10,1"<br />
"double test" "10,1"<br />
"float test" "10,1"<br />
"short test" "5000"<br />
"int test" "500000000"<br />
"long test" "5000000000000000000"<br />
"date test" "25-03-2009 1:16:31"<br />
}
History
v1.0.0.0:
First Realease
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.