Hi,
i have 3 edittexts(eteno,etename,eteadd) and two buttons (btnsave and
btnretreive) when i click on save button the edittext values will save to
xml...the values are saving...bt my problem is retreiveing...
suppose i have data like this
101 john usa
102 james uk
103 robert india
now when i give 101 in first edittext(eteno) and click on retrieve button
related data john and usa should populate to edittexts etename and etadd
respectively...
plz help me ...its my life and death question....
Unless someone literally has a gun to your head or a knife to your
throat or about to dunk you head first into a bath of hot sulfuric acid,
it is not likely to be life or death ;)
btretreive.Click += delegate { retFromXml(eteno.Text); };
If eteno.Text is NULL or empty, then you're heading for a problem here.
Always program defensively - though it's not a nice thing to do, think
of your average user as a bit of a twit, so error trap...
btretreive.Click += delegate {
if (!string.IsNullOrEmpty(eteno.Text)
retFromXml(); // eteno is defined in a global scope
else
Toast.MakeToast(context, "I cannot retrieve nothing!",
ToastLength.Short).Show();
};
Here I'm testing for eteno.Text to be not null or empty. If the
condition is met, call the retFromXml. No need to pass anything in the
call as eteno is defined globally. The other two need not be defined
globally. Something like this will do the trick
btretreive.Click += delegate {
if (!string.IsNullOrEmpty(eteno.Text)
{
string[] retValues = new string[2];
retValues = retFromXml(); // eteno is defined in a global scope
etename.Text = retValue[0];
eteadd.Text = retValue[1];
}
else
Toast.MakeToast(context, "I cannot retrieve nothing!",
ToastLength.Short).Show();
};
No requirement for the additional globals. Only problem here is if you
expand the structure, you will need to expand the size of the array.
Possibly an easier way would be to use List<string>, but that's for
another time.
For reading back in...
private void retFromXml() or private string[] retFromXml()
depending on which way you want to play it.
{
string[] retVal = new string[2]; // no need for global way
XmlReader reader = XmlReader.Create(path);
while (reader.Read())
{
if (reader.NodeType != XmlNodeType.EndElement)
{
reader.Read();
if (reader.Name == eteno.Text)
{
while (reader.NodeType != XmlNodeType.EndElement)
{
reader.Read();
if (reader.NodeType == XmlNodeType.Text)
retVal[0] = reader.Value;
reader.Read();
if (reader.NodeType == XmlNodeType.Text)
retVal[1] = reader.Value;
}
break;
}
}
}
if (string.IsNullOrEmpty(retVal[0]) && string.IsNullOrEmpty(retVal[1])
{
string error = string.Format("The username {0} could not be found.",
eteno.Text);
Toast.MakeToast(context, error, ToastLength.Short).Show();
// then either for returning the value
retVal[0] = retVal[1] = "n/a";
// or for globals
eteadd.Text = etename.Text = "Not found";
return retVal; // not needed if it's a void
}
This should give you an idea on how to do it (I've not tested the code).
There are tonnes of websites out there that show you how to read/write
to XML, MSDN is a good start as is dot net perls. Here is not a bad
piece on it... http://www.all-the-johnsons.co.uk/csharp/xml.html
Paul
--
"Space," it says, "is big. Really big. You just won't believe how
vastly, hugely, mindbogglingly big it is. I mean, you may think it's a
long way down the road to the chemist's, but that's just peanuts to
space, listen..."
Hitch Hikers Guide to the Galaxy, a truly remarkable book!
_______________________________________________
Monodroid mailing list
Monodroid@lists.ximian.com
UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid