Deserializing child elements as attributes of parent

Posted by LloydPickering on Stack Overflow See other posts from Stack Overflow or by LloydPickering
Published on 2010-06-15T11:50:50Z Indexed on 2010/06/15 11:52 UTC
Read the original article Hit count: 149

Filed under:
|

I have XML files which I need to deserialize. I used the XSD tool from Visual Studio to create c# object files. the generated classes do deserialize the files except not in the way which I need.

I would appreciate any help figuring out how to solve this problem.

The child elements named 'data' should be attributes of the parent element 'task'.

A shortened example of the XML is below:

<task type="Nothing" id="2" taskOnFail="false" >
    <data value="" name="prerequisiteTasks" />
    <data value="" name="exclusionTasks" />
    <data value="" name="allowRepeats" />
    <task type="Wait for Tasks" id="10" taskOnFail="false" >
        <data value="" name="prerequisiteTasks" />
        <data value="" name="exclusionTasks" />
        <data value="" name="allowRepeats" />
    </task>
    <task type="Wait for Tasks" id="10" taskOnFail="false" >
        <data value="" name="prerequisiteTasks" />
        <data value="" name="exclusionTasks" />
        <data value="" name="allowRepeats" />
    </task>
</task>

The Class definition I am trying to deserialize to is in the form:

public class task {
    public string prerequisiteTasks {get;set;}
    public string exclusionTasks {get;set;}
    public string allowRepeats {get;set;}

    [System.Xml.Serialization.XmlElementAttribute("task")]
    public List<task> ChildTasks {get;set;}
}

The child 'task's are fine, but the generated files put the 'data' elements into an array of data[] rather than as named members of the task class as I need.

© Stack Overflow or respective owner

Related posts about c#

Related posts about xml-serialization