1

I have a requirement like i need to write an entity class in C# which can hold xml data. I want to avoid overhead of checking the well-formedness of saved xml. I have a corresponding column with type XML. Do we have xml data type or some class which can be used as a class field to hold xml. Thanks in advance

Update: The service using this Entity class is WCF service and in future we are making it REST compatible. Will XmlDocument or XElement work with it?

MaxRecursion
  • 4,773
  • 12
  • 42
  • 76

2 Answers2

4

There are a number of ways, two of which are string and XmlDocument.

string would be 'easier' for fragments and not-well-formed XML, but XmlDocument can be configured with options to allow fragments; you'll have more trouble with ill-formed data though.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
0

if you describe your object in a XSD file you can get a compiler to generate all your C# classes automatically and easily regenerate them when you make changes.

This makes XML / C# a breeze. You can go to other languages too using equivilent generators.

See the tools described here: XSDObjectGen.exe vs XSD.exe

I believe the XSD.exe tool will read in example XML and do most of the work of producing an XSD which you can refine.

If you don't need support for a fixed XML/XSD format file why can't you make any class serialize to XML by using the [Serializable] class attribute and .Net APIS to serialize/deserialize?

Community
  • 1
  • 1
AnthonyLambert
  • 8,768
  • 4
  • 37
  • 72