Help: XML File Writing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric W

    Help: XML File Writing

    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("p roducts.xml");
    xmldoc.Load(xml file);
    xmlfile.Close() ;

    //get the products node
    playersnode =xmldoc.CreateE lement("","prod ucts","");
    }

    catch(System.IO .FileNotFoundEx ception f){
    //the file doesn't exist so we must create a new document

    //let's add the XML declaration section
    XmlNode xmlnode=xmldoc. CreateNode(XmlN odeType.XmlDecl aration,"","");

    xmldoc.AppendCh ild(xmlnode);
    XmlNode players = xmldoc.CreateNo de("","products ","");
    xmldoc.AppendCh ild(products);

    //let's add the root element
    playersnode =xmldoc.CreateE lement("","prod ucts","");

    }




    //the products node

    XmlElement product = xmldoc.CreateEl ement("product" );

    //the id

    XmlElement id = xmldoc.CreateEl ement("id");

    XmlText id_value = xmldoc.CreateTe xtNode("id text");

    id.AppendChild( id_value);

    product.AppendC hild(id); //add the id to the product

    xmldoc.Save("pr oducts.xml");



    }



    }

    }



Working...