help with C# program output in xml file

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<pre>Hello,

Can someone please help me out on my program? When the program starts, the main form appears. The user will click Add to add member. Once they click the Add button, the new member form pops up. Then the user enters the member information on to the new form and then click the Save button. Once clicked, the main form will appear showing the members information in the box. My problem is when I click the Save button, there is no output in the main form. Its just blank. When start the program without debugging, I get the error: root element missing. Im not sure where my problem is and can someone help me fix it please. I will post what I have.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace prog07

{

public class Member

{

private string firstName;

private string lastName;

private string emailAddress;

private string phoneNumber;

private int memberID;


public Member() { }

//constructor

public Member(string firstName, string lastName,string emailAddress, string phoneNumber, int memberID)

{

this.FirstName = firstName;

this.LastName = lastName;

this.EmailAddress = emailAddress;

this.PhoneNumber = phoneNumber;

this.MemberID = memberID;

}




//property


public string FirstName

{

get

{

return firstName;

}

set

{

firstName = value;

}

}

public string LastName

{

get

{

return lastName;

}

set

{

lastName = value;

}

}


public string EmailAddress

{

get

{

return emailAddress;

}

set

{

emailAddress = value;

}

}

public string PhoneNumber

{

get

{

return phoneNumber;

}

set

{

phoneNumber = value;

}

}


public int MemberID

{

get

{

return memberID;

}

set

{

memberID = value;

}

}



//Equals method

public bool Equals(Member m)

{

if (m == null)

return false;

Member n = (Member)m;

if (this.phoneNumber == n.phoneNumber)

return true;

else

return false;

}

//toString method

public override string ToString()

{

return phoneNumber + "" + lastName + "" + firstName + "" + emailAddress;

}

//getHashCode

public override int GetHashCode()

{

string hashString = this.phoneNumber + this.lastName + this.firstName

+ this.emailAddress + this.memberID;

return base.GetHashCode();

}

public string GetDisplayText(string sep)

{

return firstName + sep + LastName + sep + emailAddress + sep + phoneNumber + sep + memberID.ToString("c");

}

public string GetDisplayText()

{

return firstName + ", " + lastName + ", " + emailAddress + ", " + phoneNumber + ", " + memberID.ToString("c");

}

}

}

-------------------------------------------------------------------------------------------------------



using System;

using System.Collections.Generic;

using System.Linq;

using System.Windows.Forms;

namespace prog07

{

static class Program

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new frmMemberMain());

}

}

}

-----------------------------------------------------------------------------------------



using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace prog07

{

public partial class frmMemberMain : Form

{

public frmMemberMain()

{

InitializeComponent();

}

private List<Member> members = null;

private void frmMemberMain_Load(object sender, System.EventArgs e)

{

members = MemberDA.GetMembers();

FillMemberListBox();

}

private void FillMemberListBox()

{

lstMembers.Items.Clear();

foreach (Member m in members)

{

lstMembers.Items.Add(m.GetDisplayText("t"));

}

}

private void btnAdd_Click(object sender, System.EventArgs e)

{

frmNewMember newMemberForm = new frmNewMember();

Member member = newMemberForm.GetNewMember();

if (member != null)

{

members.Add(member);

MemberDA.SaveMembers(members);

FillMemberListBox();

}

}

private void btnDelete_Click(object sender, System.EventArgs e)

{

int i = lstMembers.SelectedIndex;

if (i != -1)

{

Member member = (Member)members;

string message = "Are you want to delete " + member.FirstName + member.LastName + "?";

DialogResult button = MessageBox.Show(message, "Confirm Delete",

MessageBoxButtons.YesNo);

if (button == DialogResult.Yes)

{

members.Remove(member);

MemberDA.SaveMembers(members);

FillMemberListBox();

}

}

}

private void btnExit_Click(object sender, EventArgs e)

{

this.Close();

}



}

}

------------------------------------------------------------------------------



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Xml;

namespace prog07

{

public static class MemberDA

{

private const string path = @"C:UsersGolden87Desktopprog07prog07Members.xml";



public static List<Member> GetMembers()

{

//create the list

List<Member> members = new List<Member>();

//create the XmlReaderSettings object

XmlReaderSettings settings = new XmlReaderSettings();

settings.IgnoreWhitespace = true;

settings.IgnoreComments = true;

//create the XmlReader object

XmlReader xmlIn = XmlReader.Create(path, settings);

//read past all nodes to the first member node

if (xmlIn.ReadToDescendant("Member"))

{

//create one member object for each member node

do

{

Member member = new Member();

xmlIn.ReadStartElement("Member");

member.FirstName = xmlIn.ReadElementContentAsString();

member.LastName = xmlIn.ReadElementContentAsString();

member.EmailAddress = xmlIn.ReadElementContentAsString();

member.PhoneNumber = xmlIn.ReadElementContentAsString();

member.MemberID = xmlIn.ReadElementContentAsInt();

members.Add(member);

}

while (xmlIn.ReadToNextSibling("Member"));

}

//close the Xmlreader object

xmlIn.Close();

return members;

}




//save

public static void SaveMembers(List<Member> members)

{

//create the XmlWriterSettings object

XmlWriterSettings settings = new XmlWriterSettings();

settings.Indent = true;

settings.IndentChars = (" ");

//create the XmlWrite Object

XmlWriter xmlOut = XmlWriter.Create(path, settings);

//write the start of the document

xmlOut.WriteStartDocument();

xmlOut.WriteStartElement("Members");

//write each member object to the xml file

foreach (Member member in members)

{

xmlOut.WriteStartElement("Member");

xmlOut.WriteElementString("First Name", member.FirstName);

xmlOut.WriteElementString("Last Name", member.LastName);

xmlOut.WriteElementString("Email Address", member.EmailAddress);

xmlOut.WriteElementString("Phone Number", member.PhoneNumber);

xmlOut.WriteElementString("Member ID", member.MemberID.ToString());

xmlOut.WriteEndElement();

}

//write the end tag for the root element

xmlOut.WriteEndElement();

//close the xmlwrite object

xmlOut.Close();

}

}

}

-------------------------------------------------------------------------------------------------------------

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace prog07

{

public partial class frmNewMember : Form

{

public frmNewMember()

{

InitializeComponent();

}

private Member member = null;

public Member GetNewMember()

{

this.ShowDialog();

return member;

}


private void btnSave_Click(object sender, System.EventArgs e)

{

if (IsValidData())

{

member = new Member(txtFirst.Text, txtLast.Text, txtEmail.Text, txtPhone.Text, int.Parse(txtID.Text));

this.Close();

}

}

private bool IsValidData()

{

return Validator.IsPresent(txtFirst) &&

Validator.IsPresent(txtLast) &&

Validator.IsPresent(txtEmail) &&

Validator.IsPresent(txtPhone) &&

Validator.IsInt32(txtID);

}

private void btnCancel_Click(object sender, System.EventArgs e)

{

this.Close();

}



private void frmNewMember_Load(object sender, EventArgs e)

{

}

}

}


---------------------------------------------------------------------------------------------------------



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace prog07

{

public static class Validator

{

private static string memb = "Entry Error";

public static string Memb

{

get

{

return memb;

}

set

{

memb = value;

}

}

public static bool IsPresent(TextBox textBox)

{

if (textBox.Text == "")

{

MessageBox.Show(textBox.Tag + " is a required field.", Memb);

textBox.Focus();

return false;

}

return true;

}

public static bool IsInt32(TextBox textBox)

{

try

{

Convert.ToInt32(textBox.Text);

return true;

}

catch (FormatException)

{

MessageBox.Show(textBox.Tag + " must be an integer.", Memb);

textBox.Focus();

return false;

}

}

}

}



------------------------------------------------------------------------------------------------

this is the Members.xml

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

<Members />

----------------------------------------------------------------

Thank you for your time.



-labre87
[/code]

View the full article
 
Back
Top