472,791 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,791 software developers and data experts.

XmlSerializer for single level serialization of objects ?

Ok the XmlSerializer is nice for my need.

BUT I what I need (maybe the XmlSerializer isnt what I need) is to be
able to take the following
objects

A Person who has a collection of Addresses

Person
(fname)
(lname)
Address
(Street)
(City)
(State)
(Zip)

And Serialize the Person object Seperatly from the Addresses.

What is the best way to do this ?

Inverse recursion ?

What I would like in the end are 2 strings, 1 representing the Person
and 1 representing the address.

If there were 2 addresses then 3 strings, 1 for the person, and 1 for
EACH address.

Thanks

Chris

Dec 6 '06 #1
4 1895
Hi Chris,

Could you just mark the address property of the Person object to be ignored
by the serializer (XmlIgnore i think). Serialise the Person to get your
first string, then iterate over the addresses and serialize them one by one?

ta,
Andrew

<cw******@gmail.comwrote in message
news:11**********************@16g2000cwy.googlegro ups.com...
Ok the XmlSerializer is nice for my need.

BUT I what I need (maybe the XmlSerializer isnt what I need) is to be
able to take the following
objects

A Person who has a collection of Addresses

Person
(fname)
(lname)
Address
(Street)
(City)
(State)
(Zip)

And Serialize the Person object Seperatly from the Addresses.

What is the best way to do this ?

Inverse recursion ?

What I would like in the end are 2 strings, 1 representing the Person
and 1 representing the address.

If there were 2 addresses then 3 strings, 1 for the person, and 1 for
EACH address.

Thanks

Chris

Dec 6 '06 #2
Hmm....thats a way...not bad.....

What I would really like to do is be able through reflection determine
if a property base type implments IEnumerable.

I have an app that I have several functions I need to determine if a
given propety is a a type of Collection

It could (and in most cases is) a Generic Collection

It could be , and array , an array list , etc though.

How can I determine if its A collection, or enumerable ?

I hate to impose contratints like that on Domain Design that all
collections must be ignored....etc.

BUT its better than what I had so far.

If I could just figure out how to determine if a property implments
IEnum, etc...

Thanks

Chris

Andrew Brook wrote:
Hi Chris,

Could you just mark the address property of the Person object to be ignored
by the serializer (XmlIgnore i think). Serialise the Person to get your
first string, then iterate over the addresses and serialize them one by one?

ta,
Andrew

<cw******@gmail.comwrote in message
news:11**********************@16g2000cwy.googlegro ups.com...
Ok the XmlSerializer is nice for my need.

BUT I what I need (maybe the XmlSerializer isnt what I need) is to be
able to take the following
objects

A Person who has a collection of Addresses

Person
(fname)
(lname)
Address
(Street)
(City)
(State)
(Zip)

And Serialize the Person object Seperatly from the Addresses.

What is the best way to do this ?

Inverse recursion ?

What I would like in the end are 2 strings, 1 representing the Person
and 1 representing the address.

If there were 2 addresses then 3 strings, 1 for the person, and 1 for
EACH address.

Thanks

Chris
Dec 6 '06 #3
System.Diagnostics.Debug.WriteLine("Addresses is " +
(typeof(IEnumerable).IsAssignableFrom(myObject.Get Type().GetProperty("Addresses").PropertyType)
? String.Empty : "not ") + "IEnumerable");

That should determine if your property is IEnumerable, although since
ICollection is derived from IEnumerable, it'd be better to check for
ICollection if you want to exclude them. But this is not behavior you can
dictate to the serializer (unless you can get it to fire the UnknownElement
event)
Dec 6 '06 #4
BINGO !

Thanks a TON !

It was driving me nuts, I was calling TypeOf differently and missing
the IsAssignableFrom

So NOW I can loop through my properties

You are 100% correct about the ICollection (I want thinking of the fact
ICollection is derived from IEnumerable)

What I plan to do as far as the serializer goes is loop from the Bottom
Up setting the collections to nothign after they have been individually
serialized. That Ive got ok, its not clean but it works.
Thanks again......


Keith Patrick wrote:
System.Diagnostics.Debug.WriteLine("Addresses is " +
(typeof(IEnumerable).IsAssignableFrom(myObject.Get Type().GetProperty("Addresses").PropertyType)
? String.Empty : "not ") + "IEnumerable");

That should determine if your property is IEnumerable, although since
ICollection is derived from IEnumerable, it'd be better to check for
ICollection if you want to exclude them. But this is not behavior you can
dictate to the serializer (unless you can get it to fire the UnknownElement
event)
Dec 7 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stuart Robertson | last post by:
I am trying to find a solution that will allow me to use XmlSerializer to serialize/deserialize a collection of objects where a given object is shared between two or more other objects, and not...
8
by: Harris Boyce | last post by:
Hello, I'm trying to use the FOR XML EXPLICIT clause with SQL Server to deserialize data from my database into a strongly-typed collection object that I will use throughout my application. I...
1
by: Bluetears76 | last post by:
Hi I have a hirachy of classes which are Message(base), then FileMessage and ChatMessage (extended) I want to serialize the objects and when i am deserizaling i dont know if i am getting...
3
by: MattB | last post by:
Hello I got this working but it is not how I really want it, basically I have an xml file which has a root of <test> and can be filled with 3 different types of <question> elements with different...
12
by: SJD | last post by:
I've just read Christoph Schittko's article on XmlSerializer: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxmlnet/html/trblshtxsd.asp . . . and very informative it is too....
4
by: Steve Long | last post by:
Hello, I hope this is the right group to post this to. I'm trying to serialize a class I've written and I'd like to be able to serialze to both binary and xml formats. Binary serialization is...
10
by: Henrik Dahl | last post by:
Hello! I have an xml schema which has a date typed attribute. I have used xsd.exe to create a class library for XmlSerializer. The result of XmlSerializer.Serialize(...) should be passed as the...
9
by: =?Utf-8?B?ai5hLiBoYXJyaW1hbg==?= | last post by:
Hi, I have a schema that has an optional element, fieldTag4000Field. If the element is omitted from the XML request, when it is deserialized, it will be null when I check it - which is fine. ...
5
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I am using XML serialization for the first time and I have noticed something unexpected. The object I am serializing contains a field private NumericSettings _numericSettings; public...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.