473,385 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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("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");

}

}

}

Nov 17 '05 #1
0 922

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: eboogyman | last post by:
I have an HTML form with Firstname, lastname, address I want to save the firstname, lastname and address appended to a file in say results.txt, in the below format 1 entry per line... ...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
36
by: felixnielsen | last post by:
What i really wanna do, is defining my own types, it doesnt really matter why. Anyway, i have run into some problems 1) typedef unsigned short U16; U16 test = 0xffffffff; // There should be a...
2
by: -D- | last post by:
I'm taking my first stab at using xml, so please bear with my novice questions and understanding of xml. I'm trying to create an xml file that holds all my website navigation. If I understand...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.