Here's a snippet that might be helpful. I really question whether or not you're using the best approach, so I made some assumptions (perhaps you just haven't given enough details).
I parsed the xml into a XmlDocument
to work with it in code. Relevant tags ("LinkedFile") are pulled out. Each tag is parsed as a Uri
. If that fails, it's unescaped and the parse is attempted again. At the end will be a list of strings containing the urls that parsed correctly. If you really need to, you can use your regex on this collection.
// this is for the interactive console
#r "System.Xml.Linq"
using System.Xml;
using System.Xml.Linq;
// sample data, as provided in the post.
string rawXml = "<SupportingDocs><LinkedFile>http://llcorp/ll/lljomet.dll/open/864606</LinkedFile><LinkedFile>http://llcorp/ll/lljomet.dll/open/1860632</LinkedFile><LinkedFile>%20http%3A%2F%2Fllenglish%2Fll%2Fll.exe%2Fopen%2F927515</LinkedFile><LinkedFile>%20http%3A%2F%2Fllenglish%2Fll%2Fll.exe%2Fopen%2F973783</LinkedFile></SupportingDocs>";
var xdoc = new XmlDocument();
xdoc.LoadXml(rawXml)
// will store urls that parse correctly
var foundUrls = new List<String>();
// temp object used to parse urls
Uri uriResult;
foreach (XmlElement node in xdoc.GetElementsByTagName("LinkedFile"))
{
var text = node.InnerText;
// first parse attempt
var result = Uri.TryCreate(text, UriKind.Absolute, out uriResult);
// any valid Uri will parse here, so limit to http and https protocols
// see https://stackoverflow.com/a/7581824/1462295
if (result && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps))
{
foundUrls.Add(uriResult.ToString());
}
else
{
// The above didn't parse, so check if this is an encoded string.
// There might be leading/trailing whitespace, so fix that too
result = Uri.TryCreate(Uri.UnescapeDataString(text).Trim(), UriKind.Absolute, out uriResult);
// see comments above
if (result && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps))
{
foundUrls.Add(uriResult.ToString());
}
}
}
// interactive output:
> foundUrls
List<string>(4) { "http://llcorp/ll/lljomet.dll/open/864606", "http://llcorp/ll/lljomet.dll/open/1860632", "http://llenglish/ll/ll.exe/open/927515", "http://llenglish/ll/ll.exe/open/973783" }