Solved

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

Posted on 2016-07-26
13
40 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 78

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 78

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

12 Experts available now in Live!

Get 1:1 Help Now