When an Office application opens an Office document, it starts with a checksum, primarily verifying the validity and integrity of the document.
When you arbitrarily change the suffix of an EXE document to docx, opening the file will definitely invalidate the file. For an Open XML document, if you want to verify it, there are several aspects:
Q is not a zip compression package
Q Does the necessary part exist
Q is the relationship complete
Q Does the part type match
Q File Size
Q Macros and OLE objects
There are a lot of ways to verify this, and here's the simplest way to use the Openxmlvalidator object in the Open XML SDK. Code Listing 14-24 is a way to verify the validity of a Word document.
Code Listing 14-24 Verifying document validity
public static void Validateworddocument (string filepath) {try {openxmlvalidator Valida
Tor = new Openxmlvalidator ();
int count = 0; foreach (Validationerrorinfo error in validator.)
Validate (Wordprocessingdocument.open (filepath, True)) {count++;
Console.WriteLine ("Error" + count); Console.WriteLine ("Description:" + error.)
Description); Console.WriteLine ("ErrorType:" + error.)
ErrorType); Console.WriteLine ("Node:" + error.)
Node); Console.WriteLine ("Path:" + error.)
Path.xpath); Console.WriteLine ("Part:" + error.)
Part.uri);
Console.WriteLine ("-------------------------------------------");
} console.readkey (); The catch (Exception ex) {Console.WriteLine (ex).
message); }
}
In Listing 14-24, it's easy to verify a document by simply using the Openxmlvalidator Validate method. The method needs to provide a Wordprocessingdocument object as a parameter and return a collection of Validationerrorinfo objects. The Validationerrorinfo object provides us with the basic description, type, node, path, and part of the error that occurred.
--------------Note: This part of the text is adapted from the ". NET Security Secrets"
Author: Hyun-Soul
Source: http://www.cnblogs.com/xuanhun/
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/web/XML/