How to read/write an XML file and sync it with a listbox in Visual Studio 2013 (C# code)?



I am creating a program that tracks users in a conference room. A user should be able to type their name in the nameTextBox, click the "Check In" button, and have their name pop up in the inMeetingListBox. In order to account for the webpage refreshing and inadvertently clearing the data, the people who are checked in need to be backed up in an XML file that can be accessed when subsequent users load the webpage. When a user leaves the conference room, they will click their name in the inMeetingListBox, and tap "Check Out", resulting in their name being erased. This will require finding the name in the attendees.xml file and removing it so that it does not erroneously appear in the future.


I have the basic functionality in place, where clicking "Check In" places the user's name in the list box, clicking "Check Out" removes the selected name, and clicking "Clear All" erases all names from the listbox. However, I could use some direction for how to implement the XML reading/writing/backup process. Any sample code implementations for how to write new users to the attendees.XML file and parsing the attendees.xml file to find and delete users who are checking out would be greatly appreciated. Thanks in advance for your ideas!


This is the Default.aspx.cs file:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls

namespace ConferenceRoomTracker
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void checkInButton_Click(object sender, EventArgs e)
{


inMeetingListBox.Items.Add(nameTextBox.Text);
}

protected void checkOutButton_Click(object sender, EventArgs e)
{
inMeetingListBox.Items.Remove(inMeetingListBox.SelectedItem);
}

protected void clearAllButton_Click(object sender, EventArgs e)
{
clearNameList();
}

public void clearNameList()
{
inMeetingListBox.Items.Clear();

}


}
}


This is the rudimentary Default.aspx page:





<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ConferenceRoomTracker.Default" %>

<!DOCTYPE html>

<html>
<head runat="server">
<title></title>
<style>
div{
background-color:#CDC9C9
}
iframe{
width:768px;
height:346px;
}
#header{
background-color:red;
width:768px;
height:200px;
}
#name{
text-align:center;
width:384px;
height:200px;
}
#time{
text-align:center;
width:384px;
height:200px;
}
#lower{
height:150px;
width:768px;
}

}
buttonzone{
width:384px;
height:500px;
}
listboxzone{
width:384px;
height:500px;
}
#inMeetingListBox{
margin-top: 0px;
width:384px;
height:500px;
}
.button{
height:160px;
width:384px;
display:inline-block;
}
#checkInButton{
width:384px;
height:160px;
background-color:green;
}
#checkOutButton{
width:384px;
height:160px;
background-color:red;
}
#clearAllButton{
width:384px;
height:160px;
background-color:blue;
}
.auto-style1 {
height: 500px;
}
.auto-style3 {
height: 500px;
width: 384px;
}
</style>

</head>
<body>
<form id="form1" runat="server">

<iframe src="https://www.aopa.org"></iframe>
<div id="calendar">
<div id="header">
<table>
<tr>
<td>
<div id="name">
Enter Name:<br />
<asp:TextBox ID="nameTextBox" runat="server"></asp:TextBox>
</div>
</td>
<td>
<div id="time">
Duration:<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
</td>
</tr>
</table>
</div>
<div>
<table>
<tr>
<td class="auto-style3">
<div class="button"><asp:Button ID="checkInButton" runat="server" OnClick="checkInButton_Click" Text="Check In" /></div>
<div class="button"><asp:Button ID="checkOutButton" runat="server" Text="Check Out" OnClick="checkOutButton_Click" /></div>
<div class="button"><asp:Button ID="clearAllButton" runat="server" Text="Clear All" OnClick="clearAllButton_Click" /> </div>
</td>
<td id="listboxzone" class="auto-style1">
<asp:ListBox ID="inMeetingListBox" runat="server" DataSourceID="XmlDataSource2"></asp:ListBox>
<asp:XmlDataSource ID="XmlDataSource2" runat="server" DataFile="~/App_Data/attendees.xml"></asp:XmlDataSource>
<asp:XmlDataSource ID="peopleInRoomRecord" runat="server" DataFile="~/App_Data/peopleInMeeting.xml"></asp:XmlDataSource>
<asp:XmlDataSource ID="XmlDataSource1" runat="server"></asp:XmlDataSource>
</td>
</tr>
</table>
</div>
</div>

</form>
</body>
</html>



Finally, this is the attendees.xml file being used for the inMeetingListBox backup:





<?xml version="1.0" encoding="utf-8" ?>
<attendee>
<name>Jake</name>
<name>James</name>
<name>Thomas</name>
<name>Tim</name>
</attendee>



No comments:

Post a Comment