3

我使用 xsd.exe 和这个 xml 文件生成了 ac# 类:

<?xml version="1.0" encoding="UTF-8"?>
<Mary>
    <Frank>
        <Joe>
            <Susan>
                <Stuff>data</Stuff>
            </Susan>
            <Susan>
                <Stuff>data</Stuff>
            </Susan>
        </Joe>
        <Joe>
            <Susan>
                <Stuff>data</Stuff>
            </Susan>
            <Susan>
                <Stuff>data</Stuff>
            </Susan>
        </Joe>
    </Frank>
</Mary>

生成的 C# 类可以在这里查看。

我可以用数据初始化对象:

var susan = new MaryFrankJoeSusan(){Stuff = "my data"};
var frank = new MaryFrank(){Joe = new MaryFrankJoeSusan[1][]};
frank.Joe[0] = new MaryFrankJoeSusan[1]{susan};
var mary = new Mary { Items = new MaryFrank[1] { frank } };

我正在使用以下内容将其序列化到磁盘:

var serializer = new XmlSerializer(typeof(Mary));

using (Stream stream = new FileStream(@"C:\out.xml", FileMode.Create))
{
    var settings = new XmlWriterSettings { Indent = true, NewLineOnAttributes = true, OmitXmlDeclaration = true};
    using (XmlWriter writer = new XmlTextWriter(stream, Encoding.Unicode))
    {
        serializer.Serialize(writer, mary);
        writer.Close();
    }
}

但是,初始化序列化程序时出现以下错误:

error CS0030: Cannot convert type 'MaryFrankJoeSusan[]' to 'MaryFrankJoeSusan'

如何将整个 Mary 对象序列化到磁盘?

4

1 回答 1

2

这些生成的类有些不对劲。

问题正在发生,因为MaryFrank.Joe它被声明为对象的二维数组MaryFrankJoeSusan,但它用 a 装饰,XmlArrayItemAttribute它告诉序列化程序该二维数组的每个项目都是 typeMaryFrankJoeSusan当它们当然是MaryFrankJoeSusan[].

如果在生成的类中更改此行:

[System.Xml.Serialization.XmlArrayItemAttribute("Susan", typeof(MaryFrankJoeSusan),
 Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]

对此:

[System.Xml.Serialization.XmlArrayItemAttribute("Susan", typeof(MaryFrankJoeSusan[]),
 Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]

然后它将无错误地序列化。但是,您不会得到想要的结果。而不是这个:

<Mary>
    <Frank>
        <Joe>
            <Susan>
                <Stuff>my data</Stuff>
            </Susan>
        </Joe>
    </Frank>
</Mary>

你会得到这个(注意额外的MaryFrankJoeSusan标签):

<Mary>
    <Frank>
        <Joe>
            <Susan>
                <MaryFrankJoeSusan>
                    <Stuff>my data</Stuff>
                </MaryFrankJoeSusan>
            </Susan>
        </Joe>
    </Frank>
</Mary>

真正的问题似乎是该xsd.exe工具一开始就错误地生成了类结构。它不是在层次结构中创建一个类来代表 Joe,而是试图将 Joe 和 Susan 组合在一起,这在此处实际上不起作用。

我通过工具从问题中运行了您的原始 XML 以生成 XSD 模式,我得到了这个:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Mary" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="Mary" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Frank">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Joe" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="Susan" minOccurs="0" maxOccurs="unbounded">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="Stuff" type="xs:string" minOccurs="0" />
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

这对我来说看起来不错。然后我采用相同的模式并再次通过该工具运行它以生成 C# 类。我本来希望得到类似的东西:

[Serializable]
[XmlRoot(Namespace = "", ElementName = "Mary")]
public class Mary
{
    [XmlElement("Frank")]
    public Frank[] Frank { get; set; }
}
[Serializable]
public class Frank
{
    [XmlElement("Joe")]
    public Joe[] Joe { get; set; }
}
[Serializable]
public class Joe
{
    [XmlElement("Susan")]
    public Susan[] Susan { get; set; }
}
[Serializable]
public class Susan
{
    [XmlElement("Stuff")]
    public string Stuff { get; set; }
}

但是相反,我得到了与您在问题中链接的相同的损坏类。所以对我来说,它看起来像是 xsd 工具中的一个错误。

要让它工作,您可以使用我上面制作的手动编码类,将初始化代码更改为:

var susan = new Susan { Stuff = "my data" };
var joe = new Joe { Susan = new Susan[] { susan } };
var frank = new Frank { Joe = new Joe[] { joe } };
var mary = new Mary { Frank = new Frank[] { frank } };

- 或者 -

另一种选择是改变xsd。将和元素的xs:sequence指示符替换为,如下所示:FrankJoexs:choice

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Mary" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="Mary" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Frank">
          <xs:complexType>
            <xs:choice> <!-- was xs:sequence -->
              <xs:element name="Joe" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:choice> <!-- was xs:sequence -->
                    <xs:element name="Susan" minOccurs="0" maxOccurs="unbounded">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="Stuff" type="xs:string" minOccurs="0" />
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:choice> <!-- was /xs:sequence -->
                </xs:complexType>
              </xs:element>
            </xs:choice> <!-- was /xs:sequence -->
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

使用此模式,生成的类会更好:现在有一个类来表示 Joe。(为了简洁起见,我在这里简化了生成的代码并删除了一些属性):

[System.SerializableAttribute()]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class Mary {
    [System.Xml.Serialization.XmlElementAttribute("Frank", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public MaryFrank[] Items { get; set; }
}

[System.SerializableAttribute()]
public partial class MaryFrank {
    [System.Xml.Serialization.XmlElementAttribute("Joe", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public MaryFrankJoe[] Items { get; set; }
}

[System.SerializableAttribute()]
public partial class MaryFrankJoe {
    [System.Xml.Serialization.XmlElementAttribute("Susan", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public MaryFrankJoeSusan[] Items { get; set; }
}

[System.SerializableAttribute()]
public partial class MaryFrankJoeSusan {
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Stuff { get; set; }
}

然后设置代码变为:

var susan = new MaryFrankJoeSusan() { Stuff = "my data" };
var joe = new MaryFrankJoe() { Items = new MaryFrankJoeSusan[] { susan } };
var frank = new MaryFrank() { Items = new MaryFrankJoe[] { joe } };
var mary = new Mary { Items = new MaryFrank[] { frank } };

我们得到了预期的输出:

<?xml version="1.0" encoding="utf-16"?>
<Mary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Frank>
        <Joe>
            <Susan>
                <Stuff>my data</Stuff>
            </Susan>
        </Joe>
    </Frank>
</Mary>
于 2013-10-24T06:39:31.700 回答