C# 十六进制值0x07是无效字符
我正在尝试通过代码将MS Word表格转换为HTML。我正在修改中给出的一些代码,但我需要最终将生成的HTML表转换为CALS表格式,然后与程序生成的现有XML树合并 我目前正在进行从Word表格到HTML表格部分的转换(在将其转换为CALS之前),但我的问题似乎是一个反复出现的错误,即:C# 十六进制值0x07是无效字符,c#,html,xml,xelement,C#,Html,Xml,Xelement,我正在尝试通过代码将MS Word表格转换为HTML。我正在修改中给出的一些代码,但我需要最终将生成的HTML表转换为CALS表格式,然后与程序生成的现有XML树合并 我目前正在进行从Word表格到HTML表格部分的转换(在将其转换为CALS之前),但我的问题似乎是一个反复出现的错误,即: hexadecimal value 0x07, is an invalid character 果然,如果我在程序运行时通过messageBox查看表中每个单元格生成的HTML,我可以看到在表单元格的文本后
hexadecimal value 0x07, is an invalid character
果然,如果我在程序运行时通过messageBox查看表中每个单元格生成的HTML,我可以看到在表单元格的文本后面有一个小“框”
我试过用类似的东西
string newContent = content.Replace((char)(0x1F), Convert.ToChar(""));
替换字符,但它会抱怨字符串长度必须为一个字符
我可能做得不对,因为我试图将HTML存储在XElement中。但我不认为这是问题的根源
问题显然是单词表单元格中的小“框”,但不确定它是什么,也不确定如何忽略或删除它
这是我的密码
private void dealWithTables()
{
try
{
foreach (Table tb in doc.Tables)
{
for (int r = 1; r <= tb.Rows.Count; r++)
{
for (int c = 1; c <= tb.Columns.Count; c++)
{
try
{
Cell cell = tb.Cell(r, c);
foreach (Paragraph paragraph in cell.Range.Paragraphs)
{
Tagging2(paragraph.Range, "P", paragraph.Range.Text);
}
Tagging2(cell.Range, "TD");
}
catch (Exception e)
{
if (e.Message.Contains("The requested member of the collection does not exist."))
{
//Most likely a part of a merged cell, so skip over.
}
else throw;
}
}
try
{
Row row = tb.Rows[r];
Tagging2(row.Range, "TR");
}
catch (Exception ex)
{
bool initialTrTagInserted = false;
int columnsIndex = 1;
int columnsCount = tb.Columns.Count;
while (!initialTrTagInserted && columnsIndex <= columnsCount)
{
try
{
Cell cell = tb.Cell
(r, columnsIndex);
//cell.Range.InsertBefore("<TR>");
initialTrTagInserted = true;
}
catch (Exception e)
{
}
columnsIndex++;
}
columnsIndex = tb.Columns.Count;
bool endTrTagInserted = false;
while (!endTrTagInserted && columnsIndex >= 1)
{
try
{
Cell cell = tb.Cell(r, columnsIndex);
//cell.Range.InsertAfter("</TR>");
endTrTagInserted = true;
}
catch (Exception e)
{
}
columnsIndex--;
}
}
}
Tagging2(tb.Range, "Table");
object separator = "";
object nestedTable = true;
tb.ConvertToText(separator, nestedTable);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
XElement tableTree = new XElement("table");
public void Tagging2(Range range, string tagName, string content)
{
string newContent = content.Replace((char)(0x1F), Convert.ToChar(""));
tableTree.Add(new XElement(tagName, newContent));
MessageBox.Show("text of para " + newContent);
}
public void Tagging2(Range range, string tagName)
{
tableTree.Add(new XElement(tagName));
}
private void处理表()
{
尝试
{
foreach(文档表中的表tb)
{
对于(int r=1;r而言,您似乎正在用空字符串替换它,因此出现了错误消息。请尝试用空格替换它:
content.Replace((char)(0x07), (char)(0x20))
content.Replace((char)(0x07),(char)(0x20))
您似乎将其替换为空字符串,因为0x07似乎是“单元格结束标记”或“行结束标记”:单元格结束标记:十六进制值为“0x07”的字符这是用来表示表中单元格的结尾的。@RubensFarias谢谢。这使我能够继续前进。太好了。@MatthewWatson,谢谢你-这解释了字符及其含义。我以前从未遇到过这种情况,但我以前从未发过关于转换表的消息(希望以后再也不会)。