私は以下のXML構造を持っています。XmlSerializerで使用するオプションのXML要素のクラスメンバーを装飾/定義する方法は?
<theElement attrOne="valueOne" attrTwo="valueTwo">
<theOptionalList>
<theListItem attrA="valueA" />
<theListItem attrA="anotherValue" />
<theListItem attrA="stillAnother" />
</theOptionalList>
</theElement>
<theElement attrOne="anotherOne" attrTwo="anotherTwo" />
対応するクラスの構造を表現するクリーンな方法は何ですか:theElement
要素はtheOptionalList
要素、あるいはませんが含まれていることができますか?
私は次のかなり確信している:theElement
ため
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace MyNamespace
{
public class TheListItem
{
[XmlAttributeAttribute("attrA")]
public string AttrA { get; set; }
public override string ToString()
{
StringBuilder outText = new StringBuilder();
outText.Append(" attrA = " + AttrA + "\r\n");
return outText.ToString();
}
}
}
しかし、何について:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace MyNamespace
{
public class TheOptionalList
{
[XmlAttributeAttribute("attrOne")]
public string AttrOne { get; set; }
[XmlAttributeAttribute("attrTwo")]
public string AttrTwo { get; set; }
[XmlArrayItem("theListItem", typeof(TheListItem))]
public TheListItem[] theListItems{ get; set; }
public override string ToString()
{
StringBuilder outText = new StringBuilder();
outText.Append("attrOne = " + AttrOne + " attrTwo = " + AttrTwo + "\r\n");
foreach (TheListItem li in theListItems)
{
outText.Append(li.ToString());
}
return outText.ToString();
}
}
}
と同様に? theOptionalList
要素を配列型として取り込み、ファイル内で何が見つかったのか(何もないものも)を読み取って、そのコードが存在するかどうかをコードでチェックしますか?それとも私が提供できる別のデコレータがありますか?それともちょうどうまくいくのですか?
編集:私はthis answerからの情報を使用して終了しました。
応答ジェームズありがとう。私はこれが要素を持たない 'theOptionalList'要素を扱うだろうと思っていますが、' theOptionalList'は全く存在しません。 (私はあなたが示唆していることを理解していないかもしれません。)また、私のフォーマットのためのスキーマはありません。私と他の開発者との間では口頭で合意しています。:) – John
は、私は肯定的ではないんだけど、私は、配列がnullの場合、 'IsNullable'はそれを排除する使用と思います。 –
スキーマを検討してください。それは大いに役立ちますが、小さくても、あなたの「ツールキット」にとっては素晴らしい作品です。 – Gusdor