Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.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
如何使用类似json格式的C#OpenXMLSDK从Word文档中获取文本?_C#_Ms Word_Openxml Sdk_Text Extraction - Fatal编程技术网

如何使用类似json格式的C#OpenXMLSDK从Word文档中获取文本?

如何使用类似json格式的C#OpenXMLSDK从Word文档中获取文本?,c#,ms-word,openxml-sdk,text-extraction,C#,Ms Word,Openxml Sdk,Text Extraction,我试图使用C#open xml SDK从word文档中提取所有文本,我希望存储每个段落中的样式属性和innerText数据,然后存储段落中的每个运行(最终对表也是如此) 这是理想的结构 我来自python背景,在那里使用python dict很容易,因为dict中键的值没有严格的类型。但我对如何在C#中实现这一点几乎一无所知 static void Main(字符串[]args) { 字符串文件名=@“”; ​ 使用(WordprocessingDocument myDoc=Wordproces

我试图使用C#open xml SDK从word文档中提取所有文本,我希望存储每个段落中的样式属性和innerText数据,然后存储段落中的每个运行(最终对表也是如此)

这是理想的结构

我来自python背景,在那里使用python dict很容易,因为dict中键的值没有严格的类型。但我对如何在C#中实现这一点几乎一无所知

static void Main(字符串[]args)
{
字符串文件名=@“”;
​
使用(WordprocessingDocument myDoc=WordprocessingDocument.Open(文件名,true))
{
IEnumerable paragraphList=myDoc.MainDocumentPart.Document.Body.Elements(),其中(c=>c是段落).Cast();
foreach(段落列表中的p段)
{
字符串段落InnerText=p.InnerText;
IEnumerable runList=p.ChildElements.Where(c=>c是Run.Cast();
foreach(在runList中运行r)
{
字符串runInnerText=r.InnerText;
IEnumerable runProperties=r.ChildElements.Where(c=>c是runProperties.Cast();
WriteLine(“获取运行数据”);
}
IEnumerable paragraphPropertiesList=p.ChildElements.Where(r=>r是ParagraphProperties.Cast();
}
}
控制台。WriteLine(“全部完成。按一个键”);
}
这是我到目前为止提出的代码,需要一些帮助

关于如何将其存储为json格式,或者应该如何操作,有什么想法吗?
提前感谢!:)

选项A,使用带有SerializeXmlNode()的源xml转换所有文档:

选项B,为所有段落和运行创建匿名类型:

IEnumerable<Paragraph> paragraphList = myDoc.MainDocumentPart.Document.Body.Elements().OfType<Paragraph>();
var proj = paragraphList.Select(p => p.ChildElements.OfType<Run>().Select(r => new
{
    r.InnerText,
    runProperties = r.ChildElements.OfType<RunProperties>().FirstOrDefault()?.Select(rp => new { rp.GetType().Name, Val = rp.GetAttributes().FirstOrDefault().Value })
}));
var json = JsonConvert.SerializeObject(proj, Newtonsoft.Json.Formatting.Indented);
IEnumerable paragraphList=myDoc.MainDocumentPart.Document.Body.Elements(),of type();
var proj=paragraphList.Select(p=>p.ChildElements.OfType().Select(r=>new
{
r、 InnerText,
runProperties=r.ChildElements.OfType().FirstOrDefault()?.Select(rp=>new{rp.GetType().Name,Val=rp.GetAttributes().FirstOrDefault().Value})
}));
var json=JsonConvert.serialized对象(proj,Newtonsoft.json.Formatting.Indented);
static void Main(string[] args)
        {
            string fileName = @"<path to file>";
​
            using (WordprocessingDocument myDoc = WordprocessingDocument.Open(fileName, true))
            {
                IEnumerable<Paragraph> paragraphList = myDoc.MainDocumentPart.Document.Body.Elements().Where(c => c is Paragraph).Cast<Paragraph>();
                foreach (Paragraph p in paragraphList)
                {
                    string paragraphInnerText = p.InnerText;
                    IEnumerable<Run> runList = p.ChildElements.Where(c => c is Run).Cast<Run>();
                    foreach (Run r in runList)
                    {
                        string runInnerText = r.InnerText;
                        IEnumerable<RunProperties> runProperties = r.ChildElements.Where(c => c is RunProperties).Cast<RunProperties>();
                        Console.WriteLine("Getting Run Data.");
                    }
                    IEnumerable<ParagraphProperties> paragraphPropertiesList = p.ChildElements.Where(r => r is ParagraphProperties).Cast<ParagraphProperties>();
                }
            }
            Console.WriteLine("All done. Press a key.");
        }
XmlDocument doc = new XmlDocument();
doc.LoadXml(myDoc.MainDocumentPart.Document.OuterXml);
var json = JsonConvert.SerializeXmlNode(doc);
IEnumerable<Paragraph> paragraphList = myDoc.MainDocumentPart.Document.Body.Elements().OfType<Paragraph>();
var proj = paragraphList.Select(p => p.ChildElements.OfType<Run>().Select(r => new
{
    r.InnerText,
    runProperties = r.ChildElements.OfType<RunProperties>().FirstOrDefault()?.Select(rp => new { rp.GetType().Name, Val = rp.GetAttributes().FirstOrDefault().Value })
}));
var json = JsonConvert.SerializeObject(proj, Newtonsoft.Json.Formatting.Indented);