/// <summary> /// Create a SmartCard from the information contained in an XmlDocument. /// </summary> /// <param name="xmlDoc">The document to process.</param> /// <returns>A new SmartCard built from the XmlDocument.</returns> /// <exception cref="InvalidSmartCardException"> /// If the deserialization of the smartcard failed for some reason. /// </exception> protected SmartCard DeserializeXML(XmlDocument xmlDoc) { SmartCard smartCard; XmlNode rootNode; XmlNodeList nodeList; XmlAttribute attrNode; ISerializer serialize; object content; // Make the new smart card. smartCard = new Types.SmartCard(); // Get the root node. rootNode = xmlDoc.DocumentElement; if (rootNode == null) { return(smartCard); } // Get the smart card node. nodeList = xmlDoc.GetElementsByTagName("sc"); if (nodeList.Count > 0) { // Get the part number attribute. attrNode = ( XmlAttribute )nodeList[0].Attributes.GetNamedItem("pn"); if (attrNode != null) { smartCard.PartNumber = attrNode.Value; } // Get the program date attribute. attrNode = ( XmlAttribute )nodeList[0].Attributes.GetNamedItem("pd"); if (attrNode != null) { smartCard.ProgramDate = DateTime.Parse(attrNode.Value); } } try { // Get all of the cylinder content nodes. nodeList = xmlDoc.GetElementsByTagName("c"); foreach (XmlNode child in nodeList) { // Deserialize all of the cylinder children. serialize = new CylinderXMLSerializer(); content = serialize.Deserialize(child.OuterXml); smartCard.Add(content, serialize); } } catch (Exception e) { throw new InvalidSmartCardException(e); } return(smartCard); }
/// <summary> /// The unit tests. /// </summary> public static void Test() { try { CylinderXMLSerializer serial; Cylinder cylinder, newCylinder; string cylinderXML, properXML; serial = new CylinderXMLSerializer(); cylinder = new Cylinder("1002-3440", "AIRLE"); cylinder.FactoryId = "18-6789-23"; cylinder.ExpirationDate = DateTime.Parse("10/23/1969"); cylinder.RefillDate = DateTime.Parse("09/16/1969"); cylinderXML = serial.Serialize(cylinder); //Console.WriteLine( "@ cylinderXML: '" + cylinderXML + "'." ); //string theXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?><cylinder partNumber=\"1002-3440\" expirationDate=\"1969-10-23 0:0:0\" factoryID=\"18-6789-23\" />"; properXML = "<cylinder partNumber=\"1002-3440\" expirationDate=\"1969-10-23 0:0:0\" factoryID=\"18-6789-23\" />"; Debug.Assert(cylinderXML.CompareTo(properXML) == 0, "Cylinder XML Persistence failed."); newCylinder = ( Cylinder )serial.Deserialize(cylinderXML); Debug.Assert(cylinder.FactoryId.CompareTo(newCylinder.FactoryId) == 0, "Cylinder XML building did not extract FactoryID properly."); Debug.Assert(cylinder.PartNumber.CompareTo(newCylinder.PartNumber) == 0, "Cylinder XML building did not extract PartNumber properly."); Debug.Assert(cylinder.ExpirationDate.CompareTo(newCylinder.ExpirationDate) == 0, "Cylinder XML building did not extract ExpirationDate properly."); Debug.Assert(cylinder.RefillDate.CompareTo(newCylinder.RefillDate) == 0, "Cylinder XML building did not extract RefillDate properly."); } catch (Exception e) { Console.WriteLine("Exception in CylinderPersistenceXMLTester.Test(): {0}", e.ToString()); } }