この問題の基本的な前提は、Webサービスの可能性のあるデータのリストを持つテキストファイルがあることです。テキストファイルに存在するWebサービスのリストから、各Webサービスで使用可能なWebメソッドを解析し、このデータをExcelシートに公開する必要があります。解析ソリューションの改善
私はあなたのテストデータがどのように見えるかの例をあげる:基本的には、最後の行は、私が使用したいラインです
<Resource Name="APP1">
<Uri UriType="PAGE" ResourceUri="http://exampleurl/default.aspx" />
</Resource>
<Resource Name="App2">
<Uri UriType="PAGE" ResourceUri="http://exampleurl2/example.aspx" />
</Resource>
<Resource Name="App3">
<Uri UriType="PAGE" ResourceUri="http://exampleurl3/exampleapp.asmx" />
</Resource>
を。使用可能なラインの他の例は、
<Resource Name="Example" WSDL="http://example.wsdl">
<Uri UriType="ASMX" ResourceUri="http://example.asmx" />
</Resource>
だから、私は基本的に.asmx
と.wsdl
ファイルを探していますされています。私が問題を考えたのは、これらのWebサービスごとにWSDLだけを探すために入力を標準化することでした。したがって、.asmx
のURLに対しては?wsdl
を追加します。
今、私は解決策の下で実装しました。ソースファイルには何千ものWebサービスがあり、潜在的にn個のWebメソッドがあるため、実行時間は1〜2時間かかることがあります。ランタイムをスピードアップするために、このソリューションをさらに改善できるかどうか疑問に思っています。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using System.Net;
using System.Data;
using ClosedXML.Excel;
namespace ParseWebservices
{
class Program
{
static void Main(string[] args)
{
var lines = File.ReadAllText(@"PATH\SourceFIle.xml");
int count = 0;
string text = "";
DataTable Webservices= new DataTable();
Webservices.Columns.Add("Wsdl URL");
Webservices.Columns.Add("Webservice Name");
Webservices.Columns.Add("WebMethod");
Regex r = new Regex("(?<=ResourceUri=\")(.*)(.asmx)(?=\")", RegexOptions.IgnoreCase);
Match m = r.Match(lines.ToString());
while (m.Success)
{
try
{
string[] test = m.ToString().Split('/');
string webservicename = test[test.Length - 1].Replace(".asmx", "");
string wsdlurl="";
var webClient = new WebClient();
string readHtml="";
try
{
readHtml = webClient.DownloadString(wsdlurl);
}
catch (Exception excxx)
{
wsdlurl = m.ToString().Replace(".asmx", ".wsdl");
readHtml = webClient.DownloadString(wsdlurl);
}
int count2 = 0;
string text2 = "";
Regex r2 = new Regex(@"(?<=s:element name\=\"")(.*)(?=Response"")", RegexOptions.IgnoreCase);
Match m2 = r2.Match(readHtml);
while (m2.Success)
{
DataRow dr = Webservices.NewRow();
dr[0] = wsdlurl;
dr[1] = webservicename;
dr[2] = m2.ToString();
Console.WriteLine(wsdlurl + "\n" + webservicename + "\n" + m2.ToString());
Webservices.Rows.Add(dr);
count2++;
m2 = m2.NextMatch();
}
count++;
m = m.NextMatch();
}
catch (Exception ex)
{
m = m.NextMatch();
}
}
XLWorkbook wb = new XLWorkbook();
wb.Worksheets.Add(Webservices, "Example");
wb.SaveAs(@"PATH\example.xlsx");
}
}
}
私がこのソリューションについて気に入らないことの1つは、例外に依存していることです。正規表現は.asmx
の文字列と一致するので、.wsdl
という文字列が見つからないことに気付きました。しかし、私はまた、.wsdl
を含むソーステキストで、接頭辞.asmx
はまったく同じであることに気付きました。だから私はそれらのテストケースのエラー処理を追加しましたが、間違いなく理想的ではありません。
とにかく、私はどのように改善し、より速く(そしてより良いものにする)ための提案に感謝します。
そのドキュメントは完全に有効なXMLのようですが、XDocumentまたはXmlDocumentを使用してデータを解析しようとしましたか?それは非常に大きなファイルを解析する正規表現を使用するよりもはるかに速くなります。 – Gusman
ここで間違ったアプローチをしている可能性があります。入力ファイルがXMLの場合は、正規表現ではなくXMLの解析を調べる必要があります。 –
また、別のスレッドでurlのcreateを連続してチェックするのではなく、これらのurlを消費するキューをparallell – Gusman