?
Solved

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

Posted on 2016-07-26
13
Medium Priority
?
95 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 63

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 63

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 63

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 82

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 63

Accepted Solution

by:
Fernando Soto earned 2000 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 63

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 82

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 63

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

Technology Partners: 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!

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 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