我可能做错了,但我创建了一个简单的测试模式:
<?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文件...
感谢您对此进行教育
的Cos
答案 0 :(得分:1)
如果您使用xsd.exe生成此类,它会为您提供相同的内容:
public partial class MyRoot {
private object[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("MyChildOne", typeof(MyRootMyChildOne))]
[System.Xml.Serialization.XmlElementAttribute("MyChildTwo", typeof(string))]
public object[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
除了使用已知类型声明外:
[System.Xml.Serialization.XmlElementAttribute("MyChildTwo", typeof(string))]
因此,如果你考虑它就有意义了。因为您的子类型2是字符串而字符串是XSD中的简单类型,所以您可以将System.String的实例添加到Items数组中,然后使用上面的代码将其序列化。每个字符串都将包装在<MyChildTwo/>
节点中。
<强>更新强>
要使这项工作你创建你的类型,然后使用XmlSerializer:
var root = new MyRoot();
root.Items = new object[2];
root.Items[0] = new MyRootMyChildOne { Items = new object[1], SomeAttribute = "test", SomethingElse = "test" };
root.Items[1] = "hello";
var ser = new XmlSerializer(typeof(MyRoot));
var memoryStream = new MemoryStream();
var xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
var streamReader = new StreamReader(memoryStream, Encoding.UTF8);
ser.Serialize(xmlTextWriter, root);
memoryStream.Position = 0;
string xml = streamReader.ReadToEnd();
这为我们提供了以下XML:
<MyRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MyChildOne SomeAttribute="test" SomethingElse="test" />
<MyChildTwo>hello</MyChildTwo>
</MyRoot>