tags:

views:

32

answers:

1

I have a settings object which I serialize to disk. When my application loads, it deserialises the object then uses it.

Let's say I have one XML file (the primary file) which is complete and I have a subset XML document which is incomplete. If I wanted to overwrite the primary with whatever the subset had and return a complete XML document which can be deserialized, how would I do that?

For a concrete example, let's say my settings class looks like this:

public class MySettings { 
public string DateFormat { get; set; }
public bool SendSMS { get; set; }
public AddressSettings AddressSettings { get; set; }
}

public class AddressSettings { 
public bool ShowPostCode { get; set; }
}

And the primary serialized XML looks like this:

<MySettings>
<DateFormat>mm/DD/yyyy</DateFormat>
<SendSMS>False</SendSMS>
<AddressSettings>
<ShowPostCode>True</ShowPostCode>
</AddressSettings>
</MySettings>

And the subset XML looks like this:

<MySettings>
<AddressSettings>
<ShowPostCode>False</ShowPostCode>
</AddressSettings>
</MySettings>

The result XML should look like this:

<MySettings>
<DateFormat>mm/DD/yyyyM</DateFormat>
<SendSMS>False</SendSMS>
<AddressSettings>
<ShowPostCode>False</ShowPostCode>
</AddressSettings>
</MySettings>

How do I do that concisely in C#?

A: 

Use XmlWriter? This will essentially allow you to build a new document from the two existing ones (by using XmlReader). For example, rewrite the root (MySettings) and any members before proceeding to add all members of the subset while disregaring its root.

Mr. Disappointment
I tried to code your suggsetion up but didn't get anywhere. Could you provide a short example?
Kias