0
我有一個類和一個來自數據庫的查詢,填充DataSet中的DataTable。被序列化Datatable來填充(Object)標記。Serilize DataTable(Dataset)to Object
對象:
public class Marks
{
/// <summary>
///
/// </summary>
public List<Mark> MarkList { get; set; }
public Marks()
{
MarkList = new List<Mark>();
}
}
public class Mark
{
public String Number { get; set; }
public String Word { get; set; }
public DateTime? DateIn { get; set; }
public DateTime? DateOut { get; set; }
}
我試着這樣做:
private void BindMarksSerialize()
{
FillDataTable(dtMarks, MarksCMD);
MemoryStream stream = new MemoryStream();
dtMarks.WriteXml(stream);
XmlSerializer serializer = new XmlSerializer(typeof(Marks), new XmlRootAttribute("Marks"));
File.WriteAllBytes("C:\\Out.xml", stream.ToArray());
StreamReader reader = new StreamReader("C:\\Out.xml");
var marks = (Marks)serializer.Deserialize(reader);
reader.Close();
}
private void FillDataTable(DataTable dataTable, Object sqlCommandContainer)
{
var sqlCommand = sqlCommandContainer as SqlCommand;
var sqlDataAdapter = sqlCommandContainer as SqlDataAdapter;
if (sqlDataAdapter == null && sqlCommand == null)
return;
try
{
dataTable.Clear();
if (sqlCommand != null)
{
sqlCommand.Connection.Open();
dataTable.Load(sqlCommand.ExecuteReader());
}
else
sqlDataAdapter.Fill(dataTable);
}
catch (SqlException ex)
{
Logger.Error(new StackTrace(1).GetFrame(0).GetMethod().Name + @": " + Environment.NewLine + ex.Message);
}
finally
{
if (sqlCommand != null)
sqlCommand.Connection.Close();
}
}
需要結果來填充XML:
<Marks xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MarkList>
<Mark>
<Number>1</Number>
<Word>123123123</Word>
<DateIn xsi:nil="true" />
<DateOut xsi:nil="true" />
</Mark>
</MarkList>
</Marks>
我:
<NewDataSet>
<Mark>
<Number>110938</Number>
<Word>110938</Word>
<DateIn>1993-04-08T00:00:00+05:00</DateIn>
<DateOut>2002-02-04T00:00:00+04:00</DateOut>
</Mark>
</NewDataSet>
提示任何人都可以知道如何做到這一點?
我認爲OP在編寫CORRECT xml時遇到了麻煩。注意最後兩段。一個是「這是我想要的」,另一個是「這就是我得到的」 – paqogomez