1

XmlSerializer を使用して、単純なテキストまたはサブ要素を含む要素を文字列に逆シリアル化する方法はありますか?

XML サンプル:

<Attribute>
    <AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">thisistext</AttributeValue>
    <AttributeValue>
        <e:Authorities xmlns:e="urn:dummy">
            <e:Authority>ORG_CHIEF</esia-encoder:Authority>
        </e:Authorities>
    </AttributeValue>
</Attribute>

C# プロパティ:

[XmlElement("AttributeValue", IsNullable = true)]
public string[] AttributeValue { get; set; }

最初の AttributeValue の逆シリアル化は成功しますが、次の逆シリアル化は失敗します。ReadElementString メソッドは単純なコンテンツまたは空のコンテンツを想定しているため、不思議ではありません。シリアライザーに「この要素の内容を文字列に入れる」ように指示する方法を探しています。

4

2 に答える 2

1

実際、XML の 2 番目の値は次のとおりです。

 <e:Authorities xmlns:e="urn:dummy">
     <e:Authority>ORG_CHIEF</esia-encoder:Authority>
 </e:Authorities>

次の理由により、これは有効なstringdataType ではありません。

public string[] AttributeValue {get; set;}
于 2013-01-16T09:04:17.780 に答える
0

XSD で定義できる場合は、XSD2Codeまたは xsd.exe を使用して、XSD が逆シリアル化するクラスを作成できます。

これはどうですか?

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="Attribute">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="AttributeValue" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:choice>
                            <xs:element name="AuthoritiesString" type="xs:string"/>
                            <xs:element name="AuthoritiesElement">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="Authority"/>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                        </xs:choice>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
于 2013-01-16T09:08:00.480 に答える