1

このコードは、10 進数値の列を合計することを意図していますが、そのint.Parse()行で例外をスローします。これを修正するためにコードを修正しようとした方法と、その試みで受け取ったエラーについては、この投稿の最後の方を参照してください。

    static void Main()
    {
        String htmlFile = "C:/Temp/Test11.html";

        HtmlDocument doc = new HtmlDocument();
        doc.Load(htmlFile);

    // Site Collection Storage Used (GB)
    var sum = doc.DocumentNode.SelectSingleNode("//table")
        // The sum will be of the row elements
        .Elements("tr")
        // Skip this many rows from the top
        .Skip(1)
        // .ElementAt(2) = third column
        // .Last() = last column
        .Sum(tr => int.Parse(tr.Elements("td").ElementAt(3).InnerText)); 
    Console.WriteLine("Site Collection Storage Used (GB): " + sum);
        Console.ReadLine();
    }

例外は次のとおりです。

System.FormatException was unhandled
  HResult=-2146233033
  Message=Input string was not in a correct format.
  Source=mscorlib
  StackTrace:
       at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean     parseDecimal)
       at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
       at System.Int32.Parse(String s)
       at ValueSpecifiedCellSpecifiedTable.Program.<Main>b__2(HtmlNode tr) in c:\Users\User1\Documents\Visual Studio     2012\Projects\_ValueSpecifiedCellSpecifiedTable\ValueSpecifiedCellSpecifiedTable\Program.cs:line 92
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at System.Linq.Enumerable.Sum(IEnumerable`1 source)
       at System.Linq.Enumerable.Sum[TSource](IEnumerable`1 source, Func`2 selector)
       at ValueSpecifiedCellSpecifiedTable.Program.Main() in c:\Users\User1\Documents\Visual Studio 2012\Projects    \_ValueSpecifiedCellSpecifiedTable\ValueSpecifiedCellSpecifiedTable\Program.cs:line 85
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state,     Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean     preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

int.Parse() を Decimal.Parse() に置き換えようとしましたが、次のように表示されます:

"Error  1   Cannot implicitly convert type 'decimal' to 'int'. An explicit conversion exists (are you missing a cast?)      C:\Users\hptSetupAdmin\Documents\Visual Studio 2012\Projects\_ValueSpecifiedCellSpecifiedTable\ValueSpecifiedCellSpecifiedTable    \Program.cs  92  17  ValueSpecifiedCellSpecifiedTable

に変更int.Parse()した後、変換するDecimal.Parse()場所がわかりません。int

私の目標を達成するためにこのコードを手伝ってください。

参照用のhtmlファイルは次のとおりです。

<html>
<head>
<title>Tables</title>
</head>
<body>
<table border="1">
  <tr>
    <th>Environment</th>
    <th>Databases</th>
    <th>Sites</th>
    <th>Site Collection Storage Used (GB)</th>
    <th>Ref</th>
 </th>
  </tr>
  <tr>
    <td>Public</td>
    <td>14</td>
    <td>28</td>
    <td>32.6602</td>
    <td>2</td>
  </tr>
  <tr>
    <td>Local</td>
    <td>5</td>
    <td>8</td>
    <td>7.0302</td>
    <td>3</td>
  </tr>
  <tr>
    <td>Shared</td>
    <td>6</td>
    <td>9</td>
    <td>17.092</td>
    <td>9</td>
  </tr>
</table>
4

1 に答える 1

0

変換はほぼ確実に LINQ ステートメントの外にあります。sum が int の場合は、明示的なキャストが必要になります。sum を decimal として宣言するか、明示的なキャストを挿入すると、問題が解決します。

    // Site Collection Storage Used (GB)
    sum = (int)doc.DocumentNode.SelectSingleNode("//table")
        // The sum will be of the row elements
        .Elements("tr")
        // Skip this many rows from the top
        .Skip(1)
        // .ElementAt(2) = third column
        // .Last() = last column
        .Sum(tr => decimal.Parse(tr.Elements("td").ElementAt(3).InnerText)); 
    Console.WriteLine("Site Collection Storage Used (GB): " + sum);

ideone の簡単なデモへのリンクを次に示します(ideone で HTML アジリティ パックを使用することはできないため、データを文字列の配列にコピーしました)。

于 2013-05-25T14:22:59.640 に答える