473,796 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Update a xml doc using windows forms and C# HELP!

I'm new to the .NET community. My name is BJ and I've been
developing web applications for years now. I'm very new
to .NET and could use some assistance. I don't think I'm
asking for very much here. This should be very common but
I haven't been able to find any concrete examples.
I'm creating a Windows Forms client/server application.
It's for sales staff and the client piece needs to store
to xml (preferrably). I know that windows programming
lends itself to this type of functionality but I have to
say I'm at a loss when it comes to finding out how to make
it work right.
I have a customer form page in the app that the user can
fill out. When the user clicks the save button I use an
event driven method that will write the form data to xml.
In the form of:
<Contacts>
<contact name="clientNam e" date="mm/dd/yyyy" />
</Contacts>
The problem is everytime I use the form it just appends
the same format to my xml document like so:
<Contacts>
<contact name="clientNam e" date="mm/dd/yyyy" />
</Contacts><Conta cts>
<contact name="clientNam e" date="mm/dd/yyyy" />
</Contacts>
This makes it unreadable.
QUESTION: How do I make the child elements append inside
the root "Contacts"?
Here's my current code that causes the problem.

//Code snippet starts here.....
System.Xml.XmlD ocument salesText = new XmlDocument();
XmlElement newContacts =
salesText.Creat eElement("Conta cts");
XmlElement newContact =
salesText.Creat eElement("Conta ct");
salesText.Appen dChild(newConta cts);
newContacts.App endChild
(newContact);
//Create a new XmlAttribute
XmlAttribute idAttribute =
salesText.Creat eAttribute("Cli ent");
idAttribute.Val ue =
this.textBox1.T ext;
//Set the Attribute on the
XmlElement
newContact.SetA ttributeNode
(idAttribute);
//Date Attribute
XmlAttribute dateAttribute =
salesText.Creat eAttribute("Dat e");
dateAttribute.V alue =
this.textBox2.T ext;
newContact.SetA ttributeNode
(dateAttribute) ;

//Insert the newly created
XmlElement into the XmlDocument
salesText.Inser tAfter
(newContacts,sa lesText.LastChi ld);

//Create a FileStream and Save the
Document
FileStream docOut =
new FileStream
(docPath,FileMo de.Append,FileA ccess.Write,Fil eShare.ReadWri
te);
salesText.Save( docOut);
//code snippet ends here.

Any help much appreciated...
Nov 15 '05 #1
1 4128
In your code snippet, you are using FileMode.Append . This is what is
causing your output file to become invalid XML.
FileStream docOut = new FileStream(docP ath,FileMode.Ap pend,FileAccess .Write,FileShar e.ReadWrite); salesText.Save( docOut);
Change the first line above to FileMode.Create or FileMode.Write

HTH,
--- Nick

"BJ Allmon" <an*******@disc ussions.microso ft.com> wrote in message
news:15******** *************** *****@phx.gbl.. . I'm new to the .NET community. My name is BJ and I've been
developing web applications for years now. I'm very new
to .NET and could use some assistance. I don't think I'm
asking for very much here. This should be very common but
I haven't been able to find any concrete examples.
I'm creating a Windows Forms client/server application.
It's for sales staff and the client piece needs to store
to xml (preferrably). I know that windows programming
lends itself to this type of functionality but I have to
say I'm at a loss when it comes to finding out how to make
it work right.
I have a customer form page in the app that the user can
fill out. When the user clicks the save button I use an
event driven method that will write the form data to xml.
In the form of:
<Contacts>
<contact name="clientNam e" date="mm/dd/yyyy" />
</Contacts>
The problem is everytime I use the form it just appends
the same format to my xml document like so:
<Contacts>
<contact name="clientNam e" date="mm/dd/yyyy" />
</Contacts><Conta cts>
<contact name="clientNam e" date="mm/dd/yyyy" />
</Contacts>
This makes it unreadable.
QUESTION: How do I make the child elements append inside
the root "Contacts"?
Here's my current code that causes the problem.

//Code snippet starts here.....
System.Xml.XmlD ocument salesText = new XmlDocument();
XmlElement newContacts =
salesText.Creat eElement("Conta cts");
XmlElement newContact =
salesText.Creat eElement("Conta ct");
salesText.Appen dChild(newConta cts);
newContacts.App endChild
(newContact);
//Create a new XmlAttribute
XmlAttribute idAttribute =
salesText.Creat eAttribute("Cli ent");
idAttribute.Val ue =
this.textBox1.T ext;
//Set the Attribute on the
XmlElement
newContact.SetA ttributeNode
(idAttribute);
//Date Attribute
XmlAttribute dateAttribute =
salesText.Creat eAttribute("Dat e");
dateAttribute.V alue =
this.textBox2.T ext;
newContact.SetA ttributeNode
(dateAttribute) ;

//Insert the newly created
XmlElement into the XmlDocument
salesText.Inser tAfter
(newContacts,sa lesText.LastChi ld);

//Create a FileStream and Save the
Document
FileStream docOut =
new FileStream
(docPath,FileMo de.Append,FileA ccess.Write,Fil eShare.ReadWri
te);
salesText.Save( docOut);
//code snippet ends here.

Any help much appreciated...

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1814
by: Stuart Norris | last post by:
Dear Group, I am attempting to write a "splash" and "status" Form using a second thread. I wish to use this Form to display status information to the user when I do CPU intensive work in my GUI during startup. I wish to also use the same Form after startup as well if I do something CPU intensive. I have developed a multi-threaded application however the very first
9
4778
by: Pam Ammond | last post by:
I need the code to update the database when Save is clicked and a text field has changed. This should be very easy since I used Microsoft's wizards for the OleDBAdapter and OleDBConnection, and DataSet; and all I'm doing is showing one record in text fields, allowing the user to modify the text fields, and then updating the database again when the user clicks the Save button. The fields already show the correct data record since I have...
2
2877
by: Joe Fetters via .NET 247 | last post by:
Have googled and read the VS.NET documentation can't seem to getthe answer to the following. Environment: Framework 1.1 VB.NET WinForm Access database Using all automagic tools (DataAdapter Wizard with generatedDataset and generated Select, Update, Insert and Deletecommands, controls bound to DataSet using the AdvancedDatabinding properties) Issue: Do I have to set parameters before I issue theDataSet.Update(Dataset, )? Again, my...
1
1858
by: Greg | last post by:
I have code which creates a DataAdapter, and upddate command etc, and then finally does and update which fails. The error message which is returned is "Object Reference not set to an instance of an object." The update SQL is "Update Leads set Company_Name = ? where LeadId = ?" If I change it to: "Update Leads set Company_Name = ? where LeadID = 5" no error is thrown and the record is updated. The first thought of course is that...
5
5249
by: Danny Tuppeny | last post by:
I've been playing around with ClickOnce today, and it's all good stuff. Except, that if I change my application to NOT be full trust (which seems to make very little difference to the user prompt, which scares me!) but to be installed on the start menu, it seems I don't have permission to call CheckForUpdate()! I understand that if I'm not a full trust app, I can't read the filesystem etc., but not being able to update myself seems a bit...
0
3628
by: Patty05 | last post by:
I have a datagrid on a form that update properly when form loads. When the program runs and I type in/add a new row in the datagrid, it does not save the changes. Any help would be greatly appreaciated. Thanks. Code below: Imports System.Data.OleDb Public Class Form2 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code "
8
5372
by: =?Utf-8?B?R3JlZyBMYXJzZW4=?= | last post by:
I'm trying to figure out how to modify a panel (panel1) from a backgroundworker thread. But can't get the panel to show the new controls added by the backgroundwork task. Here is my code. In this code there is a panel panel1, that I populate with a lable in the foreground. Then when I click on "button1" a backgroundworker thread in async mode is started. When the backgoundworker thread completes the thread returns a panel to populate...
2
2934
by: explode | last post by:
I made nova oledbdataadapter select update insert and delete command and connection veza. dataset is Studenti1data, I made it by the new data source wizard,and made datagridview and bindingsource draging Table1 to Form2. The select command works fine, but when I change the data and call update command I get a syntax error: System.Data.OleDb.OleDbException was unhandled ErrorCode=-2147217900 Message="Syntax error in UPDATE statement."...
5
2181
by: explode | last post by:
I made a procedure Public Sub Novo(ByVal nova1 As String, ByVal nova2 As String) that creates a new oledbDataAdapter with insert update select and delete commads. I also added that commands can change depending how many columns are in a Table. I add a new column with this code: Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Try...
0
3112
jas16183
by: jas16183 | last post by:
Hi Guys, need some help here, Im working on a project in vb.net, the problem im facing is that my tableadapter.update(dataset.datatable) is not functioning, the datatable has a relation with another table, the relationship is on to many. in the dataset designer I configured it to Relation only, the database is in mdf format, I can update my records in the access file, and can do it also in the server explorer, but not when from my form,...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9524
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10168
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.