Employers - Post Free Jobs
Forum guidelines
AuthorQuestion
XML DataBase - Small Windows Application
Posted on: 10 Dec 2012
I am new in c# programming and i would like to create a small database in XML file for educational purpose.
The question is which way is proper or more editable when I create a small database in XML?

For example:

//////////Example 1////////////////
<student>
  <name>Nick Young</address>
  <address>8th Street/</address>
  <phone>0123456789</phone>
</student>
//////////////////////////////////

//////////Example 2////////////////
<student Name ="Nick Young" address = "8th Street" phone ="0123456789">XXXX</student>
//////////////////////////////////

I would like to highlight here that i need to Add,Rename/Edit,Remove and Search  students.

Seny
AuthorReply
Vulpes
  • 1
  • 0
accepted
Re: XML DataBase - Small Windows Application
Posted on: 10 Dec 2012   Accepted Answer
Personally, I've always felt that child nodes are more flexible than attributes when storing data.

You might also like to read what W3Schools have to say on the matter:

http://www.w3schools.com/dtd/dtd_el_vs_attr.asp

Seny
  • 0
  • 0
Re: XML DataBase - Small Windows Application
Posted on: 10 Dec 2012  
Thank you very much.I've created a small Application with the aid of a tutorial and i use the first example from above (Example 1), but i have a serious problem with [delete property].I don't know how can i delete a student using C#. 

Here is the XML File examlpe:
////////////////////////////////////////////////
<students>
<name>Nick Young</name>
<address>8th Street/</address>
<phone>0123456789</phone>
<name>George Old</name>
<address>9th Street/</address>
<phone>1234567890</phone>
</students>
////////////////////////////////////////////////

Seny
Re: XML DataBase - Small Windows Application
Posted on: 10 Dec 2012  
I've changed the XML file so that each student's data is contained within an individual <student> ... </student> element:

<students>
  <student>
     <name>Nick Young</name>
     <address>8th Street/</address>
     <phone>0123456789</phone>
  </student>
  <student> 
     <name>George Old</name>
     <address>9th Street/</address>
     <phone>1234567890</phone>
  </student>
</students>

Suppose now that you want to delete Nick Young from the file. The following LINQ to XML code will do it:

using System;
using System.Linq;
using System.Xml.Linq;


class Test
{
   static void Main()
   {
       XElement students = XElement.Load("seny.xml");
       string findName = "Nick Young";
       XElement foundElement  = (from student in students.Elements("student") where student.Element("name").Value == findName select student).FirstOrDefault();
       if (foundElement != null)
       { 
          foundElement.Remove(); // removes it from its parent
          Console.WriteLine("{0} found and deleted", findName);
          students.Save("seny2.xml"); 
       }
       else
       {
          Console.WriteLine("{0} not present in XML file", findName);
       }

       Console.ReadKey(); 
   }
}

After running this code, the file should look like this:

<students>
  <student>
    <name>George Old</name>
    <address>9th Street/</address>
    <phone>1234567890</phone>
  </student>
</students>



Employers - Post Free Jobs

SPONSORED BY

Offshore Software and Database Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.