private void Parse() { Boolean found = false; StringReader sr = new StringReader(_raw_response); XmlDocument xdoc = new XmlDocument(); xdoc.Load(sr); // won't use GetElementsByTagName to validate the actual document structure. // find methodResponse tag found = false; XmlNode methodResponseNode = null; foreach (XmlNode x in xdoc.ChildNodes) { if (x.NodeType == XmlNodeType.Element && x.Name == MethodResponse.METHOD_RESPONSE_TAG) { methodResponseNode = x; found = true; break; } } if (!found) { throw new Exception(MethodResponse.METHOD_RESPONSE_TAG + " tag not found on response"); } // find params or fault element found = false; foreach (XmlNode x in methodResponseNode.ChildNodes) { if (x.NodeType != XmlNodeType.Element) { continue; } if (x.Name == MethodResponse.FAULT_TAG) { foreach (XmlNode y in x.ChildNodes) { if (y.NodeType != XmlNodeType.Element) { continue; } if (y.Name == Value.VALUE_TAG) { _fault_value = Value.FromXml(y); found = true; _fault = true; } } break; } else if (x.Name == MethodResponse.PARAMS_TAG) { _fault = false; // read all parameters returned foreach (XmlNode y in x.ChildNodes) { if (y.NodeType != XmlNodeType.Element) { continue; } if (y.Name != MethodResponse.PARAM_TAG) { throw new Exception("Invalid " + y.Name + " tag inside params"); } else { // find value node foreach (XmlNode z in y.ChildNodes) { if (z.NodeType != XmlNodeType.Element) { continue; } if (z.Name == MethodResponse.VALUE_TAG) { // add to values _values.Add(Value.FromXml(z)); } else { throw new Exception("Invalid tag " + z.Name + " inside param tag"); } } } } found = true; break; } } if (!found) { throw new Exception(MethodResponse.FAULT_TAG + " or " + MethodResponse.PARAMS_TAG + " not found!"); } }
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); }