1

私は何か完全に間違っているかもしれませんが、簡単なテスト スキーマを作成しました。

<?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>

1 つのルート、2 つの子 (1 つはオプション)。

VS2010 から Xsd2Code を実行すると、生成されたコードによって、予想される MyChildTwo を作成せずに 2 つの「ルート」クラス (MyRoot と MyChildOne) が作成されました。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 ファイルにシリアル化する方法がわかりません...

これについて教えてくれてありがとう

コス

4

1 に答える 1

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>
于 2011-10-20T15:37:29.497 に答える