以下是我用来从 Excel 表中获取 XML 格式数据的一段代码。但是如果excel表中的值是空白的。XML 没有将其视为价值。假设在excel中我有一个列名EmployeeAddress
,如果它是空白的,它应该关闭标签<EmployeeAddress/>
。
daAdapter = new OleDbDataAdapter("Select * FROM [KDC Report$]", connection);
System.Data.DataTable dt = new System.Data.DataTable("Assets");
//daAdapter.FillSchema(dt, SchemaType.Source);
daAdapter.Fill(dt);
DataSet ds = new DataSet("DocumentElement");
ds.Tables.Add(dt);
var memoryStream = new MemoryStream();
using (TextWriter streamWriter = new StreamWriter(memoryStream))
{
var xmlSerializer = new XmlSerializer(typeof(DataSet));
xmlSerializer.Serialize(streamWriter, ds);
Encoding.UTF8.GetString(memoryStream.ToArray());
}
ds.WriteXml("C:\\Development\\MyAppln\\ExcelToXML\\Products.xml");