protected void Page_Load(object sender, EventArgs e) { string url = "http://www.xxxxxxxxxx.yyy/"; UfWebRequest webRequest = null; UfErrors errors = new UfErrors(); try { webRequest = new UfWebRequest(); webRequest.Load(url, UfFormats.HCard()); } catch (Exception ex) { if(webRequest.Urls.Count > 0) errors.Add(new UfError(ex.Message, url, webRequest.Urls[0].Status)); else errors.Add(new UfError(ex.Message, url)); } UfDataToJson dataConvertor = new UfDataToJson(); Response.ContentType = "application/json"; //Add reporting and errors dataConvertor.Urls = webRequest.Urls; dataConvertor.Errors = errors; Response.Write(dataConvertor.Convert(webRequest.Data, UfFormats.HCard())); }
/// <summary> /// Converts a UfDataNode structure into JSON /// </summary> /// <param name="node">Node</param> /// <param name="formatArray">Array of microformat format describer to describer data in node</param> /// <param name="callBack">JSONP callback function name to wrap JSON object</param> /// <returns>JSON string</returns> public string Convert(UfDataNode node, ArrayList formatArray, string callBack) { this.callBack = callBack; this.callBack = this.callBack.Replace("(", "").Replace(")", "").Trim(); this.urls = urls; this.errors = errors; foreach (UfFormatDescriber formatDescriber in formatArray) { foreach (UfDataNode childNode in node.Nodes) { foreach (UfDataNode grandChildNode in childNode.Nodes) { if (grandChildNode.Name == formatDescriber.BaseElement.Name) { UfDataNode xChild = tree.Nodes.Append(grandChildNode.Name, grandChildNode.Value, grandChildNode.SourceUrl, grandChildNode.RepresentativeNode); if (grandChildNode.Nodes.Count > 0) { AddChildNodes(xChild, grandChildNode, formatDescriber.BaseElement); } } } } } //string output = "// UfXtract \n"; string output = ""; if (callBack != string.Empty) { output += callBack + "( "; } output += "{"; foreach (UfDataNode childNode in tree.Nodes) { output += BuildDataString(childNode, true, false); } if (tree.Nodes.Count > 0) { output = output.Substring(0, output.Length - 2); } output += AddUfErrors(); output += AddReporting(node); // End whole block output += "}"; if (callBack != string.Empty) { output += " )"; } //return output.Replace(",", ",\n").Replace("}", "}\n").Replace("{", "{\n").Replace("]", "]\n").Replace("[", "[\n"); ; return(output); }
/// <summary> /// Converts a UfDataNode structure into JSON /// </summary> /// <param name="node">Node</param> /// <param name="formatArray">Array of microformat format describer to describer data in node</param> /// <param name="callBack">JSONP callback function name to wrap JSON object</param> /// <returns>JSON string</returns> public string Convert(UfDataNode node, ArrayList formatArray, string callBack) { this.callBack = callBack; this.callBack = this.callBack.Replace("(", "").Replace(")", "").Trim(); this.urls = urls; this.errors = errors; foreach (UfFormatDescriber formatDescriber in formatArray) { foreach (UfDataNode childNode in node.Nodes) { foreach (UfDataNode grandChildNode in childNode.Nodes) { if (grandChildNode.Name == formatDescriber.BaseElement.Name) { UfDataNode xChild = tree.Nodes.Append(grandChildNode.Name, grandChildNode.Value, grandChildNode.SourceUrl, grandChildNode.RepresentativeNode); if (grandChildNode.Nodes.Count > 0) AddChildNodes(xChild, grandChildNode, formatDescriber.BaseElement); } } } } //string output = "// UfXtract \n"; string output = ""; if (callBack != string.Empty) output += callBack + "( "; output += "{"; foreach (UfDataNode childNode in tree.Nodes) output += BuildDataString(childNode, true, false); if (tree.Nodes.Count > 0) output = output.Substring(0, output.Length - 2); output += AddUfErrors(); output += AddReporting(node); // End whole block output += "}"; if (callBack != string.Empty) output += " )"; //return output.Replace(",", ",\n").Replace("}", "}\n").Replace("{", "{\n").Replace("]", "]\n").Replace("[", "[\n"); ; return output; }