开发者

Serialize all properties of class to XML

开发者 https://www.devze.com 2022-12-17 00:52 出处:网络
I have a load of classes and I need to generate an XML schema for these classes. The easiest way I ca开发者_如何学Pythonn think to do this is to create the classes, serialize as XML and then run XSD

I have a load of classes and I need to generate an XML schema for these classes.

The easiest way I ca开发者_如何学Pythonn think to do this is to create the classes, serialize as XML and then run XSD on this XML.

However, if I don't set the properties of a class, no XML node is created and therefore the XSD doesn't pick it up.

Is there a way for me to tell the XML serializer to serialize ALL properties of a class, rather than just the ones that have values set?

Hopeful, more than expecting! Duncan


XSD.EXE can generate schemas from an assembly. Just specify the /type switch.


Use metadata attribute

[XmlElement(IsNullable=true)]

It will, however, add extra attribute xsi:nil="true" to the serialized output, if the field is null.


Sure. Implement IXmlSerializable and you have complete control.

Also, I am sure you already know there are a couple different ways to parse/validate Xml… and since you are on the Microsoft platform… these tools (available on MSDN) come in handy (auto generate C# code) if you decide to serialize/deserialize to/from a schema: xsd.exe and xsdObjectGen.exe (more options).

0

精彩评论

暂无评论...
验证码 换一张
取 消