C# 如何使用OpenXML在word.docx中查找制表符?

C# 如何使用OpenXML在word.docx中查找制表符?,c#,openxml,C#,Openxml,背景:我有一个word.docx文档,其中包含制表符。我想读每一段并用一个空格代替。我需要空格作为分隔符,这样我就可以解析出日期、名称等内容 问题:使用段落.InnerText不会返回制表符,因为它们 是一个单独的xml元素。如果我手动替换空格,我的解析例程工作正常。但是,使用paragration.InnerText返回的文本将被全部压缩 我也无法使用run.InnerText获取选项卡字符。我搜索了一些例子,但没有找到一个能解决问题的 using (Wordprocessin

背景:我有一个word.docx文档,其中包含制表符。我想读每一段并用一个空格代替。我需要空格作为分隔符,这样我就可以解析出日期、名称等内容

问题:使用段落.InnerText不会返回制表符,因为它们 是一个单独的xml元素。如果我手动替换空格,我的解析例程工作正常。但是,使用paragration.InnerText返回的文本将被全部压缩

我也无法使用run.InnerText获取选项卡字符。我搜索了一些例子,但没有找到一个能解决问题的

        using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(filePath, false))
        {
            Body body = wordDocument.MainDocumentPart.Document.Body;

            foreach (var para in body.Elements<Paragraph>())
            {
                s = para.InnerText.ToString();      // Tab chars are stripped
                Console.WriteLine("Run: " + s);
            }
        }

        using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(filePath, false))
            {
                Body body = wordDocument.MainDocumentPart.Document.Body;

                foreach (var para in body.Elements<Paragraph>())
                {
                    s = "";                 // Work string to build full line

                    foreach (var run in para.Elements<Run>())
                    {
                    //  If (This is a tab char)
                    //  { 
                    //     s = s + " ";     // Yes - Substitute a space 
                    //  }
                    //  else    // No - This assumes there are no other xml tags like "Proof Error"
                    //  {
                    //      s = s + run.InnerText.ToString();
                    //  }
                    }
                    Console.WriteLine("Run: " + s);
                }
使用(WordprocessingDocument wordDocument=WordprocessingDocument.Open(filePath,false))
{
Body Body=wordDocument.MainDocumentPart.Document.Body;
foreach(body.Elements()中的变量para)
{
s=para.InnerText.ToString();//剥离制表符
Console.WriteLine(“运行:+s”);
}
}
使用(WordprocessingDocument wordDocument=WordprocessingDocument.Open(filePath,false))
{
Body Body=wordDocument.MainDocumentPart.Document.Body;
foreach(body.Elements()中的变量para)
{
s=”“;//用于生成完整行的工作字符串
foreach(变量在para.Elements()中运行)
{
//If(这是一个制表符字符)
//  { 
//s=s+“”;//是-替换空格
//  }
//else//No-假设没有其他xml标记,如“Proof Error”
//  {
//s=s+run.InnerText.ToString();
//  }
}
Console.WriteLine(“运行:+s”);
}

已解决:我能够找到制表符和替换空格。正在关闭。

我正在使用run元素的.LocalName属性。我可以测试“制表符”


我无权结束这个问题,有人能帮我吗?
                    foreach (var e in run.Elements())
                    {
                        if (e.LocalName == "tab")
                        {
                            Console.WriteLine("    Element Tab: " + e.InnerText.ToString());
                            s = s + " ";
                        }
                        else if (e.LocalName == "t")
                        {
                            Console.WriteLine("    Element Text: " + e.InnerText.ToString());
                            s = s + e.InnerText.ToString();
                        }
                        else
                        {
                            Console.WriteLine("Drop Through RUN set: " + e.LocalName);
                        }
                    }