开发者

Validate C# object to XSD and generate xml output

开发者 https://www.devze.com 2023-03-19 15:08 出处:网络
I have an object which has public properties. Say for e.g. an object called Regions and it looks something like this

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

精彩评论

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