這裏是低於c#字符串處理中的錯誤是什麼?
<h5>Sample Document </h5>
<h3> Present Tense </h3>
</p><p>The present tense is just as you have learned. You take the dictionary form of a verb, drop the 다, add the appropriate ending.
</p><p>먹다 - 먹 + 어요 = 먹어요 <br />
마시다 - 마시 + 어요 - 마시어요 - 마셔요. <br />
</p><p>This tense is used to represent what happens in the present. I eat. I drink. It is a general term for the present.
因爲考慮到我傳遞的MemoryStream的代碼下面提到的程序包含三個功能
Main
ReadDocument
TestByteOffSet
樣本HTML文件
主函數將上述指定的HTML文檔轉換爲memoryStream,然後將其進一步傳遞給ReadDocument函數,該函數將結果存儲在名爲docContent的變量中。它是一個類級別的變量。
然後,主函數使用myRange.Text嘗試在給定文檔中查找其索引。一旦找到索引,它將存儲在intByteOffSet變量中。
現在第三個函數TestByteOffSet試圖確保存儲在intByteOffSet中的索引是否正確。
這裏我有問題,當我嘗試從byteOffSet獲取字符串時,我沒有收到選定的文本。
的源代碼
using System;
using System.Collections.Generic;
using System.Text;
namespace MultiByteStringHandling
{
class Program
{
static void Main(string[] args)
{
FileStream fs = new FileStream(FileName, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] bit = br.ReadBytes((int)fs.Length);
MemoryStream Mr = new MemoryStream(bit);
ReadDocument(Mr);
mshtml.IHTMLTxtRange CompleteRange =
_body.createTextRange().duplicate();
int intByteOffset = 0;
Regex reg = default(Regex);
try
{
// Get all of the text that is in between HTML tags.
string regSearchText = myRange.htmlText;
string strTemp = regSearchText + "\\s*";
string strExp = ">(([^<])*?)" + strTemp + "(([^<])*?)<";
string _cleanedSource = "";
_cleanedSource = CompleteRange.htmlText;
// Use regular expressions to find a collection of matches
//that match a certain pattern.
foreach (Match m in Regex.Matches(_cleanedSource, strExp,
RegexOptions.IgnoreCase))
{
Int32 ret = default(Int32);
Int32 index = default(Int32);
string strMatch = m.Value;
foreach (Match m2 in Regex.Matches(strMatch, strTemp,
RegexOptions.IgnoreCase))
{
// Increment counter when finding a match.
intCount += 1;
// If counter matches occurrence number, return
//source offset.
if (intCount == OccurenceNo)
{
//Source offset is the index of the overall
//match + index innerText Match.
int intCharOffset = m.Index + m2.Index;
System.Text.UTF8Encoding d = new
System.Text.UTF8Encoding();
// Using the SourceText will give an accurate
//byte offset.
intByteOffset = d.GetBytes(
_cleanedSource.Substring(0, intCharOffset)).Length;
}
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
}
private void ReadDocument(Stream sD)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.BinaryWriter bw = new System.IO.BinaryWriter(ms);
bool hasMore = true;
sD.Position = 0;
using (System.IO.BinaryReader br = new System.IO.BinaryReader(sD))
{
while (hasMore)
{
byte[] buffer = br.ReadBytes(8192);
hasMore = buffer.Length > 0;
if (hasMore)
{
bw.Write(buffer);
}
}
}
byte[] docBuffer = ms.GetBuffer();
docContent = new byte[docBuffer.Length + 1];
Array.Copy(docBuffer, docContent, docBuffer.Length);
}
private bool TestByteOffset(TransparencyItemType transparency)
{
System.Text.UTF8Encoding encoding = default(System.Text.UTF8Encoding);
string byteOffsetLabel = null;
Int32 iLength = default(Int32);
Int32 offset = default(Int32);
if (((transparency.Label == null) == false))
{
iLength = Convert.ToInt32(transparency.Label.IEOffset.Length);
offset = Convert.ToInt32(transparency.Label.IEOffset.Offset);
}
else if (((transparency.Value == null) == false))
{
if(transparency.Value.ByteOffset!=null)
{
if (transparency.Value.ByteOffset.Offset != -1)
{
iLength = Convert.ToInt32(transparency.Value.ByteOffset.Length);
offset = Convert.ToInt32(transparency.Value.ByteOffset.Offset);
}
}
}
else
{
return false;
}
}
因此,您將得到一個表示文件的Stream,將其讀入一個字節數組,然後從該字節數組創建一個新的流,並將該流傳遞給ReadDocument,從而將其轉換爲一個字節數組。爲什麼不簡單地將第一個FileStream傳遞給ReadDocument。或者更好的是,將FileStream的內容讀入一個字符串,並對該字符串進行操作? – 2009-11-13 08:53:25
我明白你的意思,但我想我做的是同樣的事情,雖然沒有在適當的方式。 – Sandhurst 2009-11-13 08:57:32
開始刪除你有的空'catch'。你可以放一個'throw;'來重新拋出異常。 空捕獲將默默吞下任何異常,這意味着你不知道代碼是否正在工作,並且你不知道問題出現在哪裏或爲什麼。 – Guffa 2009-11-13 09:00:36