View Full Version : C# Ramblings
Animal
06-25-2008, 01:02 AM
I'm taking on a pet project using C# and XML to store data for a "fitness diary". The first big item I need to get out of the way is reading, writing, and navigating the data stored in XML files.
Initially I was playing around with the System.Xml.XmlTextReader (http://msdn.microsoft.com/en-us/library/system.xml.xmltextreader.aspx) class. However, per MSDN, this particular class is only useful if you are trying to read the XML file sequentially.
For now, the System.Xml.Document (http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx) class will have to do. There is a good possibility that the System.Xml.DataDocument (http://msdn.microsoft.com/en-us/library/system.xml.xmldatadocument.aspx) will also be used or may even replace the use of System.Xml.Document (http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx) altogether since it is more closely tied with DataSets.
Nebula
06-25-2008, 08:45 AM
Oh noesss HELP!
SOME SPAMMER STOLE AR's LOGIN INFO! :wink:
Animal
06-25-2008, 11:41 AM
Bah, I just need some place to keep my crap together. I suppose I could use a text file or something on my computer but this is better. Once I have enough information, I'm gonna write up a short tutorial and put it on my blog.
papa smurf
06-25-2008, 12:22 PM
i dont get what your asking? :doubt:
are you looking for a place to store your documents?
HHBizzle
06-25-2008, 03:04 PM
google docs on line
my trainer keeps my workouts there for me to see
papa smurf
06-25-2008, 03:30 PM
google docs on line
my trainer keeps my workouts there for me to see o0o0oh that kinda stuff, i use pastebin :wink:
Animal
06-25-2008, 10:58 PM
No, I deliberately posted it here. This is a public place but wont necessarily be viewed as spam. Those that wish to comment on my code or thought process may feel free to do so. Others can simply ignore the thread or just come in and poke fun at me! :lol:
Animal
06-28-2008, 04:20 AM
Consider the following XML:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE user [
<!ATTLIST profile id ID #REQUIRED>
]>
<user>
<profile id="0">
<name>Mickey Mouse</name>
<birthday>74</birthday>
<height>73</height>
<weight>275</weight>
<goal>250</goal>
</profile>
<profile id="1">
<name>Joe Smith</name>
<birthday>74</birthday>
<height>68</height>
<weight>230</weight>
<goal>200</goal>
</profile>
</user>
We can populate a listview to look as follows:
http://www.thegng.org/forums/attachment.php?attachmentid=1895&stc=1&d=1214704381
The code to accomplish this:
//obtain the path to "my documents"
//string getFitXML = Environment.GetFolderPath(Environment.SpecialFolde r.Personal).ToString();
string getFitXML = "E:/Documents/Visual Studio 2008/Projects/GetFit/GetFit/profiles.xml";
//check to see if the XML file exists
if (File.Exists(getFitXML))
{
XmlTextReader rdrProfileID = new XmlTextReader(getFitXML);
//rdrProfileID.WhitespaceHandling = WhitespaceHandling.None;
int i = new int();
i = 0;
rdrProfileID.MoveToContent();
while (rdrProfileID.Read())
{
if ((rdrProfileID.Name == "profile") && (rdrProfileID.HasAttributes))
{
lvwProfiles.Items.Add(rdrProfileID.GetAttribute("id"));
}
if ((rdrProfileID.Name == "profile") && (rdrProfileID.NodeType == XmlNodeType.EndElement))
{
i++;
}
switch (rdrProfileID.NodeType)
{
case XmlNodeType.Text:
lvwProfiles.Items[i].SubItems.Add(rdrProfileID.Value);
break;
}
}
}
else
MessageBox.Show("The " + getFitXML + " file was not found");
That code will add as many profiles as there are. It's very crude, error prone, and if you change the structure of the XML, you are going to break it. Still, it's a good foundation to build on.
Animal
06-28-2008, 10:00 PM
Manipulate XML data with XPath and XmlDocument (C#) (http://www.codeproject.com/KB/cpp/myXPath.aspx)
vBulletin® v3.7.2, Copyright ©2000-2008, Jelsoft Enterprises Ltd.