The controller ActionResult (Site is .Net MVC) using this class either uses:
BusInfo bi = new Models.BusInfo();
IEnumerable
ViewData["PageTitle"] = "Business Information";
return View(model);
or
BusInfo bi = new Models.BusInfo();
IEnumerable
ViewData["PageTitle"] = "Business Information";
return View(model);
depending on whether I want to get Json or Xml.
I have 3 Xml retrieval methods. The default one used above places the Xml data in a DataSet, then to the list.
The 2nd, getDataX2, has no middleman. Data goes from Xml to list, via XmlReader.
The 3rd, getDataX3, converts Xml to Json, thence to list.
The class is below:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Data;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.Runtime.Serialization.Json;//used in getDataJ
using System.Xml;
using System.Xml.Linq;
namespace SiteName.Models
{
public class BusInfo
{
public string title { get; set; }
public string url { get; set; }
public string description { get; set; }
public string keywords { get; set; }
public string category { get; set; }
readonly string baseUrij = "http://api.sba.gov/rec_sites/all_sites/keywords.json";//json
readonly string baseUrix = "http://api.sba.gov/rec_sites/all_sites/keywords.xml";//xml
public BusInfo()
: base()
{
}
//this constructor is needed if using getDataX2 because:
//we need an alternative while looping through the reader, to setting a BusInfo instance for each group of properties
//in order to Add to List
//ie, bi.Add(b) just puts a reference to 'b' in each row when set inside the reader. change b, change every member of bi
//instead we're setting local variables that are fed directly into bi.Add as a new BusInfo() for each iteration
public BusInfo(string t, string u, string d, string k, string c)
{
title = t;
url = u;
description = d;
keywords = k;
category = c;
}
public List
{
string uri = baseUrij;
WebRequest wr = WebRequest.Create(uri);
WebResponse wrs = wr.GetResponse();
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(List
List
return bi;
}//end getDataJ
public List
{
//parent node in dataset becomes 'sites_id' in place of 'sites site'
string uri = baseUrix;
HttpWebRequest wr = (HttpWebRequest) WebRequest.Create(uri);
wr.Method = "GET";
List
DataSet ds = new DataSet();
ds.ReadXml(uri);
if (ds != null)
{
if (ds.Tables.Count > 0)
{
foreach (DataTable dt in ds.Tables)
{
foreach (DataRow dr in dt.Rows)
{
BusInfo b = new BusInfo();
foreach (DataColumn dc in dr.Table.Columns)
{
switch (dc.ColumnName)
{
case "title":
b.title = dr[dc.ColumnName].ToString().Trim();
break;
case "url":
b.url = dr[dc.ColumnName].ToString().Trim();
break;
case "description":
b.description = dr[dc.ColumnName].ToString().Trim();
break;
case "keywords":
b.keywords = dr[dc.ColumnName].ToString().Trim();
break;
case "category":
b.category = dr[dc.ColumnName].ToString().Trim();
break;
default:
break;
}
}
bi.Add(b);
}
}
}
}
return bi;
}//end getDataX
public List
{
List
string uri = baseUrix;
string em = "";
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
settings.DtdProcessing = DtdProcessing.Parse;
int bc = 0;
bool runBiAdd = false;
try
{
using (XmlReader reader = XmlReader.Create(uri, settings))
{
//cannot use an instance of BusInfo inside reader - must use local variables
string btitle = "";
string burl = "";
string bdescription = "";
string bkeywords = "";
string bcategory = "";
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
runBiAdd = false;
switch (reader.Name)
{
case "title":
bc++;
btitle = reader.ReadString().ToString();
runBiAdd = true;
break;
case "url":
bc++;
burl = reader.ReadString().ToString();
runBiAdd = true;
break;
case "description":
bc++;
bdescription = reader.ReadString().ToString();
runBiAdd = true;
break;
case "keywords":
bc++;
bkeywords = reader.ReadString().ToString();
runBiAdd = true;
break;
case "category":
bc++;
bcategory = reader.ReadString().ToString();
runBiAdd = true;
break;
default:
break;
}
if (runBiAdd = true && bc == 5)
{
bi.Add(new BusInfo(btitle,burl,bdescription,bkeywords,bcategory));//see notes by constructor above
bc = 0;
}
}
}
}
}
catch (Exception ex)
{
em = "An Error Occured: " + ex.Message;
bi.Clear();
}
return bi;
}//end getDataX2
//gets xml into json, then into list
//have to know structure of xml schema
public List
{
List
string uri = baseUrix;
bi.Clear();
string em = "";
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
settings.DtdProcessing = DtdProcessing.Parse;
XmlDocument doc = new XmlDocument();
try
{
using (XmlReader reader = XmlReader.Create(uri, settings))
{
while (reader.Read())
{
doc.Load(reader);
}
}
string json = XMLToJson.XmlToJSON(doc);
JObject jo = JObject.Parse(json);
foreach (var v in jo["sites"]["site"])
{
BusInfo b = new BusInfo();
b.title = v["title"].ToString();
b.url = v["url"].ToString();
b.description = v["description"].ToString();
b.keywords = v["keywords"].ToString();
b.category = v["category"].ToString();
bi.Add(b);
}
}
catch (Exception ex)
{
em = "An Error Occured: " + ex.Message;
}
return bi;
}//end getDataX3
}
}