Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
In diesem Thema wird gezeigt, wie Sie die Klassen im Open XML SDK für Office verwenden, um ein Textverarbeitungsdokument programmgesteuert zu überprüfen.
Funktionsweise des Beispielcodes
Dieses Codebeispiel enthält zwei Methoden. Mit der ersten Methode, ValidateWordDocument, wird überprüft, ob es sich um eine reguläre Word-Datei handelt. Es löst keine Ausnahmen aus und schließt die Datei nach dem Ausführen der Überprüfung. Die zweite Methode, ValidateCorruptedWordDocument, beginnt mit dem Einfügen von Text in den Textkörper, wodurch ein Schemafehler verursacht wird. Dann wird die Word-Datei überprüft, wobei eine Ausgabe von der Methode ausgegeben wird, sobald versucht wird, die beschädigte Datei zu öffnen. Die Überprüfung erfolgt mithilfe der Validate -Methode. Vom Code werden Informationen zu allen gefundenen Fehlern angezeigt, darüber hinaus die Anzahl von Fehlern.
Wichtig
Beachten Sie, dass Sie den Code nicht zweimal ausführen können, nachdem Sie die Datei bei der ersten Ausführung beschädigt haben. Sie müssen mit einer neuen Word-Datei beginnen.
Es folgt der vollständige Beispielcode in C# und Visual Basic.
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Validation;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
static void ValidateWordDocument(string filepath)
{
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepath, true))
{
try
{
OpenXmlValidator validator = new OpenXmlValidator();
int count = 0;
foreach (ValidationErrorInfo error in
validator.Validate(wordprocessingDocument))
{
count++;
Console.WriteLine("Error " + count);
Console.WriteLine("Description: " + error.Description);
Console.WriteLine("ErrorType: " + error.ErrorType);
Console.WriteLine("Node: " + error.Node);
if (error.Path is not null)
{
Console.WriteLine("Path: " + error.Path.XPath);
}
if (error.Part is not null)
{
Console.WriteLine("Part: " + error.Part.Uri);
}
Console.WriteLine("-------------------------------------------");
}
Console.WriteLine("count={0}", count);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
static void ValidateCorruptedWordDocument(string filepath)
{
// Insert some text into the body, this would cause Schema Error
using (WordprocessingDocument wordprocessingDocument =
WordprocessingDocument.Open(filepath, true))
{
if (wordprocessingDocument.MainDocumentPart is null || wordprocessingDocument.MainDocumentPart.Document.Body is null)
{
throw new ArgumentNullException("MainDocumentPart and/or Body is null.");
}
// Insert some text into the body, this would cause Schema Error
Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
Run run = new Run(new Text("some text"));
body.Append(run);
try
{
OpenXmlValidator validator = new OpenXmlValidator();
int count = 0;
foreach (ValidationErrorInfo error in
validator.Validate(wordprocessingDocument))
{
count++;
Console.WriteLine("Error " + count);
Console.WriteLine("Description: " + error.Description);
Console.WriteLine("ErrorType: " + error.ErrorType);
Console.WriteLine("Node: " + error.Node);
if (error.Path is not null)
{
Console.WriteLine("Path: " + error.Path.XPath);
}
if (error.Part is not null)
{
Console.WriteLine("Part: " + error.Part.Uri);
}
Console.WriteLine("-------------------------------------------");
}
Console.WriteLine("count={0}", count);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}