Hi,
I'm working on the .NET Compact Framework, and I need a way in which i store
products in an xml file. Basically, when i add a product to the system, i
want to either add it to the XML file, (if it exists), or create a new XML
file with the item in it if the file doesn't exist.
Bearing in mind I'm on the compact framework, does anyone have any
suggestions how i can check the file ?
Many thanks.
This is my code so far.
using System;
using System.Xml;
using System.IO;
using System.Text;
namespace SaveExample
{
/// <summary>
/// Summary description for save.
/// </summary>
public class Save
{
public static void savefile()
{
XmlDocument xmldoc = new XmlDocument();
XmlElement playersnode;
try
{
StreamReader xmlfile = new StreamReader("products.xml");
xmldoc.Load(xmlfile);
xmlfile.Close();
//get the products node
playersnode =xmldoc.CreateElement("","products","");
}
catch(System.IO.FileNotFoundException f){
//the file doesn't exist so we must create a new document
//let's add the XML declaration section
XmlNode xmlnode=xmldoc.CreateNode(XmlNodeType.XmlDeclarati on,"","");
xmldoc.AppendChild(xmlnode);
XmlNode players = xmldoc.CreateNode("","products","");
xmldoc.AppendChild(products);
//let's add the root element
playersnode =xmldoc.CreateElement("","products","");
}
//the products node
XmlElement product = xmldoc.CreateElement("product");
//the id
XmlElement id = xmldoc.CreateElement("id");
XmlText id_value = xmldoc.CreateTextNode("id text");
id.AppendChild(id_value);
product.AppendChild(id); //add the id to the product
xmldoc.Save("products.xml");
}
}
}