467,877 Members | 1,210 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,877 developers. It's quick & easy.

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="clientName" 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="clientName" date="mm/dd/yyyy" />
</Contacts><Contacts>
<contact name="clientName" 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.XmlDocument salesText = new XmlDocument();
XmlElement newContacts =
salesText.CreateElement("Contacts");
XmlElement newContact =
salesText.CreateElement("Contact");
salesText.AppendChild(newContacts);
newContacts.AppendChild
(newContact);
//Create a new XmlAttribute
XmlAttribute idAttribute =
salesText.CreateAttribute("Client");
idAttribute.Value =
this.textBox1.Text;
//Set the Attribute on the
XmlElement
newContact.SetAttributeNode
(idAttribute);
//Date Attribute
XmlAttribute dateAttribute =
salesText.CreateAttribute("Date");
dateAttribute.Value =
this.textBox2.Text;
newContact.SetAttributeNode
(dateAttribute);

//Insert the newly created
XmlElement into the XmlDocument
salesText.InsertAfter
(newContacts,salesText.LastChild);

//Create a FileStream and Save the
Document
FileStream docOut =
new FileStream
(docPath,FileMode.Append,FileAccess.Write,FileShar e.ReadWri
te);
salesText.Save(docOut);
//code snippet ends here.

Any help much appreciated...
Nov 15 '05 #1
  • viewed: 3914
Share:
1 Reply
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(docPath,FileMode.Append,FileAccess.Writ e,FileShare.ReadWrite); salesText.Save(docOut);
Change the first line above to FileMode.Create or FileMode.Write

HTH,
--- Nick

"BJ Allmon" <an*******@discussions.microsoft.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="clientName" 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="clientName" date="mm/dd/yyyy" />
</Contacts><Contacts>
<contact name="clientName" 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.XmlDocument salesText = new XmlDocument();
XmlElement newContacts =
salesText.CreateElement("Contacts");
XmlElement newContact =
salesText.CreateElement("Contact");
salesText.AppendChild(newContacts);
newContacts.AppendChild
(newContact);
//Create a new XmlAttribute
XmlAttribute idAttribute =
salesText.CreateAttribute("Client");
idAttribute.Value =
this.textBox1.Text;
//Set the Attribute on the
XmlElement
newContact.SetAttributeNode
(idAttribute);
//Date Attribute
XmlAttribute dateAttribute =
salesText.CreateAttribute("Date");
dateAttribute.Value =
this.textBox2.Text;
newContact.SetAttributeNode
(dateAttribute);

//Insert the newly created
XmlElement into the XmlDocument
salesText.InsertAfter
(newContacts,salesText.LastChild);

//Create a FileStream and Save the
Document
FileStream docOut =
new FileStream
(docPath,FileMode.Append,FileAccess.Write,FileShar e.ReadWri
te);
salesText.Save(docOut);
//code snippet ends here.

Any help much appreciated...

Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Pam Ammond | last post: by
2 posts views Thread by Joe Fetters via .NET 247 | last post: by
1 post views Thread by Greg | last post: by
5 posts views Thread by Danny Tuppeny | last post: by
8 posts views Thread by =?Utf-8?B?R3JlZyBMYXJzZW4=?= | last post: by
5 posts views Thread by explode | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.