|
Do you want to serialize array of arraylists? As far as I remember xml serialization doesn't support serializing graohicspaths (But I'm not sure) If arraylist doesn't contain some complex type that can't be serialized by xml serialization then you can serialize it very easily. Here are some links to get you started:
xml serialization[^]
Serialize Arrays and ArrayLists to XML[^]
|
|
|
|
|
Actually i have a few arraylist which each contains different types or classes. Last time u use binary serialization, i can serialize each or the arraylist separately. e.g:
<br />
<br />
formatter.Serialize(myStream, arr1)<br />
formatter.Serialize(myStream, arr2)<br />
formatter.Serialize(myStream, arr3)<br />
<br />
<br />
<br />
<br />
arr1 = (ArrayList) formatter.Deserialize(myStream);<br />
arr2 = (ArrayList) formatter.Deserialize(myStream);<br />
arr3 = (ArrayList) formatter.Deserialize(myStream);<br />
<br />
<br />
However, does this concept applies with xml serialization?
Also, by looking at the examples given, it seems that for each arraylist i need to create a class each? If it is so, which class should is labelled as "XmlRoot"? if not mistaken, in an XML file there should have only one root. however, from my perspective, all of the arraylist should be xmlelements. Am i right?
Thanks in advance.
|
|
|
|
|
Hi Cyn8
I said how you can serialize graphicPaths (if you had problem with it just tell me) before and about the arraylists you can just create a new Class that contains those data (is much better) like collection or just simply put your arrayLists niside another one and serialize that (I think I said these before too)
but if you mean SOAP serializtion which give you the xml file just simply use SOAPFormatter Class instead of BinaryFormatter class and you don't need to change your code but as I know Xmls from SOAP are not such a readable ones So if you need to Save your data which be easily readable you can use xml.serializations which Giorgi Dalakishvili sent you some cool links about that or just create your custom data saver with xml classes if your datas are not such complex
and at last for using SOAPFormatter you must add it as reference
best regards
-- modified at 10:07 Friday 17th August, 2007
|
|
|
|
|
hi there,
With your help last time, i've manage to use binary serialization to serialize and deserialize the arraylists
However, now i have abit problem with xml.serializations. Looking at the examples given, it seems that for each arraylist i need to create a class each? If it is so, which class should is labelled as "XmlRoot"? in an XML file there should have only one root and can have many elements right? Sorry if i still don't quite understand.
Besides, i try to modify the codes by replacing all the functions that uses binary formatter to xml serializer, which does not works as shown below.
XmlSerializer s = new XmlSerializer( typeof(ArrayList) );<br />
private ArrayList alDrawingObjects = new ArrayList();<br />
public ArrayList CoordList=new ArrayList (); <br />
public ArrayList ShapeTypeList=new ArrayList (); <br />
public ArrayList ColorList=new ArrayList ();<br />
public ArrayList PolyCoord = new ArrayList();<br />
public ArrayList SizeList = new ArrayList();
Serializing:
Stream myStream ;<br />
myStream = File.OpenWrite(filename);<br />
ArrayList arr = new ArrayList();<br />
if (myStream != null)<br />
{<br />
<br />
TextWriter w = new StreamWriter( filename );<br />
s.Serialize( w, ShapeTypeList );<br />
s.Serialize( w, ColorList );<br />
s.Serialize( w, CoordList);<br />
s.Serialize( w, SizeList );<br />
<br />
for(int i=0;i<PathList.Count;i++)<br />
{<br />
GraphicsPathData gpd = new GraphicsPathData((GraphicsPath)PathList[i]);<br />
Stream gpdStream = GraphicsPathData.Serialize(gpd);<br />
arr.Add(gpdStream);<br />
<br />
}<br />
s.Serialize( w, arr);<br />
<br />
w.Close();
Deserializing:
ArrayList newList;<br />
TextReader r = new StreamReader(filename);<br />
ShapeTypeList = (ArrayList) s.Deserialize(myStream);<br />
ColorList = (ArrayList) s.Deserialize(myStream);<br />
CoordList = (ArrayList)s.Deserialize(myStream);<br />
SizeList = (ArrayList) s.Deserialize(myStream);<br />
arr=(ArrayList) s.Deserialize(myStream);<br />
for(int i=0;i<arr.Count;i++)<br />
{<br />
Stream newStream = new MemoryStream(((MemoryStream)arr[i]).ToArray());<br />
GraphicsPath gpDeserialized = GraphicsPathData.GetGraphicsPath(newStream); <br />
PathList.Add(gpDeserialized); <br />
}<br />
myStream.Close(); <br />
r.Close();
Please help to explain on which part i should change?
Thanks again.
-- modified at 2:42 Monday 20th August, 2007
|
|
|
|
|
if i put the all arraylists into another arraylist, when i deserialize them, how do i deserialize them into the original arraylist?
Thanks for any reply
|
|
|
|
|
 I think the best way to do this is to create a new class for holding whole of your data. about the GPD you used in the code ,as a tip holding serialized streams is not a good choice ,I think you can serialize whole of your GPDs together (because they are marked as serializable)
and in the code you put all your data in one stream ,retrieving from it is not impossible but is far more harder than use a new class to hold all of your data
here is the scheme of such a class (it has some works to be a complete one)
<code>
[XmlRoot("MyData")]
[Serializable]
class MyData
{
ArrayList shapeTypeList;
ArrayList colorList;
ArrayList coordList;
ArrayList sizeList;
ArrayList graphicsPathDataList;
[XmlArray("ShapeData")]
[XmlArrayItem("Shape",typeof())]
public ArrayList ShapeTypeList
{
get { return this.shapeTypeList; }
}
[XmlArray("ColorData")]
[XmlArrayItem("Color",typeof())]
public ArrayList ColorList
{
get { return this.colorList; }
}
[XmlArray("CoorinationData")]
[XmlArrayItem("Coordination",typeof())]
public ArrayList CoordList
{
get { return this.coordList; }
}
[XmlArray("SizeData")]
[XmlArrayItem("Size",typeof())]
public ArrayList SizeList
{
get { return this.sizeList; }
}
[XmlArray("GPDData")]
[XmlArrayItem("GPD",typeof())]
public ArrayList GraphicsPathDataList
{
get { return this.graphicsPathDataList; }
}
public MyData(ArrayList shape, ArrayList color, ArrayList coord, ArrayList size, ArrayList gpd)
{
this.shapeTypeList = shape;
this.colorList = color;
this.coordList = coord;
this.sizeList = size;
this.graphicsPathDataList = gpd;
}
static MyData BinLoad(string path)
{
BinaryFormatter binSer=new BinaryFormatter();
FileStream fo=new FileStream(path,FileMode.Open);
object data=binSer.Deserialize(fo);
fo.Close();
return data as MyData;
}
void BinSerialize(string path)
{
BinaryFormatter binSer=new BinaryFormatter();
FileStream fo=new FileStream(path,FileMode.OpenOrCreate);
binSer.Serialize(fo,this);
fo.Close();
}
static MyData XmlLoad(string path)
{
XmlSerializer xmlSer=new XmlSerializer(typeof(MyData));
FileStream fo=new FileStream(path,FileMode.Open);
object data=xmlSer.Deserialize(fo);
fo.Close();
return data as MyData;
}
void XmlSerialize(string path)
{
XmlSerializer xmlSer=new XmlSerializer(this.GetType());
FileStream fo=new FileStream(path,FileMode.OpenOrCreate);
xmlSer.Serialize(fo,this);
fo.Close();
}
public enum SerializeTo{XML,Binary};
public void Serialize(string path,SerializeTo to)
{
if(to==SerializeTo.Binary)
this.BinSerialize(path);
else
this.XmlSerialize(path);
}
public static MyData Load(string path,SerializeTo to)
{
if(to==SerializeTo.Binary)
return BinLoad(path);
else
return XmlLoad(path);
}
}
</code>
I'm not sure it works just after putting the types because I couldn't test that, but i'm sure it will give you the clue
another thing MyData Class can be serialize in both Xml and Binary
hope the post would be useful
good luck my friend
|
|
|
|
|
hi,
After i serialize a few arraylist, my xml file seem to look like below:
<code>
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfAnyType xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<anyType xsi:type="xsd:int">1</anyType>
</ArrayOfAnyType><?xml version="1.0" encoding="utf-8"?>
<ArrayOfAnyType xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<anyType xsi:type="Color" />
</ArrayOfAnyType><?xml version="1.0" encoding="utf-8"?>
<ArrayOfAnyType xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<anyType xsi:type="ArrayOfPoint">
<Point>
<X>40</X>
<Y>18</Y>
</Point>
<Point>
<X>110</X>
<Y>18</Y>
</Point>
<Point>
<X>110</X>
<Y>68</Y>
</Point>
<Point>
<X>40</X>
<Y>68</Y>
</Point>
</anyType>
</ArrayOfAnyType><?xml version="1.0" encoding="utf-8"?>
<ArrayOfAnyType xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" /><?xml version="1.0" encoding="utf-8"?>
<ArrayOfAnyType xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<anyType xsi:type="MemoryStream">
<Position>142</Position>
<Capacity>256</Capacity>
</anyType>
</ArrayOfAnyType></code>
is this an error? why does is the elements is named '<ArrayOfAnyType>'
Besides, how do i deserialize arraylist of different types.
first i declareas shown below:(this is declare in Form, i did not use MyData class, instead all arraylist is declared in the form)
<code>XmlSerializer s = new XmlSerializer( typeof(ArrayList),new Type[] {typeof(Coordinate),typeof(ColorL),typeof(SizeL), typeof(ArrSave)} );
</code>
then in Load function:
<code>
filename = openFileDialog.FileName;
Stream myStream = openFileDialog.OpenFile();
ShapeTypeList = (ArrayList) s.Deserialize(myStream);
ColorList = (ArrayList) s.Deserialize(myStream);
CoordList = (ArrayList)s.Deserialize(myStream);
SizeList = (ArrayList) s.Deserialize(myStream);
arr=(ArrayList) s.Deserialize(myStream);
arr=(ArrayList) s.Deserialize(myStream);
for(int i=0;i<arr.Count;i++)
{
Stream newStream = new MemoryStream(((MemoryStream)arr[i]).ToArray());
GraphicsPath gpDeserialized = GraphicsPathData.GetGraphicsPath(newStream);
PathList.Add(gpDeserialized);
}
myStream.Close();
r.Close();
</code>
thanks for any reply. Sorry if if my method is wrong. Just trying a different way.
|
|
|
|
|
While i am executing the following code, i am getting the error 'Input string was not in a correct format'
for(i=0;i<=6;i=i+2)
{
for(j=0;j<=485;j++)
{
ran = (Excel.Range)Worksheet.Cells[j+1, i+1];
s[i,j]= int.Parse((string)ran.Text);
tw.WriteLine("The Value of M["+i+"]["+j+"] is" + s[i,j]);
}
}
i am storing total 400 values from the excel sheet, i am having problem at A287
this is the output file
The Value of M[0][284] is-5
The Value of M[0][285] is-3
The Value of M[0][286] is-4
The Value of M[0][
here i am getting exception
|
|
|
|
|
It means that A287 contains something that isn't string representation of number. Instead of int.Parse use int.TryParse to see if the string can be parsed as number.
|
|
|
|
|
an empty string would not be acceptable.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
I have two forms:
the first contains a dataGridView containing a list of staff members.
it also contains 4 buttons.
1. refesh
2. update
3. add
4. delete
The add_click event opens a second form for inserting the staff member's details.
when you click the OK button on this form it must update the Staff DB
the code in this event is as follows:
public void OKbutton_Click(object sender, EventArgs e)
{
SqlCommand insertCommand = new SqlCommand("insert into Staff (Lastname,Firstname,TelephoneExtension) values (lastname,firstname,telephoneExtension)");
insertCommand.Parameters.Add("lastname", SqlDbType.NVarChar, 80, "Lastname");
insertCommand.Parameters.Add("firstname", SqlDbType.NVarChar, 80, "Firstname");
insertCommand.Parameters.Add("telephoneExtension", SqlDbType.NVarChar, 0, "TelephoneExtension");
//mySqlDataAdapter2.InsertCommand = insertCommand;
//mySqlDataAdapter2.Update(myDataSetAddandUpdateForm, "Staff");
this.Close();
}
My code is not working. can anyone help me please. I am a trainee programmer.
|
|
|
|
|
Can you be more specific? Is there any exception thrown during execution or just database doesn't get updated?
|
|
|
|
|
I think ur making a lot of mistakes in this... I'd recommend that you go thru a few walkthoughs on MSDN and next time plz take the time to tell us about ur platform. like C# 1.1,2.0 ... SQL Server 2000 uknow!
by the way you're not ExecutenonQuery here and are the syntax of the insert command is all wrong aswell u give parametes in SQL starting with the @ sign. In summary your all messed up here. First study the basics thorougly then start coding ok, dont worry u'll get over it in quick time. Remember... Every body falls the first time!
Rocky
You can't climb up a ladder with your hands in your pockets.
|
|
|
|
|
Thank you...
I use Visual Studio 2005 with SQL Server Express.
I need to hard code everything and not make use of the data adapter configuration wizard.
I need to do this small project so I can learn how this all works, but was just told: go figure it out yourself...
Can you suggest a walkthrough on MSDN I can follow please. im desperate.
thank you
from a sunny south africa.
|
|
|
|
|
just copy paste this URL in your MSDN 2005 URL bar
ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_raddata/html/15a88fb8-3bee-4962-914d-7a1f8bd40ec4.htm
There's whole list of walkthroughs
This one's from that list showing you how to save data
ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_raddata/html/68befa96-7463-43e8-abcf-dc2f42ccd53d.htm
hope thats helpful for and dont get so desperate, its all abt using ur mind, MSDN and Google intelligently, concentrate on learning more rather than just completing the job somehow
Rocky
You can't climb up a ladder with your hands in your pockets.
|
|
|
|
|
helloise wrote: insertCommand.Parameters.Add("telephoneExtension", SqlDbType.NVarChar, 0, "TelephoneExtension");
Why would telephoneExtension have a length of 0?
And, you didn't execute the insertCommand you declared so the database will not get updated.
-- If this is a post that has been helpful to you, please vote for it. Thank you!
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
--Rich Cook
|
|
|
|
|
I have the complete code here:
how do I excecute the insertCommand then, ie what is the code for it? thank you very much....
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace MyWindowsApp
{
public partial class AddAndUpdateForm : Form
{
public AddAndUpdateForm()
{
InitializeComponent();
}
private void LastnameTextBox_TextChanged(object sender, EventArgs e)
{
string lastname = LastnameTextBox.Text;
}
private void FirstnameTextBox_TextChanged(object sender, EventArgs e)
{
string firstname = FirstnameTextBox.Text;
}
private void NicknameTextBox_TextChanged(object sender, EventArgs e)
{
string nickname = NicknameTextBox.Text;
}
private void UserNameTextBox_TextChanged(object sender, EventArgs e)
{
string username = UserNameTextBox.Text;
}
private void TelExtTextBox_TextChanged(object sender, EventArgs e)
{
string telephoneExtension = TelExtTextBox.Text;
}
public void OKbutton_Click(object sender, EventArgs e)
{
SqlConnection mySqlConnection = new SqlConnection("server=ISS\\SQLEXPRESS;database=Staff;uid=sa;pwd=xbcprfly");
mySqlConnection.Open();
SqlCommand myCommand = mySqlConnection.CreateCommand();
myCommand.CommandText = "SELECT Lastname, Firstname, TelephoneExtension FROM Staff ORDER BY Lastname";
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(myCommand);
DataSet myDataSet = new DataSet();
SqlCommand insertCommand = new SqlCommand("insert into Staff (Lastname,Firstname,TelephoneExtension) values (lastname,firstname,telephoneExtension)",mySqlConnection);
insertCommand.Parameters.Add("lastname", SqlDbType.NVarChar, 80, "Lastname");
insertCommand.Parameters.Add("firstname", SqlDbType.NVarChar, 80, "Firstname");
insertCommand.Parameters.Add("telephoneExtension", SqlDbType.NVarChar, 50, "TelephoneExtension");
//the following code is making me confused.
//myCommand.ExecuteNonQuery();
//mySqlDataAdapter.Fill(myDataSet, "Staff");
//mySqlDataAdapter.InsertCommand = insertCommand;
//mySqlDataAdapter.Update(myDataSet, "Staff");
//SqlCommand cmd = new SqlCommand(cmdStr, con);
//cmd.ExecuteNonQuery();
//SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(mySqlDataAdapter);
this.Close();
}
private void Cancelbutton_Click(object sender, EventArgs e)
{
this.Close();
}
private void AddAndUpdate_Load(object sender, EventArgs e)
{
}
}
}
|
|
|
|
|
Hi all,
Any one know how to export datagridview columns(values) to MS Word using c#(windows application).
Thanks in advance
Know is Drop, Unknown is Ocean
|
|
|
|
|
You can use word object library to create new word document and put some text in it so iterate through all the rows of datagridview and put the text in word document.
|
|
|
|
|
Thanks for your suggestion, i need some sample code to understand this. Plz guid me.
Know is Drop, Unknown is Ocean
|
|
|
|
|
|
Thanks for your help. i found the solution.
Know is Drop, Unknown is Ocean
|
|
|
|
|
Hello friends,
I have developed many controls for my Window based application in C# .net. However i want to know whether i can utilise those controls on a web page. I have tried but so far no success.
Regards
Amit
|
|
|
|
|
yes you can do so by making dll of your control
and embedding your windows contol to htmls object tag.
rahul
|
|
|
|
|
Hello Friends,
I want to print contents of my Crystal report .Net to a Generic text Only Printer say an Epson 300+. But some how my printing gets disturbed.This happens only when my printer is using the Generic Text Only driver. My main concern is to print the report as fast as possible.
Thanks
Amit Kadam
|
|
|
|
|