|
Why do you say that using XML serialization would be out of the question just because you have some properties you might not want to serialize?
If you have properties you want to hide during serialization, then just mark them with the XmlIgnoreAttribute .
There are obvious advantages to using XML serialization besides the fact that it's probably faster than rolling your own solution. It's vastly more tolerant of changes to structure of the class being serialized, and almost impossible to accidentally create malformed XML.
Here's an example - enjoy...:
using System;
using System.IO;
using System.Xml.Serialization;
namespace XmlSerializationExample
{
class Example
{
[STAThread]
static void Main(string[] args)
{
MyClass mc = new MyClass();
mc.Property1 = "a";
mc.Property2 = "b";
mc.Property3 = "c";
mc.PickANumber = 1234;
mc.IgnoreThisProperty = "Blahblahblahblah.";
mc.Save(@"c:\serialized_myclass.xml");
MyClass mc2 = MyClass.Load(@"c:\serialized_myclass.xml");
Console.WriteLine("Deserialized Property1: {0}", mc2.Property1);
Console.WriteLine("Deserialized Property2: {0}", mc2.Property2);
Console.WriteLine("Deserialized Property3: {0}", mc2.Property3);
Console.WriteLine("Deserialized PickANumber: {0}", mc2.PickANumber);
Console.WriteLine("Deserialized ComboProperty: {0}", mc2.ComboProperty);
Console.WriteLine("Deserialized IgnoreThisProperty: {0}", mc2.IgnoreThisProperty);
Console.ReadLine();
}
}
public class MyClass
{
#region Fields
private string _field1;
private string _field2;
private string _field3;
private string _ignoredField;
public int PickANumber;
#endregion Fields
#region Constructor
public MyClass(){}
#endregion Constructor
#region Properties
public string Property1
{
get{return _field1;}
set{_field1 = value;}
}
public string Property2
{
get{return _field2;}
set{_field2 = value;}
}
public string Property3
{
get{return _field3;}
set{_field3 = value;}
}
[XmlIgnore]
public string IgnoreThisProperty
{
get{return _ignoredField;}
set{_ignoredField = value;}
}
public string ComboProperty
{
get{return string.Format("{0}{1}{2}", Property1, Property2, Property3);}
}
#endregion Properties
#region Methods
public void Save(string path)
{
XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
using(StreamWriter writer = new StreamWriter(path, false))
{
serializer.Serialize(writer, this);
writer.Close();
}
}
public static MyClass Load(string path)
{
MyClass instance = null;
if(File.Exists(path))
{
XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
using(StreamReader reader = new StreamReader(path))
{
instance = serializer.Deserialize(reader) as MyClass;
reader.Close();
}
}
return instance;
}
#endregion Methods
}
}
The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
|
|
|
|
|
Thanks, I didn't think of the XmlIgnore attribute. Just out of interest, if I added a property, would XmlSerializer still be able to read files that did not have that property yet?
|
|
|
|
|
The deserialization should ignore missing property values in the XML, although you might get into some trouble if you changed the type of an existing property to something that can't be coerced from the previous type (e.g. changing a string to a decimal or something), then tried to load previously serialized versions of the type.
Good luck.
The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
|
|
|
|
|
I don't think I'll ever have to change the type of a property, my only concern was newly added properties. Thanks for clearing this up for me!
|
|
|
|
|
Hello all coders!
I'm working with Updater Appication block with Enterprise Library and they work well but need to integrate Updater application into the Enterprise Library to see al topic togheter, I know i have to work with the code of Enterprise but no have idea how do that or even if is it possible. I'll glad any help, comments or link for information.
Thanks
David Alcaraz
|
|
|
|
|
Hallo...
I'm interested to write some Access Technology applications. Clearly it's only for study purpose. The problem is that I don't know where tu study this particolar problem. For example, some of my questions are:
1.How can I implement a DCM driver??
2.How does DCM driver (video intercept) work?
3.How can I access to TTS system? Ok, the speech SDK allows me to use the microsoft voice, but the other?
4.Which language should be used?
Can anyone explain me those things and how can I put them together?
I wrote in a C# forum because C# is my favourite language, but.... I don't know if I can use it for this particular situation... mmmh...
I know that this is very complex!
Thank you very much...
Comet
-- modified at 13:04 Thursday 29th September, 2005
|
|
|
|
|
I'm not familiar with DCM, but I will tell you that C# is a higher-level language. Writing things like device drivers ought to be left to languages that are closer to the metal, such as C or ASM.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Cops & Robbers
Judah Himango
|
|
|
|
|
|
Yes, I know that I have to spend much time and I knew that c# maybe wouldn't be useful, but...
The major problem is documentation!
Where can I find docs on creating dcm drivers ad all other things...
Does anyone know some links (articles, discussions etc...) or books where to start from?
Before starting coding... documentation is needed...
Thanks
Comet.
|
|
|
|
|
|
Hi, i have a quick question. I have an application installed in a server that reads from Excel, is it necesary to have installed Excel so the application can read the excel files??
|
|
|
|
|
It depends on what you mean by "Excel files", and how they are being read.
If you mean comma separated value files (*.CSV) that a user might open in Excel, then no, you don't need Excel to read them.
If you mean files saved in the Excel format (*.xls), then the answer is "maybe", and you would need to give more information about the server-side component that reads the files. If it uses some form of MS Office automation (which would interop with the Excel runtime callable COM wrappers), then you would need at least some parts of Excel installed on the server.
However, if the server component is using the Jet database engine or some other proprietary driver(s) for accessing Excel, the requirement for installing Excel in and of itself is removed, however a new requirement may arise to support the component(s) reading the spreadsheets (Jet used to require some add-on components to intrepret Excel - don't know if they're a requirement any more, or if they are just installed automatically with Jet).
Hope this helps.
The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
|
|
|
|
|
I am using the jet database engine. I installed an excel viewer but still give me the error. The problem is that the error says: "No error information available: E_FAIL(0x80004005)"...What else could i try?
|
|
|
|
|
You did not previously indicate that you were having a problem or receiving any exception information, so it was a little difficult to give you specific advice.
I don't know what you mean by "Excel Viewer", but if you mean one of the GUI applications that allows viewing Office documents without having all of Office installed, it's unlikely to make a difference.
You need (to share) more information about the server-side component that's trying to read the Excel spreadsheets. As I mentioned before, your results are highly dependent upon what that component is doing. There is no way to give you any considered advice without knowing what the "reader" component is doing and how.
I can give you suggestions based upon the error number you're receiving, but they are not likely to yield much in the way of actual help - you can look up the error number (0x80004005) at:
http://support.microsoft.com[^]
There are numerous hits there, because the error number covers quite a bit of error-related real estate. The first item pertains to the Jet engine, and the fact that if connection properties are not correctly set (using, say, an OleDbConnection with Jet), then the specified error code might return, indicating a failure to find an ISAM.
Somewhere to start, anyway.
The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
|
|
|
|
|
This my string conexion:
strConection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" l.xls@";Extended Properties=Excel 8.0;";
then i build the object:
OleDbConnection oConection = new OleDbConnection(strConection);
and open the conection:
oConexion.Open();--->HERE IS WHERE I GET THE RROR
In my server i have installed:
Microsoft Office Excel Viewer 2003
In my Data Sources(ODBC)
i have no USER DSN, SYSTEM DSN
In my drivers
I have Driver do Microsoft Excel (4.00.6200)
and Microsoft Excel Driver (4.00.6200)
What other information i can give u?
Thanks for all the help
|
|
|
|
|
Okay. First, put double-quotes around your Extended Properties value array. This requirement is not documented, but the lack of quotations around the Extended Properties will mess up the parsing of the connection string (nice, huh? Love those semicolons - the universal separator ) resulting in the exception you're seeing. See below for an example connection string that's confirmed working...
Now, if that doesn't do it:
I don't have a PC without Excel on it at the moment, but I did make a little sample application to read data from Excel spreadsheets. I used the connection string:
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"c:\\mydocument.xls\";Extended Properties=\"Excel 8.0;HDR=Yes;\"";
in my application. One difference is the addition of the HDR extended property. You could also alternately choose to use IMEX as an extended property, but I don't think they'd make a lot of difference, but might be worth a try:
"HDR=Yes;" indicates that the first row contains columnnames, not data
"IMEX=1;" tells the driver to always read "intermixed" data columns as text
Failing all other solutions to your exception problem, I would have to guess that your server has a damaged or missing Indexed Sequential Access Method (ISAM) driver for Excel.
There's an article here[^] that discusses how the Jet Db engine is used to access Excel workbooks.
Here[^] is a link dealing with how to repair missing/corrupted ISAM files.
I hope this helps - after this I've got nothing...
The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
|
|
|
|
|
Ok, ill try with this. Thanks for all ur time. Ill let u know what happened
|
|
|
|
|
Hello,
I have:
this.dataview.Table = this.dataset.Tables["CashFlow"];<br />
dataview.RowFilter = "VersionID = "+Convert.ToInt32(versionID);
When I am using foreach statement to iterate through the dataview table, I can see all the rows, even the ones that have VersionID not equal to versionID specified in the RowFilter property. I thought if you provide the filter, only rows visible in the dataview are the ones that will have proper VersionID. Any ideas what I am doing wrong?
Thank you
|
|
|
|
|
That should work. Are you sure you are iterating through the dataview and not the datatable? You should have something like:
<br />
foreach (DataRowView drv in dataview)<br />
{<br />
}<br />
Can you post your looping code?
|
|
|
|
|
Yes I am positive, here is the loop that I am using:
<br />
for (int i = 0; i < this.dataview.Table.Columns.Count; i++) {<br />
foreach (DataRow val in this.dataview.Table.Rows) {<br />
if (val["IndentLevel"].ToString() == level && val["VersionID"].ToString() == row["VersionID"].ToString()) {<br />
total = total + Convert.ToDecimal(val[i]);<br />
} <br />
}<br />
row[i] = total;<br />
total = 0;<br />
}
|
|
|
|
|
You are, in fact, looping through the rows in the table, not the rows in the dataview. DataView.Table is a reference to the original, unfiltered table. You must loop through the DataRowView objects in the DataView itself as I showed you in my previous post.
Try this:
<br />
for (int i = 0; i < this.dataview.Table.Columns.Count; i++) {<br />
foreach (DataRowView val in this.dataview) {<br />
if (val["IndentLevel"].ToString() == level && val["VersionID"].ToString() == row["VersionID"].ToString()) {<br />
total = total + Convert.ToDecimal(val[i]);<br />
} <br />
}<br />
row[i] = total;<br />
total = 0;<br />
}
|
|
|
|
|
Thank you very much.
I realised that I was looping through the unfiltered table.
|
|
|
|
|
I have a binary file.I want to read it as a String.I read it as array of chars and then copy to a String variable.Then I write a string to the new binary file.
But output file content is not equal to the input file.
ex:// 1.bin is not equal to 2.bin
BinaryReader br;
BinaryWriter bw;
BinaryWriter bw=new BinaryWriter(File.Open("c:\\1.bin", FileMode.Create ));
string str=null;
br=new BinaryReader(File.Open("c:\\2.bin", FileMode.Open ,FileAccess.Read));
char[] ChArray=br.ReadChars(200);
for (int i=0;i<200;i++)
{
str=str+ChArray[i];
}
bw.Write(str);
br.Close();
bw.Close();
|
|
|
|
|
|
You can't read a binary file as text. Read it as bytes instead.
---
b { font-weight: normal; }
|
|
|
|
|