10

I am new to XML coding. I have an XML file with which I generated the Schema file using XSD tool, then generated the class file using Xsd2Code tool.

Now I have a file called Timesheet.Designer.cs with all the class information and Serialization methods. Can I use the same file in the project or keep it as a base class and generate a separate file with Timesheet.cs for accessing this classes and modifications? Can anyone point to me a good tutorial or example which shows how can I use this in my application to Read and write to the XML file?

I have many complexType elements that are in the XML file. So the tool generated different classes for all these ComplexTypes as well.

If someone already has some code please post it here.

Jeff LaFay
  • 12,882
  • 13
  • 71
  • 101
user843813
  • 111
  • 1
  • 1
  • 6
  • This could possibly be a namespace issue. You need to tell Xsd2Code what namespace to generate the classes in, and then you need to add a 'using' directive to get access to those classes.. – MattDavey Jul 14 '11 at 07:51

2 Answers2

10

Xsd2Code can generate save & load methods which save & load the entities directly to an xml file.

You need to make sure you set the correct options when generating the classes, either through the Xsd2Code properties page, or via the command line, depending on how you're using it - see this image (especially the serialization section).

enter image description here

To load, edit, and save your xml file:

// SchemaClass is the root class generated by Xsd2Code
SchemaClass data = SchemaClass.LoadFromFile("myData.xml");

data.SomeElement.SomeProperty = "foo";

data.SaveToFile("myData.xml");
MattDavey
  • 8,897
  • 3
  • 31
  • 54
3

You can mark it as partial class or use it as a base class. Keep it in a separate file designer.cs is fine.

That way, every time you regenerate the file, your custom code will not be overwritten.

Mrchief
  • 75,126
  • 20
  • 142
  • 189
  • 1
    I was looking for a tutorial which explains how to use the Class generated by XSD2Code. – user843813 Jul 14 '11 at 06:36
  • What do you mean by 'use'? What exactly are you trying to do? What have you tried so far? Have you got any error message? Please be more specific... – MattDavey Jul 14 '11 at 07:50
  • 1
    @MattDavey: I want to read and write to the XML file using this class. So I am looking for some code which has already developed with the Class file generated by XSD2Code. – user843813 Jul 14 '11 at 08:59