0

I have an object I am using an xml-serializer to serialize. Is it possible to essentially insert an arbitrary Child Root node, and assign certain properties to belong to this root node?

A few properties public int ID { get; set; }

    public object ActivityType { get; set; }

    public int ObjectId { get; set; }

    public string RelatedObjects { get; set; }

    public string Parameters { get; set; }

    public string Tag { get; set; }

    public List<Condition> Conditions { get; set; }

    public bool OnlySaveIfValid { get; set; }

Now, when I serialize this with xml serialize I get:

 <?xml version="1.0" encoding="utf-16"?>
<FNActivity>
  <ID>0</ID>
  <ObjectId>0</ObjectId>
  <Conditions>
   <Condition>
    <Critical>false</Critical>
    <ValidIf>false</ValidIf>
    <IsValid>false</IsValid>
  </Condition>
  <Condition>
    <Critical>false</Critical>
    <ValidIf>false</ValidIf>
    <IsValid>false</IsValid>
    </Condition>
  </Conditions>
 <OnlySaveIfValid>false</OnlySaveIfValid>
</FNActivity>

The Xml I want to get is this:

<?xml version="1.0" encoding="utf-16"?>
<FNActivity>
  <ID>0</ID>
  <ObjectId>0</ObjectId>
       **<VALIDATION>
         <Conditions>
           <Condition>
           <Critical>false</Critical>
           <ValidIf>false</ValidIf>
           <IsValid>false</IsValid>
         </Condition>
         <Condition>
          <Critical>false</Critical>
          <ValidIf>false</ValidIf>
          <IsValid>false</IsValid>
         </Condition>
        </Conditions>
        <OnlySaveIfValid>false</OnlySaveIfValid>
      **</VALIDATION>
  </FNActivity>
4
  • 2
    I think the easiest would be to simply create a VALIDATION class/object to store your conditions in. You could implement IXmlSerializable to manually do it, but it seems overkill. The best is to create an object model that closely matches your XML model, then separate your serialization concerns from your application concerns. Nov 20, 2014 at 15:29
  • @ChrisSinclair I somewhat agree if this is the only instance, however there are multiple areas where we are converting from using XML scehmas that are human written and XML schemas that are written programatically, so readability is still somewhat desired. Also, restructuring code have it serialize the way I want doesn't seem like the correct solution. Nov 20, 2014 at 15:51
  • Well, what is the "correct solution" is relative. You're using the XmlSerializer class so, generally speaking, you can only use it the way it is intended; in that sense, the "correct solution" is to separate your concerns and build your data models as the XmlSerializer deems is correct. If you are working with two different schemas to connect to the same underlying data model, then this is even more important to separate your serialization concerns from your application logic concerns. And as I suggested, you can still leverage IXmlSerializable and insert the node manually. Nov 20, 2014 at 16:31
  • IXmlSerializable ...I will research this. thanks Nov 20, 2014 at 17:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.