views:

526

answers:

3

Hello!

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.

A: 

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

Andersson
+1  A: 

If you actually have an XML Schema for your document format, then you can use the xsd.exe utility to generate a bunch of XmlSerializer-compatible classes for it - elements become classes, their attributes and children become properties, element sequences become collections, and so on. You'll also get proper types so long as your schema defines them (i.e. xs:int becomes int, and so on). Then you can build an object tree in memory using those classes, and serialize it using XmlSerializer.

Pavel Minaev
+3  A: 

I would usually create a set of classes that would contain the data and tag them with appropriate XmlSerializer attributes to make sure they get serialized to the format you are expecting. This is a pretty good resource: http://msdn.microsoft.com/en-us/library/2baksw0z(VS.85).aspx

In your case you would have the following classes (untested):

[XmlRoot("Results")]
public class Results
{
  List<Result> results = new List<Result>();

  [XmlElement("Result")]
  List<Result> Results {get{return results;}}
}

public class Result
{
  List<Operation> operations = new List<Operation>();
  int WorkorderId {get; set;}
  .... other fields
  string Note{get;set;}
  List<Operation> Operations {get{return operations;}}
}

public class Operation
{
  List<string> parsedInformation = new List<string>();
  int OperationId {get;set;}
  ....
  [XmlArray("ParsedInformation")]
  [XmlArrayItem("Info")]
  List<string> ParsedInformation{get{return parsedInformation;}}
}

Later you use XmlSerializer class to serialize it to xml:

XmlSerializer serializer = new XmlSerializer(typeof(Results));
StringBuilder sb = new StringBuilder(); //this will contain the xml
serializer.Serialize(new TextWriter(sb), resultsObj);

Obviously you can also deserialize data from string to object.

Please note that if you have a XSD schema for your xml, you can use xsd.exe tool to generate the code for you.

Grzenio