继承和实现的 .NET 类型的 XML 架构
请考虑以下三种 .NET 类型:我有一个接口、一个抽象类和一个具体类。我的问题是如何编写 XML 架构以包含来自接口和抽象类的属性。
public interface IStartable
{
bool RequiresKey { get; set; }
void Start(object key);
}
public abstract class Vehicle
{
uint WheelCount { get; set; }
}
public class Car : Vehicle, IStartable
{
public bool RequiresKey { get; set; }
public string Make { get; set; }
publilc string Model { get; set; }
public Car() {}
public void Start(object key)
{
// start car with key
}
}
我不知道如何完成这个架构:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="cars"
xmlns="cars"
xmlns:c="cars">
<!-- How do I get car to have vehicle's wheelcount
AND IStartable's RequiresKey? -->
<xs:element name="Car" type="c:Car" />
<xs:complexType name="Car">
<xs:complexContent>
<xs:extension base="c:Vehicle">
<xs:group ref=c:CarGroup" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:group name="CarGroup">
<xs:sequence>
<xs:element name="Make" type="xs:token" />
<xs:element name="Model" type="xs:token" />
</xs:sequence>
</xs:group>
<xs:complexType name="Vehicle">
<xs:sequence>
<xs:element name="WheelCount" type="xs:unsignedInt" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="IStartable">
<xs:sequence>
<xs:element name="RequiresKey" type="xs:boolean" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Please consider the following three .NET types: I have an interface, an abstract class, and a concrete class. My question is how to write the XML Schema to include the properties from the interface and from the abstract class.
public interface IStartable
{
bool RequiresKey { get; set; }
void Start(object key);
}
public abstract class Vehicle
{
uint WheelCount { get; set; }
}
public class Car : Vehicle, IStartable
{
public bool RequiresKey { get; set; }
public string Make { get; set; }
publilc string Model { get; set; }
public Car() {}
public void Start(object key)
{
// start car with key
}
}
I don't know how to complete this schema:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="cars"
xmlns="cars"
xmlns:c="cars">
<!-- How do I get car to have vehicle's wheelcount
AND IStartable's RequiresKey? -->
<xs:element name="Car" type="c:Car" />
<xs:complexType name="Car">
<xs:complexContent>
<xs:extension base="c:Vehicle">
<xs:group ref=c:CarGroup" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:group name="CarGroup">
<xs:sequence>
<xs:element name="Make" type="xs:token" />
<xs:element name="Model" type="xs:token" />
</xs:sequence>
</xs:group>
<xs:complexType name="Vehicle">
<xs:sequence>
<xs:element name="WheelCount" type="xs:unsignedInt" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="IStartable">
<xs:sequence>
<xs:element name="RequiresKey" type="xs:boolean" />
</xs:sequence>
</xs:complexType>
</xs:schema>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我真的不明白如何在 XML 模式中实际做到这一点。 XML 模式确实具有继承 - 但只有单一继承,并且它不知道接口等。
在当前的 XML 模式中,没有办法让一个类(复杂类型)既基于基类,又同时基于“从第二个“接口”类继承” - XML 模式只是没有这些概念,抱歉。
您也许可以稍微重新设计您的继承 -
IStartable >车辆> Car
- 然后您可以在 XML 模式中对其进行建模。当前的设置可能无法使用当前的 XML 模式标准来实现。I don't really see how you could actually do this in XML schema. XML schema does have inheritance - but only single inheritance, and it doesn't know about interfaces etc.
There's no way in current XML schema to have a class (complex type) be based both on a base class, and at the same time "inherit" from a second "interface" class - XML schema just doesn't have those concepts, sorry.
You might be able to redesign your inheritance a bit -
IStartable > Vehicle > Car
- then you could model it in XML schema. The current setup is probably just not doable using current XML schema standards.