如何将复杂类型与 xs:any / ##any 一起使用并混合在 XSD 工具生成的代码中
我的 XML 模式中有以下复杂类型:
<xs:complexType name="Widget" mixed="true">
<xs:sequence>
<xs:any namespace="##any" processContents="skip" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
派生 XML 中的元素可以包含字符串或可以包含格式良好的 XML,因此混合属性为 true。
当我通过 .NET XSD 工具运行它时,我得到以下生成代码:
public partial class Widget{
private System.Xml.XmlNode[] anyField;
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
[System.Xml.Serialization.XmlAnyElementAttribute()]
public System.Xml.XmlNode[] Any {
get {
return this.anyField;
}
set {
this.anyField = value;
}
}
}
我的问题是我不完全确定应该如何使用它。最终我需要能够将小部件的值设置为:
<widget>Hello World!</widget>
或
<widget>
<foo>Hello World</foo>
</widget>
两者都根据架构进行验证
I have the following complex type in my XML schema:
<xs:complexType name="Widget" mixed="true">
<xs:sequence>
<xs:any namespace="##any" processContents="skip" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
The element in derived XML could contain string or could contained wellformed XML, hence the mixed attribute being true.
When I run this through the .NET XSD Tool I get the following generate code:
public partial class Widget{
private System.Xml.XmlNode[] anyField;
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
[System.Xml.Serialization.XmlAnyElementAttribute()]
public System.Xml.XmlNode[] Any {
get {
return this.anyField;
}
set {
this.anyField = value;
}
}
}
The question I have is that I am not entirely sure how I should then use this. Ultimately I need to be able to set the value of widget to either:
<widget>Hello World!</widget>
or
<widget>
<foo>Hello World</foo>
</widget>
Both of which validate agaisnt the schema
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用于此:
使用此:
用于此:
使用此:
For this:
Use this:
For this:
Use this:
将此作为答案发布,因为从技术上讲它有效并回答了问题。然而,这似乎是一个非常令人讨厌的黑客行为。因此,如果有人有替代和更好的解决方案,我会洗耳恭听。
正如您所看到的,我将字符串放入标记中的 XmlDocument 中,这可以正常工作、编译和序列化,没有任何问题 - 所以,是的,这是一个解决方案,但让我感到这是一个令人讨厌的黑客攻击。
在此示例中,我将 XML 放入 XmlDocument 中,然后将其分配给生成的类。这更有意义,因为我使用的是 XML 而不是原始字符串。
奇怪的是我也可以这样做并且它也有效(但也是一个令人讨厌的黑客):
Posting this as a answer as technically it works and answers the question. However it seems like a really nasty hack. So if anybody has an alternative and better solution I am all ears.
As you can see I am placing my string into an XmlDocument wrapped in tags, this works, compiles and serializes without issues - so yes it is a solution but it strikes me that this is a nasty hack.
In this example I am placing my XML into an XmlDocument and then assigning that to the generated class. This makes more sense as I am working with XML not raw string.
Curiously I can also do this and it works as well (but is also a nasty hack):