Created
February 25, 2013 12:52
-
-
Save Cheesebaron/5029622 to your computer and use it in GitHub Desktop.
Just a sample for Krish on the MonoDroid mailing list.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Text; | |
using System.Xml; | |
using Android.App; | |
using Android.Widget; | |
using Android.OS; | |
namespace MonoDroid.ReadWriteXML | |
{ | |
[Activity(Label = "ReadWriteXML Test", MainLauncher = true, Icon = "@drawable/icon")] | |
public class Activity1 : Activity | |
{ | |
private EditText _id, _name, _description; | |
private TextView _xmlView; | |
private Button _save, _load; | |
private readonly string _path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "Empp.xml"); | |
protected override void OnCreate(Bundle bundle) | |
{ | |
base.OnCreate(bundle); | |
// Set our view from the "main" layout resource | |
SetContentView(Resource.Layout.Main); | |
_id = FindViewById<EditText>(Resource.Id.userId); | |
_name = FindViewById<EditText>(Resource.Id.name); | |
_description = FindViewById<EditText>(Resource.Id.description); | |
_xmlView = FindViewById<TextView>(Resource.Id.xmlview); | |
_save = FindViewById<Button>(Resource.Id.saveButton); | |
_load = FindViewById<Button>(Resource.Id.retrieveButton); | |
_save.Click += SaveOnClick; | |
_load.Click += LoadOnClick; | |
} | |
private void LoadOnClick(object sender, EventArgs eventArgs) | |
{ | |
var doc = new XmlDocument(); | |
doc.Load(_path); | |
XmlNode root = doc.DocumentElement; | |
var sb = new StringBuilder(); | |
var nodeList = root.SelectNodes("Employee"); | |
foreach (XmlNode node in nodeList) | |
{ | |
sb.Append(string.Format("ID: {0} ", node.SelectSingleNode("Id").InnerText)); | |
sb.Append(string.Format("Name: {0} ", node.SelectSingleNode("Name").InnerText)); | |
sb.Append(string.Format("Description: {0} ", node.SelectSingleNode("Description").InnerText)); | |
} | |
_xmlView.Text = sb.ToString(); | |
} | |
private void SaveOnClick(object sender, EventArgs eventArgs) | |
{ | |
var doc = new XmlDocument(); | |
if (File.Exists(_path)) | |
doc.Load(_path); | |
else | |
doc.InnerXml = "<Employees></Employees>"; | |
var newElem = doc.CreateElement("Employee"); | |
newElem.InnerXml = "<Id></Id><Name></Name><Description></Description>"; | |
newElem["Id"].InnerText = _id.Text; | |
newElem["Name"].InnerText = _name.Text; | |
newElem["Description"].InnerText = _description.Text; | |
doc.DocumentElement.SelectNodes("/Employees")[0].AppendChild(newElem); | |
doc.PreserveWhitespace = true; | |
using (var wrtr = new XmlTextWriter(_path, Encoding.Unicode)) | |
{ | |
doc.WriteTo(wrtr); | |
} | |
_id.Text = _name.Text = _description.Text = ""; | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="internalOnly" package="monodroid.readwritexml" android:versionCode="1" android:versionName="1.0"> | |
<uses-sdk android:targetSdkVersion="14" /> | |
<application android:label="ReadWrite XML"> | |
</application> | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | |
<uses-permission android:name="android.permission.WRITE_OWNER_DATA" /> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent"> | |
<TextView | |
android:text="Add User" | |
android:textAppearance="?android:attr/textAppearanceMedium" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/textView1" /> | |
<EditText | |
android:inputType="number" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/userId" | |
android:hint="ID" /> | |
<EditText | |
android:inputType="textPersonName" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/name" | |
android:hint="Name" /> | |
<EditText | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/description" | |
android:hint="Description" /> | |
<Button | |
android:text="Save User" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/saveButton" /> | |
<Button | |
android:text="Retrieve Users" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/retrieveButton" /> | |
<TextView | |
android:text="Text" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:id="@+id/xmlview" /> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment