Generating C# classes from XSD
I generate C# classes for this XSD schema using xsd.exe or Xsd2Code:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="student" type="personinfo"/>
<xs:element name="employee" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
I get similar results in both cases:
...
[System.Xml.Serialization.XmlRootAttribute("student", Namespace="", IsNullable=false)]
public partial class personinfo
{
...
public string firstname { ... }
public string lastname { ... }
}
It seems strange to me that employee
is not referenced anywhere and that some information is lost after code generation. For example, if I look at the generated code it is not obvious that this XML can be created:
<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>
Why is information about schema elements not included into the generated code? Are there any other tools that can somehow save this information in the generated classes?
They remain unreferenced until you actually use them. Try something like this:
<xs:complexType name="PersonInfo">
<xs:sequence>
<xs:element name="student" type="personinfo"/>
<xs:element name="employee" type="personinfo"/>
</xs:sequence>
</xs:complexType>
链接地址: http://www.djcxy.com/p/34322.html
上一篇: 如何插入&nbsp; 在XSLT中
下一篇: 从XSD生成C#类