0

私はHTMLAgilityを使用してすべての画像を取得しています。これは、画像が常に絶対パスを持っているとは限らないためです。ただし、コードで以下にマークされている行はエラーを生成します

タイプ「System.Uri」を「System.Collections.Generic.List」に暗黙的に変換することはできません

私はこれを修正する方法がわかりません私は非常に多くのオプションを試しましたが、いずれかのエラーが発生し続けます

List<String> imgList = (from x in doc.DocumentNode.Descendants("img")
                      where x.Attributes["src"] != null
                      select x.Attributes["src"].Value.ToLower()).ToList<String>();

List<String> AbsoluteImageUrl = new List<String>();

foreach (String element in imgList)
{
    AbsoluteImageUrl = new Uri(baseUrl, element); //GIVES ERROR
}
4

3 に答える 3

2

AbsoluteImageUrlの型が の型と互換性がないため、コンパイラはエラーを生成しますUriUriを文字列リストに追加する必要がある場合は、その基になる文字列 (例: Uri.AbsolutePath) を取得する必要があります。この場合、コードは次のようになります。

AbsoluteImageUrl.Add(new Uri(baseUrl, element).AbsolutePath);

一方、Uri代わりにリストが必要な場合は、元のコードを保持して のタイプを変更しますAbsoluteImageUrl

List<Uri> AbsoluteImageUrl = new List<Uri>();

これが完了したらAbsoluteImageUrl.Add、ループ内で使用しUriてリストに追加する必要があります。


Uri.ToString()との違いに関するコメントでの議論についてはUri.AbsolutePath、公式の MSDN ごとに異なる定義があるため、使用する OP の要件によって異なります。ちなみに のソースコードはUri.ToString以下の通りで、 とは根本的に異なりAbsolutePathます。

[SecurityPermission(SecurityAction.InheritanceDemand, Flags=SecurityPermissionFlag.Infrastructure)]
public override string ToString()
{
    if (this.m_Syntax == null)
    {
        if (this.m_iriParsing && this.InFact(Flags.HasUnicode))
        {
            return this.m_String;
        }
        return this.OriginalString;
    }
    this.EnsureUriInfo();
    if (this.m_Info.String == null)
    {
        if (this.Syntax.IsSimple)
        {
            this.m_Info.String = this.GetComponentsHelper(UriComponents.AbsoluteUri, (UriFormat) 0x7fff);
        }
        else
        {
            this.m_Info.String = this.GetParts(UriComponents.AbsoluteUri, UriFormat.SafeUnescaped);
        }
    }
    return this.m_Info.String;
}
于 2012-11-01T11:43:30.443 に答える
1

You probably want

AbsoluteImageUrl.Add(new Uri(baseUrl, element).ToString());
于 2012-11-01T11:42:33.207 に答える
0
List<Uri> AbsoluteImageUrl = new List<Uri>();

foreach (String element in imgList)
{
    AbsoluteImageUrl.Add(new Uri(baseUrl, element));
}
于 2012-11-01T11:46:53.067 に答える