Click here to Skip to main content
Click here to Skip to main content

Xmlawy XML Framework

, 29 Dec 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
An object based framework to access XML files.

Introduction

This framework provides an easy way to access XML files with support for inserting, updating, deleting, and selecting by querying the XML like querying SQL.

Using the code

I'll start by writing simple lines of code that demonstrate the operations of inserting, updating, deleting, and selecting, and then show the results in an XML file.

Here is how we create an XML file and access an object:

static void Main(string[] args)
{
    BaseDataAccess access = 
      new BaseDataAccess("D:/xmltest.xml", "Objects");
}

Simple inserting

The Insert method takes an object of type BaseDataObject as a parameter, which contains the name of the node, the value, the attributes, and a list of children which are of the same type as well.

access.Insert(new BaseDataObject("type", "human")).Execute();
access.Insert(new BaseDataObject("type", "animal")).Execute();

2.png

Simple selecting

You can query the XML to select specific nodes:

List<BaseDataObject> dataList = access.Select("type").
                                          Where("type").
                                          IsEqualTo("animal").
                                          ExecuteSelect<BaseDataObject>();

for (int i = 0; i < dataList.Count; i++)
{
    Console.WriteLine(dataList[i].Name + " : " + dataList[i].Value);
}

3.png

Updating

It is also easy to update specific nodes:

access.Update("type").Set("type").EqualTo("animal").
       Where("type").IsEqualTo("human").Execute();

4.png

Deleting

And here is how we delete:

access.Delete("type").Where("type").IsEqualTo("animal").Execute();

6.png

Advanced inserting

The way to insert children of children is to make a list of BaseDataObjects and insert them in the list of the children of another BaseDataObject. (The best way to do this is recursively.) The following code inserts three levels of children in depth:

static void Main(string[] args)
{
    // ACCESS
    BaseDataAccess access = 
      new BaseDataAccess("test.xml", "objects");

    // INSERTING 3 LEVELS IN DEPTH
    List<BaseDataObject> parents = new List<BaseDataObject>();
    for (int i = 0; i < 5; i++)
    {
        parents.Add(new BaseDataObject("Parent", MakeChilds(5)));
    }
    BaseDataObject parentOfParents = 
       new BaseDataObject("GrandParent", parents);
    access.Insert(parentOfParents).Execute();

    Console.WriteLine(access.XmlString);

    Console.ReadLine();
}

private static List<BaseDataObject> MakeChilds(int count)
{
    List<BaseDataObject> childs = new List<BaseDataObject>();
    for (int i = 0; i < count; i++)
    {
        childs.Add(new BaseDataObject("N" + i.ToString(), 
                   "V " + i.ToString()));
    }
    return childs;
}

7.png

Advanced selecting

Here is how we select any child in the selected objects, recursively, with the method PrintObject(obj):

static void Main(string[] args)
{
    // ACCESS
    BaseDataAccess access = new BaseDataAccess("test.xml", "objects");

    // INSERTING 3 LEVELS IN DEPTH
    List<BaseDataObject> parents = new List<BaseDataObject>();
    for (int i = 0; i < 5; i++)
    {
        parents.Add(new BaseDataObject("Parent", MakeChilds(5)));
    }
    BaseDataObject parentOfParents = new BaseDataObject("GrandParent", parents);
    access.Insert(parentOfParents).Execute();

    // SELECTING
    List<BaseDataObject> objects = 
      access.Select("GrandParent").ExecuteSelect<BaseDataObject>();
    for (int i = 0; i < objects.Count; i++)
    {
        PrintObject(objects[i]);
    }

    Console.ReadLine();
}

private static List<BaseDataObject> MakeChilds(int count)
{
    List<BaseDataObject> childs = new List<BaseDataObject>();
    for (int i = 0; i < count; i++)
    {
        childs.Add(new BaseDataObject("N" + i.ToString(), 
                   " V" + i.ToString() + " "));
    }
    return childs;
}

private static void PrintObject(BaseDataObject obj)
{
    if (obj.Childs.Count > 0)
    {
        Console.WriteLine(obj.Name + " >");
        for (int i = 0; i < obj.Childs.Count; i++)
        {
            PrintObject(obj.Childs[i]);
        }
    }
    else
    {
        Console.WriteLine(obj.Name + " : " + obj.Value);
    }
}

8.png

Inserting simple objects

If you want to quickly save the properties of an object in XML (simple serialization), there is another generic Insert method which takes any object as a parameter and inserts all the properties of type string or int in the XML file.

class MyClass
{
    public MyClass()
    {
        Age = 20;
        FirstName = "Islam";
        LastName = "Eldemery";
        Address = "Egypt, Cairo, bla bla bla";
    }

    public int Age { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }

}

class Program
{
    static void Main(string[] args)
    {
        MyClass obj = new MyClass();

        // ACCESS
        BaseDataAccess access = new BaseDataAccess("test.xml", "objects");

        // INSERT
        access.Insert<MyClass>(obj).Execute();

        Console.WriteLine(access.XmlString);

        Console.ReadLine();
    }
}

9.png

So far so good, let's dig deeper..

Overriding the virtual methods in the base class

Imagine you want to encrypt the XML after inserting, and decrypt it before selecting. This can be easily done by overriding the base methods as follows:

class XmlAccess : BaseDataAccess
{
    XMLEncryptor encryptor;

    public XmlAccess(string path, string rootElement)
        : base(path, rootElement)
    {
        encryptor = new XMLEncryptor(path);
    }

    public override bool Execute()
    {
        bool executed = base.Execute();

        ///////////////////////////////////////////////////
        // ENCRYPTOR GOES HERE
        encryptor.Encrypt(base._objectToInsert.Name, "myKey");
        ///////////////////////////////////////////////////

        return executed;
    }

    public override List<T> ExecuteSelect<T>()
    {
        ///////////////////////////////////////////////////
        // ENCRYPTOR GOES HERE
        encryptor.Decrypt("myKey");
        ///////////////////////////////////////////////////

        return base.ExecuteSelect<T>();
    }
}

Select all nodes

You can also select all the nodes in the root node with all children and children of children, in one line of code:

List<BaseDataObject> objects = access.SelectAll<BaseDataObject>();

And again, this is to loop on them recursively:

static void Main(string[] args)
{
    // ACCESS
    BaseDataAccess access = 
      new BaseDataAccess(@"D:\XmlFiles\StructureMap.xml", 
                         "doesntMatter");

    // SELECTING
    List<BaseDataObject> objects = access.SelectAll<BaseDataObject>();

    // DISPLAYING
    for (int i = 0; i < objects.Count; i++)
        PrintObject(objects[i]);

    Console.ReadLine();
}

private static void PrintObject(BaseDataObject obj)
{
    if (obj.Childs.Count > 0)
    {
        Console.WriteLine(obj.Name + " >");

        for (int i = 0; i < obj.Childs.Count; i++)
            PrintObject(obj.Childs[i]);
    }
    else
        Console.WriteLine(obj.Name + " : " + obj.Value);
}

History

The code in the library is fully commented. This is version 1. I hope we can add more and more features in the future.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Islam ElDemery
Web Developer Business Development Gate
Egypt Egypt
Blog
Follow on   Twitter

Comments and Discussions

 
Questioncool! Pinmemberpophelix24-Jul-11 22:06 
GeneralFillChildObjects & SelectAll (undocumented features). PinmemberNo37.Joburg.SA5-Jan-09 23:37 
Great peace of coding. Love your work. Smile | :)
 
It looks like the attributes doesn't get added to the object in the FillChildObjects method.
I've added a line obj.Attributes.AddRange(attributes); to fix it on my side.
 
private void FillChildObjects<T>(ref T parentDataObject, IEnumerable<XElement> root) where T : BaseDataObject, new()
{
    foreach (var childElement in root)
    {
        T obj = new T();
 
        List<XmlAttribute> attributes = new List<XmlAttribute>();
 
        if (childElement.HasAttributes)
        {
            foreach (XAttribute attr in childElement.Attributes())
            {
                attributes.Add(new XmlAttribute(attr.Name.LocalName, attr.Value));
            }
            obj.Attributes.AddRange(attributes);
        }
 
        obj.Name = childElement.Name.LocalName;
 
        if (childElement.HasElements)
        {
            FillChildObjects<T>(ref obj, childElement.Elements());
        }
        else
        {
            obj.Value = childElement.Value;
        }
            
        parentDataObject.Childs.Add(obj);
    }
}
 
Also in the SelectAll method the dataRecord's objectToSelect property comes through as null. I've changed it to:
//dataRecord.Name = this.objectToSelect;
dataRecord.Name = record.Name.LocalName;
 
Hope this is correct. Roll eyes | :rolleyes:
 
Cheers,
Leon "No37"
GeneralRe: FillChildObjects & SelectAll (undocumented features). PinmemberIslam ElDemery6-Jan-09 0:23 
GeneralCool job. PinmemberKevin Lu5-Jan-09 21:06 
Questionhow to do? Pinmemberm7775-Jan-09 15:34 
AnswerRe: how to do? PinmemberIslam ElDemery6-Jan-09 0:17 
Generalwow... Pinmemberdudeserius5-Jan-09 9:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 29 Dec 2008
Article Copyright 2008 by Islam ElDemery
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid