|
PIEBALDconsult wrote: The set accessor should then be private.
No Xml serialization (I am getting tired typing that!) will choke on that. Just try it!
|
|
|
|
|
|
I had no trouble with it.
namespace Template
{
public partial class MyClass : System.Xml.Serialization.IXmlSerializable
{
public MyClass
(
)
{
}
public MyClass
(
string Name
)
{
this.Name = Name ;
return ;
}
public string
Name
{
get ;
private set ;
}
public void
WriteXml
(
System.Xml.XmlWriter Writer
)
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument() ;
doc.AppendChild ( doc.CreateElement ( "MyClass" ) ) ;
doc.DocumentElement.InnerText = this.Name ;
doc.WriteTo ( Writer ) ;
Writer.Close() ;
return ;
}
public void
ReadXml
(
System.Xml.XmlReader Reader
)
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument() ;
doc.Load ( Reader ) ;
Reader.Close() ;
this.Name = doc.DocumentElement.InnerText ;
return ;
}
public System.Xml.Schema.XmlSchema
GetSchema
(
)
{
return ( null ) ;
}
public override string
ToString
(
)
{
return ( this.Name ) ;
}
}
public partial class Template
{
private static System.Xml.Serialization.IXmlSerializable
Write
(
System.Xml.Serialization.IXmlSerializable Subject
)
{
Subject.WriteXml ( System.Xml.XmlWriter.Create ( @"C:\X.xml" ) ) ;
return ( Subject ) ;
}
private static System.Xml.Serialization.IXmlSerializable
Read
(
System.Xml.Serialization.IXmlSerializable Subject
)
{
Subject.ReadXml ( System.Xml.XmlReader.Create ( @"C:\X.xml" ) ) ;
return ( Subject ) ;
}
[System.STAThreadAttribute()]
public static int
Main
(
string[] args
)
{
int result = 0 ;
try
{
if ( args.Length > 0 )
{
MyClass x = new MyClass ( args [ 0 ] ) ;
Write ( x ) ;
MyClass y = new MyClass() ;
Read ( y ) ;
System.Console.WriteLine ( y.Name ) ;
}
}
catch ( System.Exception err )
{
System.Console.Write ( err.Message ) ;
}
return ( result ) ;
}
}
}
|
|
|
|
|
Implementing your own custom IXmlSerializable is cheating For starters you need to use the XmlSerializer. And you code should just represent a basic C# object.
Example:
<font color="Blue">using</font> System<font color="DarkBlue">;</font>
<font color="Blue">using</font> System<font color="DarkBlue">.</font>Xml<font color="DarkBlue">.</font>Serialization<font color="DarkBlue">;</font>
<font color="Blue">using</font> System<font color="DarkBlue">.</font>IO<font color="DarkBlue">;</font>
<font color="Blue">public</font> <font color="Blue">class</font> <font color="Teal">Foo</font>
<font color="DarkBlue">{</font>
<font color="Blue">public</font> <font color="Blue">int</font> Bar <font color="DarkBlue">{</font> <font color="Blue">get</font><font color="DarkBlue">;</font> <font color="Blue">private</font> <font color="Blue">set</font><font color="DarkBlue">;</font> <font color="DarkBlue">}</font>
<font color="DarkBlue">}</font>
<font color="Blue">class</font> <font color="Teal">Entrypoint</font>
<font color="DarkBlue">{</font>
<font color="Blue">static</font> <font color="Blue">void</font> Main<font color="DarkBlue">(</font><font color="DarkBlue">)</font>
<font color="DarkBlue">{</font>
<font color="Teal">XmlSerializer</font> ser <font color="DarkBlue">=</font> <font color="Blue">new</font> <font color="Teal">XmlSerializer</font><font color="DarkBlue">(</font><font color="Blue">typeof</font><font color="DarkBlue">(</font><font color="Teal">Foo</font><font color="DarkBlue">)</font><font color="DarkBlue">)</font><font color="DarkBlue">;</font>
<font color="Blue">using</font> <font color="DarkBlue">(</font><font color="Teal">Stream</font> s <font color="DarkBlue">=</font> <font color="Teal">File</font><font color="DarkBlue">.</font>OpenWrite<font color="DarkBlue">(</font><font color="Maroon">"foo.xml"</font><font color="DarkBlue">)</font><font color="DarkBlue">)</font>
<font color="DarkBlue">{</font>
ser<font color="DarkBlue">.</font>Serialize<font color="DarkBlue">(</font>s<font color="DarkBlue">,</font> <font color="Blue">new</font> <font color="Teal">Foo</font><font color="DarkBlue">(</font><font color="DarkBlue">)</font><font color="DarkBlue">)</font><font color="DarkBlue">;</font>
<font color="DarkBlue">}</font>
<font color="DarkBlue">}</font>
<font color="DarkBlue">}</font>
Fails!
|
|
|
|
|
That's just crazy talk; if the class doesn't implement IXmlSerializable then clearly it can't be serialized to XML, so don't try.
This is like trying to use a DataAdapter on a query with a join or a view or something and then complaining that .Update won't work.
And as I said, "Not if it's done right"; I done it right, you didn't. I win, neener neener neener!
|
|
|
|
|
PIEBALDconsult wrote: That's just crazy talk; if the class doesn't implement IXmlSerializable then clearly it can't be serialized to XML, so don't try.
The way I showed you is exactly how web services does it.
[update] I think, it's 5:30 am, I just woke for a smoke, not thinking really [update]
modified on Saturday, March 22, 2008 11:38 PM
|
|
|
|
|
This is a continuation of the sample:
You might say, then use the XmlIgnore attribute, but in that case the property is never emitted, but what if you want to emit it, but you simply dont care about the result after deserializing it?
|
|
|
|
|
Why write a class in a particular (bad) way just to bypass a flaw in some other brain-dead class?
Dealing with a class that someone else wrote which doesn't implement the desired serialization is another matter entirely, but when you're writing the class you have control.
If you're writing a class intending it to be serialized, then it should be marked with SerializableAttribute and implement ISerializable and/or IXmlSerializable as appropriate.
"Use the right tool for the right job." -- Scotty, et al
|
|
|
|
|
THAT'S IT! That's why I had to do something similar to this coding horror.
|
|
|
|
|
You missed
public virtual Property { get {...} set {..}}
|
|
|
|
|
Well that is what I was alluding to when I said:
"
The set accessor should then be private. However, the base class' contract may not allow that and then you're stuck.
"
(In a different branch.)
However in your example, the derived class should at least call base.Property .
If the base class is abstract then you're stuck (but an exception should be thrown or something!). However, I argue that an abstract class should probably not specify that a set is required, which may be a whole new topic.
|
|
|
|
|
Found this C# beauty in 3 places in a particular class i'm updating.
<br />
if (this == null || .....<br />
{<br />
}<br />
I'm guessing the person that wrote that was going to be checking a field to see if it was null but none of the assignment code within the {} indicates what that might have been.
|
|
|
|
|
maybe it's some philosophic-thing...
(yes|no|maybe)*
|
|
|
|
|
Sanity checking?
|
|
|
|
|
The object might feel a bit of insecure and thus need to re-assure itself sometimes.
|
|
|
|
|
It's an AI piece of code - it's the equivalent to "I think therefore I am, you aren't me so you don't matter."
|
|
|
|
|
AI is much clearer in VB, as in
If (Me Is Nothing) Then
' I don't exist, so I can't think (straight? at all?)
End If
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Luc Pattyn wrote: Me Is Nothing
that is not philosophy, but an inferiority complex
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP blog: TDD - the Aha! | Linkify!| FoldWithUs! | sighist
|
|
|
|
|
peterchen wrote: inferiority complex
not being fluent in VB.NET does that to people; I'm just a C# guy.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Luc Pattyn wrote: not being fluent in VB.NET does that to people;
I'm not fluent, and that doesn't make me feel inferior.
Conclusion? I'm not people.
Wow, that was easy
In case you missed it, I meant the language (not you) expresses its inferiority complex with "me is nothing"
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP blog: TDD - the Aha! | Linkify!| FoldWithUs! | sighist
|
|
|
|
|
OK, so you'd not want to assign a value to an object that was null, but it brings up an interesting discussion on the use of "if (this != null)".
Take a look at this discussion on MSN[^] about this != null in C#.
cheers,
Chris Maunder
CodeProject.com : C++ MVP
|
|
|
|
|
|
if (this == null || .....
{
// assigned values to self
}
You can get into the true part there, you just have to do a bit of IL hacking.
Since instance methods are like static methods with a "this" param as the first arg in il.
you can invoke such method and pass null to "this"
Im not saying you should do this, but its possible
I happened to get that problem when I was doing the subclass proxies for NAspect (our AOP framework)
It works as long as you don't access any member variables :-P
|
|
|
|
|
Hi Guys,
I'm going to write an article for beginners on how to refactor deeply nested "if" statements, and I was wondering if anyone here had any VB.NET or C# code that they could share that had a few "if" statements at least five levels deep that I could use as a refactoring example...do I have any takers?
|
|
|
|
|
Philip Laureano wrote: article for beginners on how to refactor deeply nested "if" statements
You won't endorse the use of multiple return in one function, no?
And surly not even name the dreaded word GOTO ?
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all. Douglas Adams, "Dirk Gently's Holistic Detective Agency"
|
|
|
|