Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 134
  • Last Modified:

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

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
RadhaKrishnaKiJaya
Asked:
RadhaKrishnaKiJaya
  • 6
  • 5
  • 2
1 Solution
 
Fernando SotoCommented:
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
 
RadhaKrishnaKiJayaAuthor Commented:
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
 
Fernando SotoCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RadhaKrishnaKiJayaAuthor Commented:
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
 
Fernando SotoCommented:
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
 
RadhaKrishnaKiJayaAuthor Commented:
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
 
David Johnson, CD, MVPOwnerCommented:
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
 
Fernando SotoCommented:
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
 
RadhaKrishnaKiJayaAuthor Commented:
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
 
Fernando SotoCommented:
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
 
RadhaKrishnaKiJayaAuthor Commented:
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
 
David Johnson, CD, MVPOwnerCommented:
it is technically invalid xml having multiple parents when you remove the outer <Barcode> tag.
0
 
Fernando SotoCommented:
@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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now