0

I have a class, which is serialized using the DataContractSerializer class. Now I want to update a single property (a string) of the serialized object.

For some reason, I can't find a functionality for this, so my only option would be to serialize the complete object - which can get quite big. It seems a bit of overkill to serialize several hundreds of lines just to change a single string element. Is there no better approach to this?

Roland Deschain
  • 2,211
  • 19
  • 50
  • You are using a Data Contract which uses Xml Serialization. You can load the xml using other Net Libraries and then parse file and change one item. I prefer using the Xml Linq library to do this. – jdweng Oct 07 '20 at 09:57
  • @jdweng Thanks, but I tried to use the `XDocument` and `XmlDocument` class, both of which wouldn't work - meaning they wouldn't find the needed Element. [https://stackoverflow.com/questions/544310/best-way-to-change-the-value-of-an-element-in-c-sharp/32027344](I used this method.). I thought the problem was, that for exampl a property called `Name` is saved with a tag prefix like `d2p1:Name` (I think, not currently on the machine). Maybe I have to define a custom Name for the item... – Roland Deschain Oct 07 '20 at 12:14
  • I've use XDocument a lot and always able to find a tag. If you are not finding a tag it either means you need a namespace or the tag is a descendent and not a Element. Check Xml to see if there is a default namespace xmlns="URL" ( a namespace with no prefix – jdweng Oct 07 '20 at 12:53
  • @jdweng Hmm, I think you pointed me to the issue. I'll test it out, thanks! :) – Roland Deschain Oct 08 '20 at 06:25

0 Answers0