データベースでxmlファイルをバイナリにシリアル化しようとしました。コードを正しい方法で書いたかどうかはわかりません。私はぐるぐる回っていますが、答えが見つかりません。私は何かを逃したかもしれません。小さな問題に違いないと思います。
私はここで結果を期待しています:
<TestFiles>
<XmlFile>test001.xml </XmlFile>
<XmlFile>test002.xml </XmlFile>
<XmlFile>test003.xml </XmlFile>
</TestFiles>
データベースでエラー結果が得られました:
<?xml version="1.0"?>
<ArrayO
私のコードで何が間違っている可能性がありますか?
C#:
[Serializable]
[XmlRoot("XmlFile")]
public class XmlFile
{
public string FileName
{
get;
set;
}
}
[Serializable]
public class TestFiles: List<XmlFile>
{
}
public void SerializeXmlFileToDatabase()
{
var files = new TestFiles
{
new XmlFile {FileName = "test001.xml"},
new XmlFile {FileName = "test002.xml"},
new XmlFile {FileName = "test003.xml"},
};
var ser = new XmlSerializer(typeof (TestFiles));
var ms = new MemoryStream(8096);
ser.Serialize(ms, XmlFile);
DbConnection connection = null;
try
{
connection = DbFactory.CreateConnection();
connection.ConnectionString = ConnectionString;
connection.Open();
using (DbCommand command = connection.CreateCommand())
{
//insert
var id = Guid.NewGuid();
command.CommandText = "insert into testtable ([id], [data]) values (@id, @data)";
command.CommandType = CommandType.Text;
command.AddParameter("id", DbType.Guid, id);
byte[] data = ms.GetBuffer();
command.AddParameter("data", DbType.Binary, data, ParameterDirection.Input, data.Length);
command.Prepare();
command.ExecuteNonQuery();
}
}
finally
{
if (connection != null)
connection.Close();
}
}