开发者

Cannot WriteXML for DataTable because Windows Search Returns String Array for Authors Property

The System.Author Windows property is a multiple value string. Windows Search returns this value as an array of strings in a DataColumn. (The column开发者_Go百科's data-type is string[] or String().) When I call the WriteXML method on the resulting data-table, I get the following InvalidOperationException exception.

Is there a way to specify the data-table's xml-serializer to use for specific columns or specific data-types?

Basically, how can I make WriteXML work with this data-table?

System.InvalidOperationException: Type System.String[] does not implement IXmlSerializable interface therefore can not proceed with serialization.


You could easily copy your DataTable changing the offending Authors column to a String and joing the string[] data with a proper delimiter like "|" or "; ".

DataTable xmlFriendlyTable = oldTable.Clone();
xmlFriendlyTable.Columns["Author"].DataType = typeof(String);
xmlFriendlyTable.Columns["Author"].ColumnMapping = MappingType.Element;
foreach(var row in oldTable.Rows) {
    object[] rowData = row.ItemArray;
    object[] cpyRowData = new object[rowData.Length];
    for(int i = 0; i<rowData.Length; i++) {
        if(rowData[i] != null && rowData[i].GetType() == typeof(String[])) {
            cpyRowData[i] = String.Join("; ", (rowData[i] as String[]));
        } else {
            cpyRowData[i] = rowData[i];
        }
        xmlFriendlyTable.Rows.Add(cpyRowData);
    }
}
xmlFriendlyTable.WriteXml( ... );

NOTE Wrote the above in the web browser, so there may be syntax errors.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜