Solved

Write data from textbox to dataset and then to an XML file

Posted on 2016-07-26
13
52 Views
Last Modified: 2016-07-27
Hi Experts,

I have created a dataset with 2 fields. ID and Barcode. On the form I have created 2 textbox for ID and barcode. When I click the save button I want to save the values of the textboxes into an XML file thru the dataset.

I know how to do it by generating from the dataset by setting the  'Detail' and then drag it to the form. But I do not want that. I want to create the textboxes separately and then do the operation.

Thanks in advance.
0
Comment
Question by:RadhaKrishnaKiJaya
  • 6
  • 5
  • 2
13 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41729812
What do you mean by this, " I want to save the values of the textboxes into an XML file thru the dataset"?

What is the data type of the two columns in the dataset?
What is the schema of the XML file you are working with?
0
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 41729821
Hi,

Actually I want to write data to an XML file when Save button is clicked. I have ID and Barcode. ID is integer and Barcode is string. I need to create an XML file if it is not there and if it is there the add the data into it. Every time I click Save, the data should be appended to the XML file not overwrite it.

Thank you.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41729848
Well the XML has a schema defined by the application / developer, so what is the layout of your XML and which node in that XML will these new nodes get added to.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:RadhaKrishnaKiJaya
ID: 41729862
I want something like this. But the ID and the barcode should come from the textboxes when I click Save.

<?xml version="1.0" standalone="yes"?>
<XMLDataSet xmlns="http://tempuri.org/XMLDataSet.xsd">
  <Barcodes>
    <ID>0</ID>
    <Barcode>http://www.kaspersky.com/pure</Barcode>
  </Barcodes>
  <Barcodes>
    <ID>1</ID>
    <Barcode>www.rogersparkfoodcoop.com</Barcode>
  </Barcodes>
  <Barcodes>
    <ID>3</ID>
    <Barcode>Test</Barcode>
  </Barcodes>
</XMLDataSet>
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41729982
Hi RadhaKrishnaKiJaya;

The code snippet below will add to an existing XML file or create a new one if none exists.
using System.Xml.Linq;

// Save button click event handler
private void Save_Click( object sender, EventArgs e ) {
	  // Location of the XML file on the file system. Name without path info file is in same directory as executable
    string filename = "MyData.xml";
    // The XML namespace used in the XML document
    XNamespace ns = "http://tempuri.org/XMLDataSet.xsd";
    // Check to see if the XML document exists.
    if (File.Exists(filename)) {
        // Load the XML in to memory
        XDocument xdoc = XDocument.Load(filename);
        // Create the new nodes to be inserted
        XElement newBarcode = new XElement("Barcode", 
            new XElement("ID", int.Parse(txtID.Text)),
            new XElement("Barcode", txtBarcode.Text)
            );
        // Add the new nodes to the end of the root node
        xdoc.Root.Element("Barcodes" ).Add( newBarcode );
        // Save the XML to the same file on disk
        xdoc.Save( filename );
    } else {
        // Create a new XML document with a new node
        XDocument xdoc = new XDocument(
            new XDeclaration("1.0", "utf-8", "yes"),
            new XElement(ns + "XMLDataSet",
                new XElement("Barcodes",
                    new XElement("Barcode",
                        new XElement("ID", int.Parse(txtID.Text)),
                        new XElement("Barcode", txtBarcode.Text)
                        )))
            );
        // Save the new XML document to the file system
        xdoc.Save( filename );
    }
}

Open in new window

0
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 41730229
Hi Mr. Soto,

Thank you for trying to help me. I have few small issues.

I do not need the outer <Barcode> tag.
I need exactly like this.

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<XMLDataSet xmlns="http://tempuri.org/XMLDataSet.xsd">
  <Barcodes>
    <ID>0</ID>
    <Barcode>Test1</Barcode>
  </Barcodes>

  <Barcodes>
    <ID>1</ID>
    <Barcode>Test2</Barcode>
  </Barcodes>
</XMLDataSet>

I tried to remove it but messed it up. Please help.

Thanks a lot in advance for your time.
0
 
LVL 79

Expert Comment

by:David Johnson, CD, MVP
ID: 41730280
nopoints:
Yes you do need the <Barcode> </Barcode> to make a proper xml file single parent, children,grandchildren.  You seem to want multiple parents
Fernando's code works both with a new file and appending to an existing file.
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<XMLDataSet xmlns="http://tempuri.org/XMLDataSet.xsd">
  <Barcodes xmlns="">
    <Barcode>
      <ID>1</ID>
      <Barcode>Test 1</Barcode>
    </Barcode>
    <Barcode>
      <ID>2</ID>
      <Barcode>Test 2</Barcode>
    </Barcode>
  </Barcodes>
</XMLDataSet>

Open in new window

0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41730329
Hi RadhaKrishnaKiJaya;

I made the changes you asked for in the code snippet below.
private void Save_Click( object sender, EventArgs e ) {
    string filename = "MyData.xml";
    XNamespace ns = "http://tempuri.org/XMLDataSet.xsd";
    if (File.Exists(filename)) {
        XDocument xdoc = XDocument.Load(filename);
        XElement newBarcode = new XElement("Barcodes", 
            new XElement("ID", int.Parse(txtID.Text)),
            new XElement("Barcode", txtBarcode.Text)
            );
        xdoc.Root.Add( newBarcode );
        xdoc.Save( filename );
    } else {
        XDocument xdoc = new XDocument(
            new XDeclaration("1.0", null, "yes"),
            new XElement(ns + "XMLDataSet",
                new XElement("Barcodes",
                    new XElement("ID", int.Parse(txtID.Text)),
                    new XElement("Barcode", txtBarcode.Text)
                    ))
            );
        xdoc.Save( filename );
    }
}

Open in new window

0
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 41731412
Hi Mr. Soto,

I think I am almost there but if you can help me with this issue. I am getting the Barcodes tag like this :  <Barcodes xmlns="">. But I do not need the xmlns="" with the barcode tag. How can I remove that.

Thanks


<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<XMLDataSet xmlns="ALLBARCODES">
  <Barcodes xmlns="">
    <ID>0</ID>
    <Barcode>t1</Barcode>
  </Barcodes>
</XMLDataSet>
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41731495
Sorry about that, I missed something when I modified the code. Try this
private void Save_Click( object sender, EventArgs e ) {
    string filename = "MyData.xml";
    XNamespace ns = "http://tempuri.org/XMLDataSet.xsd";
    if (File.Exists(filename)) {
        XDocument xdoc = XDocument.Load(filename);
        XElement newBarcode = new XElement(ns + "Barcodes", 
            new XElement(ns + "ID", int.Parse(txtID.Text)),
            new XElement(ns + "Barcode", txtBarcode.Text)
            );
        xdoc.Root.Add( newBarcode );
        xdoc.Save( filename );
    } else {
      XDocument xdoc = new XDocument(
                          new XDeclaration("1.0", null, "yes"),
                          new XElement(ns + "XMLDataSet",
                              new XElement(ns +"Barcode",
                                  new XElement(ns + "ID", int.Parse(txtID.Text)),
                                  new XElement(ns + "Barcode", txtBarcode.Text)
                                  ))
                          );
        xdoc.Save( filename );
    }
}

Open in new window

0
 

Author Closing Comment

by:RadhaKrishnaKiJaya
ID: 41731537
Thank you very much for your wonderful help. I think I can manage with that space. I am opening another thread . I want to search a barcode and delete a barcode. Please help me with that if you can.

Thanks a lot.
0
 
LVL 79

Expert Comment

by:David Johnson, CD, MVP
ID: 41732201
it is technically invalid xml having multiple parents when you remove the outer <Barcode> tag.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41732209
@David

This is the XML document created by the code.
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<XMLDataSet xmlns="http://tempuri.org/XMLDataSet.xsd">
  <Barcode>
    <ID>0</ID>
    <Barcode>Test 0</Barcode>
  </Barcode>
  <Barcodes>
    <ID>1</ID>
    <Barcode>Test 1</Barcode>
  </Barcodes>
  <Barcodes>
    <ID>2</ID>
    <Barcode>Test 2</Barcode>
  </Barcodes>
</XMLDataSet>

Open in new window

The root node being XMLDataSet and it has child nodes called Barcodes which has child nodes ID and Barcode. This is a well formed XML document and if it were not when adding nodes to the XML document XDocument class would have thrown an exception because it would not allow you to create a invalid XML.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Where did System.Data.Objects go? 2 23
start a process from a service 3 21
ASP/VB email question 4 30
Please explain the parts of these 2 LINQ expressions 3 26
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now