Xsd2Code 帮助 - 生成的代码似乎与架构不匹配
我可能做的事情完全错误,但我创建了一个简单的测试模式:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="MyRoot">
<xs:annotation>
<xs:documentation>Comment describing your root element</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:choice>
<xs:element name="MyChildOne" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:choice>
<xs:element name="SubChild" minOccurs="0" maxOccurs="unbounded"/>
</xs:choice>
<xs:attribute name="SomeAttribute" type="xs:string"/>
<xs:attribute name="SomethingElse" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="MyChildTwo" type="xs:string" maxOccurs="unbounded"/>
</xs:choice>
</xs:complexType>
</xs:element>
一个根,两个孩子(一个可选)。
我从 VS2010 运行 Xsd2Code,生成的代码创建了两个“根”类(MyRoot 和 MyChildOne),但没有创建预期的 MyChildTwo。我本来期望一个带有 MyRoot.MyChildOne 的模型...
这是生成的代码:
using System;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Collections;
using System.Xml.Schema;
using System.ComponentModel;
using System.Collections.Generic;
public partial class MyRoot
{
private List<object> itemsField;
public MyRoot()
{
this.itemsField = new List<object>();
}
public List<object> Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
}
public partial class MyRootMyChildOne
{
private List<object> itemsField;
private string someAttributeField;
private string somethingElseField;
public MyRootMyChildOne()
{
this.itemsField = new List<object>();
}
public List<object> Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
public string SomeAttribute
{
get
{
return this.someAttributeField;
}
set
{
this.someAttributeField = value;
}
}
public string SomethingElse
{
get
{
return this.somethingElseField;
}
set
{
this.somethingElseField = value;
}
}
}
我不明白如何将其序列化为有效的(符合架构的)XML 文件...
感谢您对我进行了这
方面的教育
I might be doing something totally wrong, but i created a simple test schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="MyRoot">
<xs:annotation>
<xs:documentation>Comment describing your root element</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:choice>
<xs:element name="MyChildOne" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:choice>
<xs:element name="SubChild" minOccurs="0" maxOccurs="unbounded"/>
</xs:choice>
<xs:attribute name="SomeAttribute" type="xs:string"/>
<xs:attribute name="SomethingElse" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="MyChildTwo" type="xs:string" maxOccurs="unbounded"/>
</xs:choice>
</xs:complexType>
</xs:element>
One root, two children (one optional).
I ran the Xsd2Code from VS2010 and the generated code created two "root" classes (MyRoot and MyChildOne) without creating the expected MyChildTwo. I would have expected a model with MyRoot.MyChildOne...
Here's the generated code:
using System;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Collections;
using System.Xml.Schema;
using System.ComponentModel;
using System.Collections.Generic;
public partial class MyRoot
{
private List<object> itemsField;
public MyRoot()
{
this.itemsField = new List<object>();
}
public List<object> Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
}
public partial class MyRootMyChildOne
{
private List<object> itemsField;
private string someAttributeField;
private string somethingElseField;
public MyRootMyChildOne()
{
this.itemsField = new List<object>();
}
public List<object> Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
public string SomeAttribute
{
get
{
return this.someAttributeField;
}
set
{
this.someAttributeField = value;
}
}
public string SomethingElse
{
get
{
return this.somethingElseField;
}
set
{
this.somethingElseField = value;
}
}
}
I don't understand how can I serialize this into a valid (schema compliant) XML file...
Thanks for educating me on this
Cos
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 xsd.exe 生成此类,它会为您提供相同的结果:
除了使用已知类型声明之外:
因此,如果您考虑一下,这是有道理的。因为您的子类型 2 是字符串,而字符串是 XSD 中的简单类型,所以您可以将 System.String 的实例添加到 Items 数组中,然后使用上面的代码将其序列化。每个字符串都将包装在
节点中。更新
要完成此工作,您需要创建类型,然后使用 XmlSerializer:
这为我们提供了以下 XML:
If you use xsd.exe to generate this class, it gives you the same thing:
Except for the use of the known type declarations:
So it makes sense if you think about it. Because your child type 2 is a string and string is a simple type in XSD you can add instances of System.String to your Items array and then serialize this out using the above code. Each string will be wrapped in a
<MyChildTwo/>
node.UPDATE
To make this work you create your type and then use XmlSerializer:
This gives us the following XML: