homeGeek CultureWebstoreeCards!Forums!Joy of Tech!AY2K!webcam

The Geek Culture Forums


Post New Topic  New Poll  Post A Reply
my profile | directory login | | search | faq | forum home
  next oldest topic   next newest topic
» The Geek Culture Forums   » Other Geeky Stuff   » Ask a Geek!   » writing XML with C#

 - UBBFriend: Email this page to someone!    
Author Topic: writing XML with C#
maybe.logic
Alpha Geek
Member # 5014

Member Rated:
3
Icon 1 posted June 18, 2006 05:33      Profile for maybe.logic     Send New Private Message       Edit/Delete Post   Reply With Quote 
I know how to write to an XML file with c# but I am having an issue with it just overwritting what is already in the file. Let me show you what I am doing and what I need to happen.

Below is the code that writes the XML file

code:
XmlTextWriter InvWriter = null;
InvWriter = new XmlTextWriter(EBAY_Constants.m_InventoryFileName, null);
try
{
InvWriter.Formatting = Formatting.Indented;
InvWriter.Indentation = 6;
InvWriter.Namespaces = false;

InvWriter.WriteStartDocument();

InvWriter.WriteStartElement("", "Inventory", "");

InvWriter.WriteStartElement("", "ItemNumber", "");
InvWriter.WriteString(this.textBox_ItemNumber.Text.ToString());
InvWriter.WriteEndElement();

InvWriter.WriteStartElement("", "Title", "");
InvWriter.WriteString(this.textBox_title.Text.ToString());
InvWriter.WriteEndElement();

InvWriter.WriteStartElement("", "Description", "");
InvWriter.WriteString(this.textBox_Description.Text.ToString());
InvWriter.WriteEndElement();

InvWriter.WriteStartElement("", "Cost", "");
InvWriter.WriteString(this.textBox_ItemCost.Text.ToString());
InvWriter.WriteEndElement();

InvWriter.WriteStartElement("", "Tax", "");
InvWriter.WriteString(this.textBox_Tax.Text.ToString());
InvWriter.WriteEndElement();

InvWriter.WriteStartElement("", "TCost", "");
InvWriter.WriteString(this.textBox_TCost.Text.ToString());
InvWriter.WriteEndElement();

InvWriter.WriteStartElement("", "Quantity", "");
InvWriter.WriteString(this.textBox_Quantity.Text.ToString());
InvWriter.WriteEndElement();

InvWriter.WriteStartElement("", "PurchaseDate", "");
InvWriter.WriteString(this.dateTimePicker_AddItem.Text.ToString());
InvWriter.WriteEndElement();

}
catch (Exception error)
{
Console.WriteLine("Exception: {0}", error.ToString());
}
finally
{
if (InvWriter != null)
{
InvWriter.Close();
}
MessageBox.Show("Item: " + this.textBox_title.Text.ToString() +
"\n\nUnits: " + this.textBox_Quantity.Text.ToString(),
"Item Added To Inventory");
ClearTextBoxes();
}

One call to the method produces this:

code:
<?xml version="1.0"?>
<Inventory>
<ItemNumber>...</ItemNumber>
<Title>...</Title>
<Description>...</Description>
<Cost>...</Cost>
<Tax>...</Tax>
<TCost>...</TCost>
<Quantity>...</Quantity>
<PurchaseDate>...</PurchaseDate>
</Inventory>

Now when I make a new call to the method it will overwrite the data that was in there and replace with new data. I want this.

code:
<?xml version="1.0"?>
<Inventory>
<ItemNumber>...</ItemNumber>
<Title>...</Title>
<Description>...</Description>
<Cost>...</Cost>
<Tax>...</Tax>
<TCost>...</TCost>
<Quantity>...</Quantity>
<PurchaseDate>...</PurchaseDate>
</Inventory>
<Inventory>
<ItemNumber>...</ItemNumber>
<Title>...</Title>
<Description>...</Description>
<Cost>...</Cost>
<Tax>...</Tax>
<TCost>...</TCost>
<Quantity>...</Quantity>
<PurchaseDate>...</PurchaseDate>
</Inventory>
.
.
.
<Inventory>
<ItemNumber>...</ItemNumber>
<Title>...</Title>
<Description>...</Description>
<Cost>...</Cost>
<Tax>...</Tax>
<TCost>...</TCost>
<Quantity>...</Quantity>
<PurchaseDate>...</PurchaseDate>
</Inventory>

Anyone have the reason its not producing what I want and just overwritting the data each time?
Posts: 345 | Registered: Mar 2006  |  IP: Logged
GrumpySteen

Solid Nitrozanium SuperFan
Member # 170

Icon 1 posted June 18, 2006 06:26      Profile for GrumpySteen     Send New Private Message       Edit/Delete Post   Reply With Quote 
[Razz]
Posts: 6364 | From: Tennessee | Registered: Jan 2000  |  IP: Logged
uilleann
Discontinued


Icon 11 posted June 18, 2006 11:02            Edit/Delete Post   Reply With Quote 
Snaggy, is there any way to stop UBB putting this crap in every code block pre tag:

style="font-size:x-small; ..."

I don't know why they're doing but it's supremely retarded. IE has no idea what it means so I get sensibly-sized text, but Firefox obeys it like a good little puppy (as does iCab I believe), and trying to read code blocks in Firefox comes close to wearing out my eyeballs unless I go crank up the font size. The word "code" is also reduced to size="1" but Verdana at that size is still readable.

IP: Logged
Spiderman

Solid Nitrozanium Superfan!
Member # 1609

Member Rated:
4
Icon 1 posted June 18, 2006 19:29      Profile for Spiderman     Send New Private Message       Edit/Delete Post   Reply With Quote 
If I recall correctly, XmlTextWriter can direct output in to whatever object you pass it. For example, XmlTextWriter w = new XmlTextWriter(Console.Out); should do...just what it looks like it does.

With this in mind, creating a file object with the option to append data (as opposed to just writing from the beginning of the file), and then passing this file into the XmlTextWriter instantiation statement should help considerably.

It's been awhile since I last worked with anything .NET related, (and when I did work with it, my experience was limited...thankfully), so hopefully I'm not way off base here, but it seems that giving the XmlTextWriter instance a slightly clearer definition of what you're trying to do should resolve your issue.

--------------------
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]

Posts: 846 | From: Chicago | Registered: Aug 2002  |  IP: Logged
quantumfluff
BlabberMouth, a Blabber Odyssey
Member # 450

Member Rated:
5
Icon 1 posted June 18, 2006 20:35      Profile for quantumfluff     Send New Private Message       Edit/Delete Post   Reply With Quote 
When you do the new XMLTextWriter, you're going to overwrite the file. That's the correct behavior. If you want to append, you should load the file, write the headers, write each Inventory and then close.

On the other hand, this document is so simple you could just open a text file and use printf to create it. If you want to append, just open it in append mode.

Posts: 2902 | From: 5 to 15 meters above sea level | Registered: Jun 2000  |  IP: Logged
GrumpySteen

Solid Nitrozanium SuperFan
Member # 170

Icon 1 posted June 18, 2006 20:52      Profile for GrumpySteen     Send New Private Message       Edit/Delete Post   Reply With Quote 
/me wonders if anybody noticed that the [Razz] was actually a link...

--------------------
Worst. Celibate. Ever.

Posts: 6364 | From: Tennessee | Registered: Jan 2000  |  IP: Logged
dragonman97

SuperFan!
Member # 780

Member Rated:
4
Icon 1 posted June 18, 2006 21:41      Profile for dragonman97   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by Steen:
/me wonders if anybody noticed that the [Razz] was actually a link...

I did, but that's because I have graphics turned off. [Wink]

--------------------
There are three things you can be sure of in life: Death, taxes, and reading about fake illnesses online...

Posts: 9332 | From: Westchester County, New York | Registered: May 2001  |  IP: Logged
uilleann
Discontinued


Icon 1 posted June 18, 2006 22:04            Edit/Delete Post   Reply With Quote 
WTF?

How did ...

Ah. Now that's a nasty trick. UBB adds border="0" to all it's Graemlin image tags, probably for the Instant Graemlins area in the post system ... I never spotted it to be a link (not as easy as your previous stunt) and when you mentioned it I wondered how you'd forced a border="0" in...

IP: Logged


All times are Eastern Time  
Post New Topic  New Poll  Post A Reply Close Topic    Move Topic    Delete Topic next oldest topic   next newest topic
 - Printer-friendly view of this topic
Hop To:

Contact Us | Geek Culture Home Page

2015 Geek Culture

Powered by Infopop Corporation
UBB.classicTM 6.4.0



homeGeek CultureWebstoreeCards!Forums!Joy of Tech!AY2K!webcam