如何编程生成一个类型的XML模式? [英] How do I programmatically generate an xml schema from a type?

查看:155
本文介绍了如何编程生成一个类型的XML模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想产生一个xs:架构从任何.NET类型编程。我知道我可以使用反射和迭代的公共属性生成它,但有一个内置的方式?

例如:

  [Serializable接口]
公共类Person
{
    [的XmlElement(ISNULLABLE =假)]公共字符串名字{获得;组; }
    [的XmlElement(ISNULLABLE =假)]公共字符串名字{获得;组; }
    [的XmlElement(ISNULLABLE = TRUE)]公共字符串PHONENO {获得;组; }
}
 

所需的输出:

 < XS:模式的xmlns:XS =htt​​p://www.w3.org/2001/XMLSchema>
  < XS:元素的名称=人型=人/>
  < XS:复杂类型的名称=人>
    < XS:序列>
      < XS:元素的minOccurs =0的maxOccurs =1的形式=不合格NAME =姓类型=XS:字符串/>
      < XS:元素的minOccurs =0的maxOccurs =1的形式=不合格NAME =姓氏类型=XS:字符串/>
      < XS:元素的minOccurs =0的maxOccurs =1的形式=不合格NAME =PHONENO类型=XS:字符串/>
    < / XS:序列>
  < / XS:复杂类型>
< / XS:模式>
 

解决方案

所以这个作品,我想这是不是因为丑,因为它似乎是:

  VAR SOA preflectionImporter =新的SOA preflectionImporter();
VAR xmlTypeMapping = SOA preflectionImporter.ImportTypeMapping(typeof运算(人));
VAR xmlSchemas =新XmlSchemas();
VAR XMLSCHEMA =新的XmlSchema();
xmlSchemas.Add(XML模式);
VAR xmlSchemaExporter =新XmlSchemaExporter(xmlSchemas);
xmlSchemaExporter.ExportTypeMapping(xmlTypeMapping);
 

我仍然希望有一个2线的解决方案在那里,好像应该有,感谢小费@dtb


修改 只是踢,这里的2行版本(自去precating幽默)

  VAR typeMapping =新的SOA preflectionImporter()ImportTypeMapping(typeof运算(人))。
新XmlSchemaExporter(新XmlSchemas {新的XmlSchema()})ExportTypeMapping(typeMapping)。
 

I'm trying to generate an xs:schema from any .net Type programmatically. I know I could use reflection and generate it by iterating over the public properties, but is there a built in way?

Example:

[Serializable]
public class Person
{
    [XmlElement(IsNullable = false)] public string FirstName { get; set; }
    [XmlElement(IsNullable = false)] public string LastName { get; set; }
    [XmlElement(IsNullable = true)] public string PhoneNo { get; set; }
}

Desired Output:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Person" type="Person" />
  <xs:complexType name="Person">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" form="unqualified" name="FirstName" type="xs:string" />
      <xs:element minOccurs="0" maxOccurs="1" form="unqualified" name="LastName" type="xs:string" />
      <xs:element minOccurs="0" maxOccurs="1" form="unqualified" name="PhoneNo" type="xs:string" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>

解决方案

So this works, I guess it wasn't as ugly as it seemed:

var soapReflectionImporter = new SoapReflectionImporter();
var xmlTypeMapping = soapReflectionImporter.ImportTypeMapping(typeof(Person));
var xmlSchemas = new XmlSchemas();
var xmlSchema = new XmlSchema();
xmlSchemas.Add(xmlSchema);
var xmlSchemaExporter = new XmlSchemaExporter(xmlSchemas);
xmlSchemaExporter.ExportTypeMapping(xmlTypeMapping);

I was still hoping there was a 2 line solution out there, it seems like there should be, thanks for the tip @dtb


EDIT Just for kicks, here's the 2 line version (self deprecating humor)

var typeMapping = new SoapReflectionImporter().ImportTypeMapping(typeof(Person));
new XmlSchemaExporter(new XmlSchemas { new XmlSchema() }).ExportTypeMapping(typeMapping);

这篇关于如何编程生成一个类型的XML模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆