Ok, here's a relatively easy one for all you experienced C# coder's out there. I am making a small address book application and I write data to a .txt (.dat) file in this format:
Last Name, First Name" "Street Address" "City" "State" "Zip Code" "(Area Code)-Phone Number"
The quotation marks dont actually exist, but they are there for this example to show the single spaces I have between the data blocks to aid reading.
I was wondering how exactly I could read that data individually and restore the data to respective variables for display? I want to read the Last Name, place it in a variable, followed by the First Name, place it into a variable, and so on and so forth.
Im not even really sure how to go about it so any help I could get would be much appreciated. Im building this application to put in my portfolio for an internship and Im basically teaching myself :) Thank you!
-Scrap
Loading
Posted Jun 6, 2007, 4:14 PM
Ok, so heres what Ive come up with. Ive gotten the writing part down so I am now able to instruct my program how to write to the xml document. But I used the example you gave me and it just displays "System.Xml.XmlElement" in the list box instead of displaying the name. Kinda weird and Im not sure why. Heres the code Im using:
private void loadFileToolItem_Click(object sender, EventArgs e)
{
XmlDocument infoDoc = new XmlDocument(); try{
infoDoc.Load(
"infoData.xml"); XmlNode info = infoDoc.SelectSingleNode("//record[name = 'Lewis, Brandon']");clientListBox.Items.Add(info);
}
catch{
MessageBox.Show("Operation failed.", "SysApp", MessageBoxButtons.OK, MessageBoxIcon.Information);};
}
Ive been messing with it for awhile and cant figure it out. And one more question, how do you "append" to an xml file? Like if I want to add another record to the records element. All this XML stuff is pretty new to me and Im just trying to get the basic's down before I move on to anything else, lol. Thank you very much for all the help!
-Scrap
EDIT:
Nevermind, I figured it out, lol
Mike GoldPosted Jun 6, 2007, 1:07 PM
Posted Jun 6, 2007, 11:37 AM
-Scrap
Mike GoldPosted Jun 6, 2007, 2:55 AM
If you are going to treat this more as a database, you may want to consider using an XML format for your file. That way you can easily search on nodes such as Last Name and First Name using XPath. So you could have a structure like:
xml version="1.0" encoding="utf-8" ?>
144 Byron Way
...
144 Byron Way
...
You can use the System.Xml namespace which contains the XmlDocument class to do most of your work including Saving, Loading, and Searching on names. For Example use SelectNode with an XPath expression to find a last name.
XmlDocument doc;
doc.Load("c:\\myRecords.xml");
XmlNode node = doc.SelectSingleNode("//Record[Last = 'Smith']");
Check out
http://www.w3schools.com/xpath/ if you are new to XPath
-Mike
Posted Jun 6, 2007, 1:25 AM
"LastName, FirstName"
And that is all it is to contain. Problem is, Im not quite sure how to get that and just that out of the text file. Once I do that, Ill be able to continue on and have it to where when you click on the persons name, it searches the file for that persons name and then displays their information. That is the part I needed the individual variables assigned for. My summary:
How do I get just the first and last name out of the file?
How do I move it down a line and so on and so forth so it will read all of the persons names in the file?
Im sure itll come to me eventually, but Ive been hacking at it for hours and to pretty much no avail. Well thanks for any help I can get!
-Scrap
Mike GoldPosted Jun 5, 2007, 5:18 PM
Best,
Posted Jun 5, 2007, 2:36 PM
-Scrap
EDIT:
I plan on doing something like this:
name = allFields[0];
address = allFields[1];
city = allFields[2];
And so on, Ive got it down. Thanks again :)
Mike GoldPosted Jun 5, 2007, 2:10 PM
First of all you should write the data out either tab delimited or comma delimited. If you have commas in your data, you should probably use a tab. I would use the System.IO namespace's
StreamReader and StreamWriter.
Below is a quick example of how this might be used for a single line in a file:
using
System;using
System.Collections.Generic;using
System.Text;using
System.IO;namespace
ReadWriteApp{
class Program{
private static string name = "Howard"; private static string address = "444 Mulberry Street"; private static string telephone = "212-445-4343"; private static string fax = "212-433-4322"; static void Main(string[] args){
StreamWriter sw = new StreamWriter(@"c:\myfile.txt"); // write out each line of data like this: sw.WriteLine("{0}\t{1}\t{2}\t{3}", name, address, telephone, fax); sw.Close(); StreamReader sr = new StreamReader(@"c:\myfile.txt"); // read each line like this string nextLine; nextLine = sr.ReadLine(); sr.Close(); string[] allFields = nextLine.Split('\t'); // split the tab delimited line Console.WriteLine("name ={0}, address={1} telephone={2} fax={3}", allFields[0], allFields[1], allFields[2], allFields[3]); Console.WriteLine("Hit any key to continue..."); Console.ReadLine();}
}
}
Ok, here's a relatively easy one for all you experienced C# coder's out there. I am making a small address book application and I write data to a .txt (.dat) file in this format:
Last Name, First Name" "Street Address" "City" "State" "Zip Code" "(Area Code)-Phone Number"
The quotation marks dont actually exist, but they are there for this example to show the single spaces I have between the data blocks to aid reading.
I was wondering how exactly I could read that data individually and restore the data to respective variables for display? I want to read the Last Name, place it in a variable, followed by the First Name, place it into a variable, and so on and so forth.
Im not even really sure how to go about it so any help I could get would be much appreciated. Im building this application to put in my portfolio for an internship and Im basically teaching myself :) Thank you!
-Scrap
Todd VancePosted Jun 5, 2007, 2:03 PM
Check out this article on the Tokenizer here :
this should get you started.
http://www.c-sharpcorner.com/UploadFile/pseabury/JavaLikeStringTokenizer11232005015829AM/JavaLikeStringTokenizer.aspx