开发者

Validate C# object to XSD and generate xml output

I have an object which has public properties.

Say for e.g. an object called Regions and it looks something like this

public class Region
{
// public properties
 string name;

 string description;

 string timezone;

 string regionURL;

 string regioncode;

}

Now I want to validate this object to region XSD which basically doesn't have all the properties which are present in the region object. And for the properties which are in the XSD I want to generate the output as XML.

开发者_高级运维

The region XSD looks like

<xs:schema>
<xs:complexType name="region">
    <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="URl" type="xs:string"/>
      <xs:element name="code" type="xs:int"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

Is there something inbuilt in .net which can be helpful?


You could look into implementing IXMLSerializable on your class, and generate the XML serialization via XSD that way.

There's an article about it on codeproject here


You have two choices.

  1. Generate code from your region schema so you have a RegionFromXsd type. Map the data from an instance of your Regions type to your new RegionFromXsd type. Then serialise your RegionFromXsd type into XML document.

  2. Populate an instance of your Regions type, serialise it to XML, then apply an XSLT which transforms the XML to a form which complies with your region XSD schema.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜