“'\ f',十六进制值0x0C,是无效字符” - 无法清除它

时间:2016-10-13 13:28:27

标签: c# xml exception ascii

在尝试将我的Xdocument内容保存到文件时,我得到了可怕的“'\ f',十六进制值0x0C,是一个无效字符”。

我用谷歌搜索了它,我试图通过将Xdocument转换为字符串来清除它的非ascii字符(见下文)。我从另一张海报中复制了这样做的方法。

现在抛出异常,我尝试将字符串传递给清理方法。它似乎不想转换为字符串。我尝试输出到文本fie所以我可以看到问题字符是什么,但它然后抛出异常。有什么想法吗?

public void combineContentXmlWithS1000Dtemplate(XElement content)
{

    XDocument XDoc = XDocument.Load(GlobalVars.pathToDMshells + "\\descript.xml" );

    content.Descendants("para").Where(e => string.IsNullOrEmpty(e.Value)).Remove(); // remove all empty para elements

    XDoc.XPathSelectElement("/dmodule/content").Add(content); // adds the new tree to the S1000D template XML

    writeHeaderData(XDoc);

    //System.IO.File.WriteAllText(GlobalVars.pathToOutput + "\\Log.text", XDoc.ToString()); // It even threw exception here

    string cleanedXML = CleanInvalidXmlChars(XDoc.ToString()); // clean the doc of non ascii characters
    XDocument FinishedDM = XDocument.Parse(cleanedXML);

    saveMyS1000Dfile(FinishedDM);
}

public static string CleanInvalidXmlChars(string StrInput)
{
    //Returns same value if the value is empty.
    if (string.IsNullOrWhiteSpace(StrInput))
    {
        return StrInput;
    }
    // From xml spec valid chars:
    // #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]    
    // any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
    string RegularExp = @"[^\x09\x0A\x0D\x20-\xD7FF\xE000-\xFFFD\x10000-x10FFFF]";
    return Regex.Replace(StrInput, RegularExp, String.Empty);
}

1 个答案:

答案 0 :(得分:0)

好的,所以这最终对我有用:

object missing = System.Reflection.Missing.Value;

            object findText = "\f";
            object replaceText = "^p^p";
            currentDocument.Range().Find.Execute(ref findText,
                true, true, true, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref replaceText, Word.WdReplace.wdReplaceAll,
                ref missing, ref missing, ref missing, ref missing);