Search code examples
c#xmlxml-generation

Creating a XmlDocument with hardcoded schema C#


I have an application that works with XML file. It works great if the xml file is present, however there is also a requirement, that the project generates a blank XML file and then writes to it in case the output file is not present.

The xml file has a quite complicated schema, something like the following:

<?xml version="1.0" encoding="UTF-8"?>
<Results>
    <Result>
        <WorkorderId>45</WorkorderId>
        <WorkerId>13</WorkerId>
        <DeviceId>38954178</DeviceId>
        <Latitude>45.234</Latitude>
        <Longitude>19.54</Longitude>
        <Note>[all is good]</Note>
        <Operations>
            <Operation>
                <OperationId>23</OperationId>
                <Result>Success</Result>
                <ParsedInformation>
                    <Info>parsed data</Info>
                    <Info>more parsed data</Info>
                </ParsedInformation>
            </Operation>
            <!-- more operations ... -->
        </Operations>
    </Result>
    <!-- more results ... -->
</Results>

I am wondering how would I create a XmlDocument like this, so I can write the results to it? Is there a "best practice" about hard coding the schema somewhere in the project, etc?

Please note that I am new to Xml so any additional information/literature would be very welcome.


Solution

  • To my knowledge you have to build the document node by node. Perhaps you can save an empty tree as template.