{"id":2105,"date":"2014-06-26T20:33:54","date_gmt":"2014-06-26T20:33:54","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=2105"},"modified":"2014-06-26T20:33:54","modified_gmt":"2014-06-26T20:33:54","slug":"reading-andor-writing-xsd-files","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/reading-andor-writing-xsd-files\/","title":{"rendered":"Reading and\/or writing xsd files"},"content":{"rendered":"<p>I wanted to look at reading an xsd (XML schema) file and generate C# source from it in a similar way to xsd.exe. <\/p>\n<p>As XML schema is itself XML I first looked at writing the code using an XmlReader, but whilst this might be an efficient mechanism for reading the file, it&#8217;s a bit of a pain to write the code to process the elements and attributes. So what&#8217;s the alternative ?<\/p>\n<p>Well there is actually a much simpler class that we can use named XmlSchema which, admittedly, will read the xsd into memory, but I&#8217;m not currently looking at performance being an issue.<\/p>\n<p><em>Note: I&#8217;m going to deal with XmlSchema as a reader but there&#8217;s a good example of using it to write an XML schema at <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.xml.schema.xmlschema.aspx\" title=\"XmlSchema Class\" target=\"_blank\">XmlSchema Class<\/a><\/em><\/p>\n<p>Here&#8217;s a quick example of reading a stream (which contains an XML scherma)<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing (XmlReader reader = new XmlTextReader(stream))\r\n{\r\n   XmlSchema schema = XmlSchema.Read(reader, (sender, args) =&gt;\r\n   {\r\n      Console.WriteLine(args.Message);\r\n   });\r\n   \/\/ process the schema items etc. here\r\n}\r\n<\/pre>\n<p>The <em>Console.WriteLine(args.Message)<\/em> code is within a ValidationEventHandler delegate which is called when syntax errors are detected.<\/p>\n<p>Once we successfully get an XmlSchema we can interact with it&#8217;s Items, for example here some code which is intended loop through all complex types and then process the elements and attributes within each complex type<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nforeach (var item in schema.Items)\r\n{\r\n   XmlSchemaComplexType complexType = item as XmlSchemaComplexType;\r\n   if (complexType != null)\r\n   {\r\n      XmlSchemaSequence sequence = complexType.Particle as XmlSchemaSequence;\r\n      if (sequence != null)\r\n      {\r\n         foreach (var seqItem in sequence.Items)\r\n         {\r\n            XmlSchemaElement element = seqItem as XmlSchemaElement;\r\n            if (element != null)\r\n            {\r\n               \/\/ process elements\r\n            }\r\n         }\t\t\r\n      }\r\n      foreach (var attributeItem in complexType.Attributes)\r\n      {\r\n         XmlSchemaAttribute attribute = attributeItem as XmlSchemaAttribute;\r\n         if (attribute != null)\r\n         {\r\n            \/\/ process attributes\r\n         }\r\n      }\r\n   }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to look at reading an xsd (XML schema) file and generate C# source from it in a similar way to xsd.exe. As XML schema is itself XML I first looked at writing the code using an XmlReader, but whilst this might be an efficient mechanism for reading the file, it&#8217;s a bit of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[70],"tags":[],"class_list":["post-2105","post","type-post","status-publish","format-standard","hentry","category-xml"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=2105"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2105\/revisions"}],"predecessor-version":[{"id":2108,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2105\/revisions\/2108"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=2105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=2105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=2105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}