カスタムXML出力を実行する必要があるクラスがあるため、IXmlSerializableインターフェイスを実装します。ただし、xmlタグ名を変更する場合を除いて、デフォルトのシリアル化で出力するフィールドがいくつかあります。serializer.Serializeを呼び出すと、XMLでデフォルトのタグ名を取得します。どういうわけかこれらを変更できますか?
これが私のコードです:
public class myClass: IXmlSerializable
{
//Some fields here that I do the custom serializing on
...
// These fields I want the default serialization on except for tag names
public string[] BatchId { get; set; }
...
... ReadXml and GetSchema methods are here ...
public void WriteXml(XmlWriter writer)
{
XmlSerializer serializer = new XmlSerializer(typeof(string[]));
serializer.Serialize(writer, BatchId);
... same for the other fields ...
// This method does my custom xml stuff
writeCustomXml(writer);
}
// My custom xml method is here and works fine
...
}
これが私のXml出力です:
<MyClass>
<ArrayOfString>
<string>2643-15-17</string>
<string>2642-15-17</string>
...
</ArrayOfString>
... My custom Xml that is correct ..
</MyClass>
最終的には次のようになります。
<MyClass>
<BatchId>
<id>2643-15-17</id>
<id>2642-15-17</id>
...
</BatchId>
... My custom Xml that is correct ..
</MyClass>