Write to XML with C#

M

Mike

I am trying to collect and write some temporary data to xml file
before I put them into DB.

I am writing into xml file like follows:
private void WriteXML()
{
//Use the xmlTextWriter to open a new xml file
XmlTextWriter writer = new
XmlTextWriter(@"C:\\Data\\tblPomocnaTablica.xml",
System.Text.Encoding.UTF8);

//Write the root element
writer.WriteStartElement("tblTableX");

// Write the elements
writer.WriteElementString("ClientID", "2");
writer.WriteElementString("EmployeeID", "4");
writer.WriteElementString("ShopID", "100");
writer.WriteElementString("ProductID", "100");
writer.WriteElementString("Qty", "1");
// end the root element
writer.WriteEndElement();
// Flush
writer.Flush();
//Write the XML to file and close the writer
writer.Close();
}
It is works and I get something like this:
ClientID | EmployeeID | ProdavonicaID | ProductID | Qty |
2 | 4 | 100 | 100 |1 |
Two Questions
1) How I can write a new rows without rewriting existing rows,
for e.g.?
2 | 4 | 124 | 25 |1 |
etc.

Result should be something like this:
ClientID | EmployeeID | ProdavonicaID | ProductID | Qty |
------------------------------------------------------------------------
2 | 4 | 100 | 100 |1 |
2 | 4 | 124 | 25 |1 |

2) How I can change one value in some row, for e.g.
row 2 ShopID = 250, is that possible?
Thanks in advance
Mike
 
M

Martin Honnen

Mike said:
I am trying to collect and write some temporary data to xml file
before I put them into DB.

I am writing into xml file like follows:
private void WriteXML()
{
//Use the xmlTextWriter to open a new xml file
XmlTextWriter writer = new
XmlTextWriter(@"C:\\Data\\tblPomocnaTablica.xml",
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You should either use @ on your string literal or escape the backslash
as \\ but doing both does not work.
System.Text.Encoding.UTF8);

//Write the root element
writer.WriteStartElement("tblTableX");

// Write the elements
writer.WriteElementString("ClientID", "2");
writer.WriteElementString("EmployeeID", "4");
writer.WriteElementString("ShopID", "100");
writer.WriteElementString("ProductID", "100");
writer.WriteElementString("Qty", "1");
// end the root element
writer.WriteEndElement();
// Flush
writer.Flush();
//Write the XML to file and close the writer
writer.Close();
}
It is works and I get something like this:
ClientID | EmployeeID | ProdavonicaID | ProductID | Qty |
2 | 4 | 100 | 100 |1 |

Why would you get that output with an XmlWriter? That format certainly
is not XML.
Two Questions
1) How I can write a new rows without rewriting existing rows,
for e.g.?
2 | 4 | 124 | 25 |1 |
etc.

Result should be something like this:
ClientID | EmployeeID | ProdavonicaID | ProductID | Qty |

Using XmlWriter/XmlTextWriter you write a complete document. If you want
to edit/manipulate existing XML then you have a couple of options in the
..NET framework, one is System.Xml.XmlDocument, .NET's XML DOM
implementation, a second is getting a relational view on the data by
using the ReadXml/WriteXml methods of System.Data.DataSet/DataTable, a
third is XML serialization/deserialization.
2) How I can change one value in some row, for e.g.
row 2 ShopID = 250, is that possible?

See above for the options you have.
As you seem to have a relational view of your data anyway you might
simply use DataTable/DataSet.
 
M

Mike

Why would you get that output with an XmlWriter? That format certainly
is not XML.

I'd like to temporary save a values before processed an order,
something simmilar like shoping cart (user collect articles into
shooping cart - in this case in xml file).
I am not sure is this a good way, but...

If you know for some good example or something, plese let me know.

Thanks
 
A

Andrew Faust

Why not just store the information in some temporary tables inside the
database and move them to the final tables later?
 
A

Andrew Faust

Why wouldn't it be? You have data to store and you have a database. Seems
like a match made in heaven.

Just so we're clear, I wasn't proposing you create a table on the fly, use
it for a while and then destroy it. I was proposing you create a table (or
tables) as part of your design that you always use for this temporary
storage. You can delete the records after you've moved them to the final
table and keep reusing the table.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top