Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom XmlSerializer

Status
Not open for further replies.

tektipster79

Programmer
Oct 1, 2007
34
US
I'm making a custom XmlSerializer that I only need to use for serializing (my classes are populated by a SqlDataReader). I need to loop through all of the elements and check the value of a property. How can I loop through the elements in the WriteXml method? I don't seem to have access to a XmlNode type there.
 
Can you post some code or add detail. Are you trying to serialize populated objects or take the result of a SqlDataReader and write it out in xml format?
 
I have populated objects that I need to serialize. I need to use a custom serializer, because I to be able to selectively choose items to serialize. For instance, in the example below, I might need to show the first "type1" but hide the 2nd and 3rd "type1" elements.

<ElementA>
<type1></type1>
<type1></type1>
<type1></type1>
</ElementA>
<ElementA>
...
</ElementA>
 
I see. I would probable add and remove object before going into serialization or remove the nodes after. If you find your solution I'd be interested in seeing it
 
I really need to do the altering during serialization so that I don't change the base storage of the data. I don't want to remove the nodes afterwards either because that means I would have to hold all that data in memory again. I want to be able to check a property that each node has and either write it or not write it into a memory stream to send back to the client.

I guess since I don't need the deserialization functionality I am better off just using a plain XmlWriter since that's what the serializer is using anyway.
 
One question though, I'm trying to come up with a clean way to cycle through all of the populated classes that were generated by xsd.exe. Any suggestions?
 
Theres no rule that says you have to use a serializer. Just create an XMLOutput() method for each of your objects that only writes the data you want to store.

If you have objects within objects, just recurse through them by calling XmlOutput() of member classes within the parent class XmlOutput()

The parent class should be the one that actually does the saving to disk, the rest just add thier nodes, or to make it easy, just add a string, then the parent class can dump the string into an XML file.
 
Could you not put the objects into a dll and share the dll across client and server. You could then just de-serilize the xml to objects?

Age is a consequence of experience
 
NeilTrain, that sounds like the direction I was looking for. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top