public static void SaveToXml(TableInfo table, IEnumerable records, XmlWriter xw) { List <string> ids = new List <string>(); foreach (var col in table.Columns) { ids.Add(XmlTool.NormalizeIdentifier(col.Name)); } foreach (ICdlRecord rec in records) { xw.WriteStartElement("Row"); for (int i = 0; i < rec.FieldCount; i++) { rec.ReadValue(i); string xdata = "", xtype = ""; if (!GetValueAsXml(rec, ref xtype, ref xdata)) { continue; } xw.WriteStartElement(ids[i]); xw.WriteAttributeString("t", xtype); xw.WriteString(xdata); xw.WriteEndElement(); } xw.WriteEndElement(); } }
public static IEnumerable <ICdlRecord> LoadFromXml(TableInfo table, XmlReader reader) { reader.MoveToContent(); reader.Read(); reader.MoveToContent(); Dictionary <string, int> colPos = new Dictionary <string, int>(); int index = 0; foreach (var col in table.Columns) { colPos[XmlTool.NormalizeIdentifier(col.Name)] = index; index++; } while (reader.NodeType == XmlNodeType.Element) { if (reader.LocalName != "Row") { throw new XmlFormatError(String.Format("DBSH-00070 Bad xml, expected tag {0}, found {1}", "Row", reader.LocalName)); } reader.Read(); reader.MoveToContent(); object[] values = new object[table.Columns.Count]; for (int i = 0; i < values.Length; i++) { values[i] = DBNull.Value; } while (reader.NodeType == XmlNodeType.Element) { string colname = reader.LocalName; int pos = colPos[colname]; string xtype = reader.GetAttribute("t"); reader.Read(); string xdata = ""; if (reader.NodeType == XmlNodeType.Text) { xdata = reader.Value; reader.Read(); } values[pos] = GetValueFromXml(xtype, xdata); if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == colname) { // skip end of element reader.Read(); } } yield return(new ArrayDataRecord(table, values)); if (reader.NodeType == XmlNodeType.EndElement) { reader.Read(); } } }