简体   繁体   English

十六进制值 0x07,是一个无效字符

[英]hexadecimal value 0x07, is an invalid character

I'm trying to convert MS Word tables to HTML via code.我正在尝试通过代码将 MS Word 表转换为 HTML。 I'm in the process of adapting some code given in this answer but I need the resulting HTML table to ultimately be converted to a CALS table format, then merged with an existing XML tree my program generates.我正在调整此答案中给出的一些代码,但我需要最终将生成的 HTML 表转换为 CALS 表格式,然后与我的程序生成的现有 XML 树合并。

I'm currently working on the conversion from Word table to HTML table part (prior to converting that to CALS) but my problem seems to be a recurring error which says:我目前正在处理从 Word 表到 HTML 表部分的转换(在将其转换为 CALS 之前),但我的问题似乎是一个反复出现的错误,它说:

hexadecimal value 0x07, is an invalid character

And sure enough, if i look at the resulting HTML from each cell in the table via a messageBox as my program runs, i can see there is a small 'box' after the text from the table cell.果然,如果我在程序运行时通过 messageBox 查看表格中每个单元格的结果 HTML,我可以看到表格单元格中的文本后面有一个小“框”。

I have tried using something like我试过使用类似的东西

string newContent = content.Replace((char)(0x1F), Convert.ToChar(""));

to replace the character, but it complains then that the string must be one character long.替换字符,但它会抱怨字符串必须是一个字符长。

I may be going about things the wrong way in the sense that i'm trying to store HTML inside an XElement.我可能会以错误的方式处理事情,因为我试图将 HTML 存储在 XElement 中。 But i don't think this is causing the problem?!但我不认为这是造成问题的原因?!

The issue is clearly the little 'box' in the Word table cells, but not sure what it is or how to ignore or remove it.问题显然是 Word 表格单元格中的小“框”,但不确定它是什么或如何忽略或删除它。

Here is my code这是我的代码

        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));
        }

Seems you're replacing it with an empty string, thus your error message.似乎您正在用空字符串替换它,因此您的错误消息。 Try to replace it with a white space:尝试用空格替换它:

content.Replace((char)(0x07), (char)(0x20))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导出到excel数据时出错:&#39;&#39;,十六进制值0x07,使用C#是无效字符 - error while exporting to excel data : ' ', hexadecimal value 0x07, is an invalid character using c# 十六进制值0x1E,是无效字符 - hexadecimal value 0x1E, is an invalid character 十六进制值0x00是加载XML文档的无效字符 - Hexadecimal value 0x00 is a invalid character loading XML document 错误:十六进制值 0x00 是无效字符 c# - Error: hexadecimal value 0x00 is an invalid character c# XMLDocument.Load(),十六进制值0x17,是无效字符 - XMLDocument.Load(), hexadecimal value 0x17, is an invalid character {&quot;&#39;\&#39;,十六进制值 0x04,是无效字符 - {"'\u0004', hexadecimal value 0x04, is an invalid character 十六进制值0x1F是无效字符。 第1行,位置1 - hexadecimal value 0x1F, is an invalid character. Line 1, position 1 C# 十六进制值 0x12,是一个无效字符 - C# hexadecimal value 0x12, is an invalid character &#39;&lt;&#39;,十六进制值 0x3C,是无效的属性字符 - '<', hexadecimal value 0x3C, is an invalid attribute character 发布到Azure错误 - 错误:&#39;&#39;,十六进制值0x0F,是无效字符。 第1行,第285位 - Publish to Azure Error - Error : '', hexadecimal value 0x0F, is an invalid character. Line 1, position 285
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM