.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Add and Retrieve data from XML using Three Tier Architecture

Posted By:Narayanan       Posted Date: June 11, 2013    Points: 200    Category: C#    URL: http://www.dotnetspark.com  

Here , I expalined Add and Retrieve data from XML using Three Tier Architecture
 

Introduction:

          One of my team members asked me, how to add and retrieve data from XML using Three Tier Architecture. This is the program for him and all viewers.

Index

1)      Decide  XML data Structure

2)      Decide Input Screen Properties.

3)      Create Project.

4)      Test and see Result.

1)      Decide XML Data Structure:

              Really, I like XML language .because user can decide and Write Data Structure in XML and it's also flexible.

In this sample Program XML Data structure is

Root [LoginScreen]

Element [LoginDetails]

       Element [UserName]

               Element [Password]

Sample Data

<? Xml version="1.0" encoding="utf-8"?>

<LoginScreen>

  <LoginDetails>

<UserName>naraayanan11245 </UserName>

<Password>naraayanan1234</Password></ LoginDetails >

</ LoginScreen >

2)      Decide Input Screen Properties:

                 Labels, Textboxes and Buttons - 2.

1.       Lable 1: User Name

2.       Lable 2: Password

3.       TextBox 1: User Name

4.       TextBox 2: Password

5.       Button1: Add

6.       Button2: Edit

 

3)      Create Project :

  Create DLLogin .cs.Copy and Paste

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Xml;

using System.IO;

using XMLProject.BusLayer;

 

namespace XMLProject.DataLayer

{

    class DLLogin

    {

 

        private static bool result;

        private static bool deleteResult;

        private static BLLogin BLLgn;

        public static bool CreateXMLFile(string XMLPath, int mode)

        {

            XmlDocument XMLDoc = new XmlDocument();

            string XMLSpec = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";

            XMLSpec = XMLSpec + Environment.NewLine + "<Loginscreen></Loginscreen>";

            File.AppendAllText(XMLPath, XMLSpec);

            if (mode == 1)

            {

                if (File.Exists(XMLPath))

                {

                    XMLDoc.Load(XMLPath);

                    XmlElement XMLEle = XMLDoc.CreateElement("LoginDetails");

                    string strConfig = string.Empty;

                    strConfig = strConfig + Environment.NewLine + @"<UserName>naraayanan</UserName>";

                    strConfig = strConfig + Environment.NewLine + @"<Password>naraayanan</Password>";

                    XMLEle.InnerXml = strConfig;

                    XMLDoc.DocumentElement.AppendChild(XMLEle);

                    XMLDoc.Save(XMLPath);

                 }

            }

            else if (mode == 2)

            {

                if (File.Exists(XMLPath))

                {

                    BLLgn = new BLLogin();

                    XMLDoc.Load(XMLPath);

                    XmlElement XMLEle = XMLDoc.CreateElement("LoginDetails");

                    string strConfig = string.Empty;

                    strConfig = strConfig + Environment.NewLine + @"<UserName>" + BLLgn.UserName +" </UserName>";

                    strConfig = strConfig + Environment.NewLine + @"<Password>" + BLLgn.Password +"</Password>";

                    XMLEle.InnerXml = strConfig;

                    XMLDoc.DocumentElement.AppendChild(XMLEle);

                    XMLDoc.Save(XMLPath);

                }

            }

            return result;

        }

       public static bool DeleteXML(string XMLPath)

        {

            if (File.Exists(XMLPath))

            {

                File.Delete(XMLPath);

                deleteResult = true;

            }

            else

            {

                deleteResult = false;

            }

            return deleteResult;

        }

        public static void ReadXML(string XMLPath)

        {

            XmlTextReader XMLText = new XmlTextReader(XMLPath);

            while (XMLText.Read())

            {

                XmlNodeType nodeType = XMLText.NodeType;

                if (nodeType == XmlNodeType.Element)

                {

                    BLLgn = new BLLogin();

                    switch (XMLText.Name)

                    {

                        case "UserName":

                            BLLgn.UserName = XMLText.ReadString();

                            break;

                         case "Password":

                            BLLgn.Password  = XMLText.ReadString();

                            break;

                    }

                }

            }

            XMLText.Close();

        }

    }


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend