} /// <summary> /// Deserialize XML string into XSD generated SECURE code /// </summary> /// <typeparam name="T">Type of object to be deserialized into</typeparam> /// <param name="xml">string, containing the XML data</param> /// <returns>object of Type T with data from the XML string</returns> public static T DeserializeXML <T>(string xml) { return(TestController.DeserializeXML <T>(xml, true)); }
/// <summary> /// Format XML into readable /// </summary> /// <param name="inputXML">string, containing unformatted XML</param> /// <returns>string, containing formatted XML</returns> public static string FormatXML(string inputXML) { try { string outputXML = string.Empty; string readPart = string.Empty; int pos = 0; int numIndents = 0; string lastNode = string.Empty; Hashtable nodes = new Hashtable(new Dictionary <string, int>()); while (pos < inputXML.Length) { readPart += inputXML.Substring(pos++, 1); if (readPart[0] == '<' && readPart.Contains("/>")) // self contained node and section { if (lastNode.Contains("</")) { for (int i = 0; i < int.Parse(nodes[TestController.GetXMLStart(lastNode)].ToString()); i++) { outputXML += "\t"; } } else { for (int i = 0; i < numIndents; i++) { outputXML += "\t"; } } outputXML += (readPart + "\r\n"); readPart = string.Empty; } else if (readPart.Contains("<?") && readPart.Contains("?>")) // XML comment { if (lastNode.Contains("</")) { for (int i = 0; i < int.Parse(nodes[TestController.GetXMLStart(lastNode)].ToString()); i++) { outputXML += "\t"; } } else { for (int i = 0; i < numIndents; i++) { outputXML += "\t"; } } outputXML += (readPart + "\r\n"); readPart = string.Empty; } else if (readPart[0] == '<' && readPart.Contains(">") && !readPart.Contains("</")) // node start { if (nodes.ContainsKey(readPart)) { for (int i = 0; i < int.Parse(nodes[readPart].ToString()); i++) { outputXML += "\t"; } lastNode = readPart; } else { if (lastNode.Contains("</")) { for (int i = 0; i < int.Parse(nodes[TestController.GetXMLStart(lastNode)].ToString()); i++) { outputXML += "\t"; } } else { for (int i = 0; i < numIndents; i++) { outputXML += "\t"; } } } outputXML += (readPart + "\r\n"); if (!nodes.ContainsKey(readPart)) { if (readPart.Contains(" ")) { string newNode = readPart.Substring(0, readPart.IndexOf(" ")) + ">"; if (!nodes.ContainsKey(newNode)) { nodes.Add(newNode, numIndents); if (!lastNode.Contains("</")) { numIndents++; } lastNode = newNode; } } else { nodes.Add(readPart, numIndents); if (!lastNode.Contains("</")) { numIndents++; } lastNode = readPart; } } readPart = string.Empty; } else if (readPart.Contains("</") && readPart.Contains(">")) // value and node close { if (readPart[0] == '<') // node close only { for (int i = 0; i < int.Parse(nodes[TestController.GetXMLStart(readPart)].ToString()); i++) { outputXML += "\t"; } outputXML += (readPart + "\r\n"); lastNode = readPart; readPart = string.Empty; } else { for (int i = 0; i < ((int)nodes[lastNode]) + 1; i++) { outputXML += "\t"; } outputXML += (readPart.Substring(0, readPart.IndexOf("</")) + "\r\n"); numIndents = (int)nodes[lastNode]; for (int i = 0; i < int.Parse(nodes[TestController.GetXMLStart(readPart.Substring(readPart.IndexOf("</")))].ToString()); i++) { outputXML += "\t"; } outputXML += (readPart.Substring(readPart.IndexOf("</")) + "\r\n"); lastNode = readPart.Substring(readPart.IndexOf("</")); readPart = string.Empty; } } if (readPart != string.Empty && pos == inputXML.Length) { outputXML += readPart; } } return(outputXML); } catch { return(inputXML); } }
/// <summary> /// Serialize the XSD generated SECURE code to XML in string format. /// </summary> /// <param name="o">The System.Object to serialize.</param> /// <returns>a UTF-8 encoded XML string</returns> public static string SerializeXML(object o) { return(TestController.SerializeXML(o, true)); }