/// <summary> /// Writes the contents of this spreadsheet to dest using an XML format. /// The XML elements should be structured as follows: /// <spreadsheet IsValid="IsValid regex goes here"><cell name="cell name goes here" contents="cell contents go here"></cell><cell name="cell name goes here" contents="cell contents go here"></cell><cell name="cell name goes here" contents="cell contents go here"></cell></spreadsheet> /// The value of the IsValid attribute should be IsValid.ToString() /// There should be one cell element for each non-empty cell in the spreadsheet. /// If the cell contains a string, the string (without surrounding double quotes) should be written as the contents. /// If the cell contains a double d, d.ToString() should be written as the contents. /// If the cell contains a Formula f, f.ToString() with "=" prepended should be written as the contents. /// If there are any problems writing to dest, the method should throw an IOException. /// </summary> /// <param name="dest"></param> /// <exception cref="System.NotImplementedException"></exception> public override void Save(TextWriter dest) { using (var sw = new StringWriter()) { using (var xw = XmlWriter.Create(sw)) { xw.WriteStartDocument(); xw.WriteStartElement("spreadsheet"); xw.WriteAttributeString("IsValid", IsValid.ToString()); foreach (Cell c in cds.getCells()) { xw.WriteStartElement("cell"); xw.WriteAttributeString("name", c.name); //gotta <3 lambdas! xw.WriteAttributeString("contents", (c.contents is string?(string)c.contents: (c.contents is double?c.contents.ToString() : (c.contents is Formula ? "=" + c.contents.ToString() : pretendToReturnStringButActuallyThrowAnException())))); xw.WriteEndElement(); } xw.WriteEndElement(); xw.WriteEndDocument(); } dest.Write(sw.ToString()); } Changed = false; }
public static IEnumerable <string> SummonNames(CellDS cds) { //this overly complicated statement returns all nonempty cell names return((cds.getCells()).Select(c => c.contents is string?((string)c.contents == "" ? "" : c.name) : c.name).Except(new HashSet <string>() { "" })); }