using System.Xml.Linq;
using System.IO.IsolatedStorage;
using System.IO;
using System.Linq;
private void CreateXdoc()
{
_xdoc = new XDocument(
new XComment("Created " + DateTime.Now.ToLongTimeString()),
new XElement("Root",
new XElement("Child", "data1",new XAttribute("attr", 1)),
new XElement("Child", "data2", new XAttribute("attr", 2)),
new XElement("Child", "data3", new XAttribute("attr", 3)),
new XElement("Child", "data4", new XAttribute("attr", 4))
)
);
ShowXDoc();
}
private void ReadXDoc()
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
IsolatedStorageFileStream stream = store.OpenFile("MyXmlFile.Xml", FileMode.Open, FileAccess.Read);
_xdoc = XDocument.Load(stream);
ShowXDoc();
stream.Dispose();
}
}
private void SaveXDoc()
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
IsolatedStorageFileStream stream = store.OpenFile("MyXmlFile.Xml", FileMode.Create, FileAccess.Write);
_xdoc.Save(stream);
stream.Dispose();
}
MessageBox.Show("Saved");
}
private void AddChild()
{
if (_xdoc == null)
{
MessageBox.Show("Please create doc first");
return;
}
int nextNumber = _xdoc.Element("Root").Elements("Child").Count()+1;
XElement newChild = new XElement("Child","data" + nextNumber );
newChild.Add(new XAttribute("attr", nextNumber));
_xdoc.Element("Root").Add(newChild);
ShowXDoc();
}
private void ShowXDoc()
{
TextBox1.Text = _xdoc.ToString();
}
private void ModifyXDoc()
{
var item = from c in _xdoc.Element("Root").Elements("Child")
where c.Attribute("attr").Value == TextBox2.Text
select c;
item.Remove();
ShowXDoc();
}
Not able to access xml file , where should we keep this xml file ?On our disc or xml file.
ReplyDelete