Profiles.Edit.Utilities.DataIO.GetURIRelLink C# (CSharp) Method

GetURIRelLink() public method

public GetURIRelLink ( string URL, string &passeduri ) : XmlDocument
URL string
passeduri string
return System.Xml.XmlDocument
        public XmlDocument GetURIRelLink(string URL, ref string passeduri)
        {
            string result = string.Empty;
            XmlDocument rawdata = new XmlDocument();
            string rawhtml = string.Empty;
            XmlDocument htmlhead = new XmlDocument();
            try
            {
                HttpWebRequest request = null;
                request = (HttpWebRequest)WebRequest.Create(URL);
                request.Method = "POST";
                request.ContentType = "application/rdf+xml";
                request.ContentLength = URL.Length;

                using (Stream writeStream = request.GetRequestStream())
                {
                    UTF8Encoding encoding = new UTF8Encoding();
                    byte[] bytes = encoding.GetBytes(URL);
                    writeStream.Write(bytes, 0, bytes.Length);
                }

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
                        {
                            result = readStream.ReadToEnd();
                            readStream.Close();
                        }

                        responseStream.Close();
                    }
                }

                if (result.ToLower().Contains("<html"))
                {
                    string xml = result.Substring(result.ToLower().IndexOf("<head"), (result.ToLower().IndexOf("/head>") - result.ToLower().IndexOf("<head")) - 1) + "</head>";

                    rawhtml = result.Substring(result.ToLower().IndexOf("<head"), (result.ToLower().IndexOf("/head>") - result.ToLower().IndexOf("<head")) - 1);

                    int stopindex = 0;
                    int startindex = rawhtml.IndexOf("alternate");
                    for (int i = rawhtml.IndexOf("alternate"); i < rawhtml.Length; i++)
                    {
                        if (rawhtml.Substring(i, 2) == "/>")
                        {
                            stopindex = (i + 2) - startindex;

                            i = rawhtml.Length;
                        }
                    }

                    xml = rawhtml.Substring(startindex, stopindex);

                    if (xml.Contains("\""))
                    {
                        xml = "<link rel=\"" + xml;

                    }
                    else if (xml.Contains("'"))
                    {
                        xml = "<link rel='" + xml;
                    }
                    htmlhead.LoadXml(xml.ToLower());

                    //<link rel="alternate" type="application/rdf+xml" href="/individual/n25562/n25562.rdf" />

                    string uri = htmlhead.SelectSingleNode("link/@href").Value;

                    //If a prefix of the / char or ~ exists then remove them
                    if (uri.Substring(0, 1) == "~")
                        uri = uri.Substring(2);

                    if (uri.Substring(0, 1) == "/")
                        uri = uri.Substring(1);

                    if (!uri.Contains("http"))
                    {
                        string[] domianparse = URL.Split('/');

                        uri = domianparse[0] + "//" + domianparse[2] + "/" + uri;

                    }

                    passeduri = uri;
                    if (passeduri.Contains(".rdf"))
                    {
                        string[] rdfparse = passeduri.Split('/');
                        passeduri = passeduri.Replace("/" + rdfparse[rdfparse.Length - 1], "");

                    }

                    result = this.GetURIRelLink(uri, ref passeduri).OuterXml;

                }
                else if (result.Contains("rdf:RDF"))
                {
                    // do nothing, let it be loaded into the rawdata xml doc below.
                }
                else
                {
                    result = "<eof/>";
                }

                rawdata.LoadXml(result);

            }
            catch (Exception ex)
            {
                Framework.Utilities.DebugLogging.Log(ex.Message + " ++ " + ex.StackTrace);
            }

            return rawdata;
        }

Usage Example

        protected void cmdSaveByURI_onclick(object sender, ImageClickEventArgs e)
        {
            Edit.Utilities.DataIO data = new Profiles.Edit.Utilities.DataIO();

            if (txtURI.Text.ToLower().Trim() != string.Empty)
            {
                string uri = string.Empty;
                bool validuri = false;
                bool urifound = false;
                XmlNamespaceManager nmsmanager = null;

                pnlAddByURI.Visible = false;

                    //you need to get back the actual URI that is discovered during the post.
                    //The post might be to an HTML document where I have to look for an alt link to RDF data.
                    //Plus the URI we get back from this method needs to be used to create a triple in our Database so dont lose it.
                    XmlDocument rtndata = data.GetURIRelLink(txtURI.Text.Trim(), ref uri);

                    if (rtndata.InnerXml != string.Empty)
                        urifound = true;

                    if (urifound)
                    {
                        Framework.Utilities.Namespace namespaces = new Namespace();
                        nmsmanager = namespaces.LoadNamespaces(rtndata);

                        if (rtndata.SelectSingleNode("rdf:RDF/rdf:Description/rdf:type[@rdf:resource='" + PropertyList[0].Value + "']", nmsmanager) != null)
                            validuri = true;
                    }

                    if (uri.Trim() != string.Empty)
                    {
                        litAddByURIConfirmLabel.Text = rtndata.SelectSingleNode("rdf:RDF/rdf:Description[@rdf:about='" + uri + "']/rdfs:label", nmsmanager).InnerText;
                        hdnURI.Value = uri;
                    }
                    else
                    {
                        litAddByURIConfirmLabel.Text = txtURI.Text.Trim();
                        hdnURI.Value = txtURI.Text.Trim();

                    }

                    if (validuri)
                        litAddByURIConfirmType.Text = PropertyList[0].Text;
                    else if (!urifound)
                        litAddByURIConfirmType.Text = "The item could not be found. Do you still want to add this URL?";
                    else if (!validuri)
                        litAddByURIConfirmType.Text = "The item type is invalid. Do you want to add this item anyway?";

                    pnlAddByURIConfirm.Visible = true;
                    pnlAddBySearch.Visible = false;
                    pnlAddNew.Visible = false;
                    pnlAddByURI.Visible = false;
                    phAddBySearch.Visible = false;
                    phAddNew.Visible = false;
                    phAddByURL.Visible = true;
                    imgAddArror.ImageUrl = "~/Framework/Images/icon_squareDownArrow.gif";
                    Session["pnlAddByURI.Visible"] = true;

                }
        }
All Usage Examples Of Profiles.Edit.Utilities.DataIO::GetURIRelLink