Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Word文档。被open XML find损坏&;代替_C#_Openxml_Openxml Sdk - Fatal编程技术网

C# Word文档。被open XML find损坏&;代替

C# Word文档。被open XML find损坏&;代替,c#,openxml,openxml-sdk,C#,Openxml,Openxml Sdk,我正在使用C#中的以下函数使用OpenXMLSDK查找和替换word文档 // To search and replace content in a document part. public static void SearchAndReplace(string document) { using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true)) { st

我正在使用C#中的以下函数使用OpenXMLSDK查找和替换word文档

// To search and replace content in a document part.
public static void SearchAndReplace(string document)
{
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
    {
        string docText = null;
        using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
        {
            docText = sr.ReadToEnd();
        }

        Regex regexText = new Regex("Findme");
        docText = regexText.Replace(docText, "Before *&* After"); // <-- Replacement text

        using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
        {
            sw.Write(docText);
        }
    }
}
//搜索和替换文档部分中的内容。
公共静态无效搜索和替换(字符串文档)
{
使用(WordprocessingDocument wordDoc=WordprocessingDocument.Open(document,true))
{
字符串docText=null;
使用(StreamReader sr=newstreamreader(wordDoc.MainDocumentPart.GetStream())
{
docText=sr.ReadToEnd();
}
Regex regexText=新正则表达式(“Findme”);

docText=regexText.Replace(docText,“Before*&*After”);//你试过“Before&After”吗?我现在试过了&是的,它起作用了..你能解释一下为什么会发生..实际上替换文本是动态的,即我不知道“&”的位置在哪里还有太多的文本需要替换,因此搜索“&”的位置也是不可行的在每一个字符串中..是否还有其他此类问题持续存在的情况?它是一个特殊字符。XML中的所有特殊字符都必须转义。请参阅。如果文本是动态的,我们该怎么做..我必须使用正则表达式还是有其他更简单的方法..您可以始终通过
HttpUtil包装替换文本html.HtmlEncode
(虽然它说“HtmlEncode”,但它同样适用于XML)这样,文本总是在必要时转义。
// To search and replace content in a document part.

public static void SearchAndReplace(string document)
{
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
    {
        string docText = null;
        using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
        {
            docText = sr.ReadToEnd();
        }

        Regex regexText = new Regex("Findme");
        docText = regexText.Replace(docText, new System.Xml.Linq.XText("Before *&* After").ToString()); // when we replace string with "&" we need to convert like this

        using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
        {
            sw.Write(docText);
        }
    }
}