Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. help in creating a xml

help in creating a xml

Scheduled Pinned Locked Moved C#
xmldatabasehelptutorial
3 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dhol
    wrote on last edited by
    #1

    I have did a program such that , i will have two text box name and age in my form..and a button..so that when the values entered r in the text box..a xml file is created with those data... and my coding is as follows. Inside the burtton click, th e coding is as follows //create a dataset DataSet dataSet = new DataSet(); //read the schema dataSet.ReadXmlSchema("d:\\Menu.xsd"); //create a new row DataRow newrow; newrow = dataSet.Tables[0].NewRow(); //enter the values newrow[0] = Convert.ToInt32(txtName.Text); newrow[1] = Convert.ToInt32(txtAge.Text); //add the row to the dataset dataSet.Tables[0].Rows.Add(newrow); //write the data to a xml file dataSet.WriteXml("D:\\myData.xml", XmlWriteMode.WriteSchema); MessageBox.Show("saved"); Now i have one doubt.. ie, the program which i did ..now ..in the form...when i am giving data as for name: john age:22... now myData.xml wil be saved with those data ... Now if i am clearing those vaues in th textbox and adding some other data... that should be added without deleting the previous data... and also if i am entering again john..it should show that already it exists.... please say how to do this...

    J 1 Reply Last reply
    0
    • D dhol

      I have did a program such that , i will have two text box name and age in my form..and a button..so that when the values entered r in the text box..a xml file is created with those data... and my coding is as follows. Inside the burtton click, th e coding is as follows //create a dataset DataSet dataSet = new DataSet(); //read the schema dataSet.ReadXmlSchema("d:\\Menu.xsd"); //create a new row DataRow newrow; newrow = dataSet.Tables[0].NewRow(); //enter the values newrow[0] = Convert.ToInt32(txtName.Text); newrow[1] = Convert.ToInt32(txtAge.Text); //add the row to the dataset dataSet.Tables[0].Rows.Add(newrow); //write the data to a xml file dataSet.WriteXml("D:\\myData.xml", XmlWriteMode.WriteSchema); MessageBox.Show("saved"); Now i have one doubt.. ie, the program which i did ..now ..in the form...when i am giving data as for name: john age:22... now myData.xml wil be saved with those data ... Now if i am clearing those vaues in th textbox and adding some other data... that should be added without deleting the previous data... and also if i am entering again john..it should show that already it exists.... please say how to do this...

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      Before writing to the XML file, read the existing one into a DataSet. Before writing a new name & age to the XML file, first make sure its not in the dataset. If it is, show a box saying it already exists. That said, if you're going to be storing lots & lots of data like this, I recommend you use a database rather than XML files. XML files are good for configuration, settings, storing discreet, concise bits of information in a human readable format. If that's not what your goals are, you should choose another tool such as a database to store your data.

      D 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        Before writing to the XML file, read the existing one into a DataSet. Before writing a new name & age to the XML file, first make sure its not in the dataset. If it is, show a box saying it already exists. That said, if you're going to be storing lots & lots of data like this, I recommend you use a database rather than XML files. XML files are good for configuration, settings, storing discreet, concise bits of information in a human readable format. If that's not what your goals are, you should choose another tool such as a database to store your data.

        D Offline
        D Offline
        dhol
        wrote on last edited by
        #3

        Hi I have did a program such that , i will have two text box name and age in my form..and a button named save. so that when the values entered r in the text box..a xml file is created with those data... Now I can able to create one set of values…. No what I need is that when again some other value is entered in the textbox. It should also be written in the xml file..but without deleting the other. I mean it should append on the other.. For example: first I entered forst as john and 22 for name and age ..it will added in the xml file..no again if I enter charu and 23 as name and age.. Now xml should have john and 22 as well as charu and 23… And also one more thing is that if I again enter John. It should give a message that it has already been entered. No values should be repeated again. Please help me to do this….. The coding which I did is as follows. In side the button click //save Button private void button1_Click(object sender, System.EventArgs e) { DataSet dataSet = new DataSet(); //read the schema dataSet.ReadXmlSchema("..\\..\\PlaygroMenu.xsd"); //create a new row DataRow oValues; oValues = dataSet.Tables[0].NewRow(); //enter the values oValues[0] = txtName.Text; oValues[1] = Convert.ToInt32(txtAage.Text); //check for duplication bool hasalready = false; foreach(DataRow row in dataSet.Tables[0].Rows) { if(row.ItemArray[0].ToString().Equals(txtID.Text)) { hasalready = true; } } // if(!hasalready) { //add the row to the dataset dataSet.Tables[0].Rows.Add(oValues); //write the data to a xml file dataSet.WriteXml("..\\..\\resultdata.xml", XmlWriteMode.WriteSchema); MessageBox.Show("Saved"); } else { MessageBox.Show("Already Exists"); } } private void Form1_Load(object sender, System.EventArgs e) { DataSet dataSet = new DataSet(); //read the schema dataSet.ReadXmlSchema("..\\..\\Menu.xsd"); System.IO.FileInfo file = new System.IO.FileInfo("..\\..\\mydata.xml"); //check whethere there is result xml file... if (file.Exists) { dataSet.ReadXml("..\\..\\resultdata.xml"); } } Please help me to do this.. Dhol

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups