public static Value FromXml(XmlNode p_Node) { Value v = null; foreach (XmlNode x in p_Node.ChildNodes) { if (x.NodeType != XmlNodeType.Element && x.NodeType != XmlNodeType.Text) { continue; } if (x.NodeType == XmlNodeType.Text) { // by default, it is a string v = new Value(Type.XMLRPC_TYPE_STRING, x.Value); } else { // it's an element if (x.Name == Value.GetTypeName(Type.XMLRPC_TYPE_BOOLEAN)) { if (x.InnerText == "0") { v = new Value(Type.XMLRPC_TYPE_BOOLEAN, false); } else { v = new Value(Type.XMLRPC_TYPE_BOOLEAN, true); } } else if (x.Name == Value.GetTypeName(Type.XMLRPC_TYPE_DOUBLE)) { Double d; if (Double.TryParse(x.InnerText, out d)) { v = new Value(Type.XMLRPC_TYPE_DOUBLE, d); } else { throw new Exception("Invalid double value: " + x.InnerText); } } else if (x.Name == Value.GetTypeName(Type.XMLRPC_TYPE_INTEGER) || x.Name == "i4") // i4 is alternate typing for int { Int32 i; if (Int32.TryParse(x.InnerText, out i)) { v = new Value(Type.XMLRPC_TYPE_INTEGER, i); } else { throw new Exception("Invalid integer value: " + x.InnerText); } } else if ( x.Name == Value.GetTypeName(Type.XMLRPC_TYPE_STRING) || x.Name == Value.GetTypeName(Type.XMLRPC_TYPE_BASE64) || x.Name == Value.GetTypeName(Type.XMLRPC_TYPE_DATETIME) ) { v = new Value(Value.GetType(x.Name), x.InnerText); } else if (x.Name == Value.GetTypeName(Type.XMLRPC_TYPE_NIL)) { v = new Value(Type.XMLRPC_TYPE_NIL); } else if (x.Name == Value.GetTypeName(Type.XMLRPC_TYPE_ARRAY)) { v = new Value(Type.XMLRPC_TYPE_ARRAY); foreach (XmlNode y in x.ChildNodes) { if (y.NodeType != XmlNodeType.Element) { continue; } if (y.Name != Value.DATA_TAG) { throw new Exception("Got " + y.Name + " tag. Expected " + Value.DATA_TAG); } foreach (XmlNode z in y.ChildNodes) { if (z.NodeType != XmlNodeType.Element) { continue; } if (z.Name != Value.VALUE_TAG) { throw new Exception("Got " + z.Name + " tag. Expected " + Value.VALUE_TAG); } v.AddToArray(Value.FromXml(z)); } } } else if (x.Name == Value.GetTypeName(Type.XMLRPC_TYPE_STRUCT)) { v = new Value(Type.XMLRPC_TYPE_STRUCT); foreach (XmlNode y in x.ChildNodes) { if (y.NodeType != XmlNodeType.Element) { continue; } if (y.Name != Value.MEMBER_TAG) { throw new Exception("Got " + y.Name + " tag. Expected " + Value.DATA_TAG); } String name = null; Value innerValue = null; foreach (XmlNode z in y.ChildNodes) { if (z.NodeType != XmlNodeType.Element) { continue; } if (z.Name == Value.NAME_TAG) { name = z.InnerText; } else if (z.Name == Value.VALUE_TAG) { innerValue = Value.FromXml(z); } else { throw new Exception("Got " + z.Name + " tag. Expected " + Value.VALUE_TAG); } } if (name != null && innerValue != null) { v.AddToStruct(name, innerValue); } else { throw new Exception("name or value tags are missing"); } } } else { throw new Exception("Invalid type name: " + p_Node.Name); } } } return(v); }
public String ToXml() { StringBuilder sbuf = new StringBuilder(); XmlWriterSettings xws = new XmlWriterSettings(); xws.Indent = true; xws.IndentChars = " "; xws.Encoding = Encoding.UTF8; xws.OmitXmlDeclaration = true; xws.ConformanceLevel = ConformanceLevel.Fragment; xws.CloseOutput = false; XmlWriter xw = XmlWriter.Create(sbuf, xws); xw.WriteStartElement(Value.VALUE_TAG); if (this._type != Type.XMLRPC_TYPE_UNTYPED) { switch (this._type) { case Type.XMLRPC_TYPE_BOOLEAN: if (_boolean_value) { xw.WriteElementString(Value.GetTypeName(_type), "1"); } else { xw.WriteElementString(Value.GetTypeName(_type), "0"); } break; case Type.XMLRPC_TYPE_DOUBLE: xw.WriteElementString(Value.GetTypeName(_type), _double_value.ToString()); break; case Type.XMLRPC_TYPE_INTEGER: xw.WriteElementString(Value.GetTypeName(_type), _int_value.ToString()); break; case Type.XMLRPC_TYPE_BASE64: case Type.XMLRPC_TYPE_DATETIME: case Type.XMLRPC_TYPE_STRING: xw.WriteElementString(Value.GetTypeName(_type), _string_value); break; case Type.XMLRPC_TYPE_NIL: xw.WriteStartElement(Value.GetTypeName(_type)); xw.WriteEndElement(); break; case Type.XMLRPC_TYPE_ARRAY: // iterate over elements and output xw.WriteStartElement(Value.ARRAY_TAG); xw.WriteStartElement(Value.DATA_TAG); foreach (Value v in _array_value) { xw.WriteRaw(v.ToXml()); } xw.WriteEndElement(); // Value.DATA_TAG xw.WriteEndElement(); // Value.ARRAY_TAG break; case Type.XMLRPC_TYPE_STRUCT: // iterate over elements and output xw.WriteStartElement(Value.GetTypeName(_type)); // <struct> foreach (KeyValuePair <String, Value> kvp in _struct_value) { xw.WriteStartElement(Value.MEMBER_TAG); xw.WriteElementString(Value.NAME_TAG, kvp.Key); xw.WriteRaw(kvp.Value.ToXml()); xw.WriteEndElement(); // Value.MEMBER_TAG } xw.WriteEndElement(); // </struct> break; default: throw new Exception("Not implemented :("); } } xw.WriteEndElement(); // Value.VALUE_TAG xw.Flush(); return(sbuf.ToString()); }
public String ToString(bool p_PrintTypes) { StringBuilder sbuf = new StringBuilder(); if (p_PrintTypes) { sbuf.Append("Type " + Value.GetTypeName(_type)).Append(": "); } switch (_type) { case Type.XMLRPC_TYPE_BOOLEAN: if (_boolean_value) { sbuf.Append("1"); } else { sbuf.Append("0"); } break; case Type.XMLRPC_TYPE_DOUBLE: sbuf.Append(_double_value); break; case Type.XMLRPC_TYPE_INTEGER: sbuf.Append(_int_value); break; case Type.XMLRPC_TYPE_BASE64: case Type.XMLRPC_TYPE_DATETIME: case Type.XMLRPC_TYPE_STRING: sbuf.Append(_string_value); break; case Type.XMLRPC_TYPE_NIL: sbuf.Append("nil"); break; case Type.XMLRPC_TYPE_ARRAY: if (p_PrintTypes) { sbuf.Append("\n"); } int i = 0; // iterate over elements and output foreach (Value v in _array_value) { sbuf.Append("Element #").Append(i++).Append(": ").Append(v.ToString(p_PrintTypes)).Append("\n"); } break; case Type.XMLRPC_TYPE_STRUCT: if (p_PrintTypes) { sbuf.Append("\n"); } // iterate over elements and output foreach (KeyValuePair <String, Value> kvp in _struct_value) { sbuf.Append("Member ").Append(kvp.Key).Append(": ").Append(kvp.Value.ToString(p_PrintTypes)).Append("\n"); } break; default: throw new Exception("Not implemented :("); } return(sbuf.ToString()); }