Package Explorer 3.0 is nearing completion. During the addition of my latest feature I needed to validate various identifiers found in Open XML packages. Just thought I'd write up the ways I validate these identifiers.
Note that the following code only looks at the data-type, and not whether a specific conformant value is actually allowed semantically.
Relationship IDs
The valid values for a relationship ID is defined by the XML specification. Hence you can easily use the XML support in .NET to validate a relationship ID.
- Must not be empty (String.Empty)
- Must not already be in use
- Must conform to the XML Name data type
Now the first two demands are easy to meet. Package Explorer validates this all and prevents you from entering an existing relationship ID by querying the package. The last demand can be satisfied using the following code:
try
{
XmlConvert.VerifyName(myRelationshipID);
}
catch (XmlException)
{
}
Content Types
Content-Types are far more difficult to validate than a relationship ID, since a content-type is allowed to contain parameter values. It's basically a mime-type. There is also adequate support for this in the .NET Framework.
- Must not be empty
- Must not contain leading or trailing whitespace
- Must conform to RFC 2616 (HTML) paragraph 3.7
Again mostly easy. I was a little daunted by the last demand. If you read the HTML specification you'll notice that a content-type takes more parsing to validate. Luckily the .NET Framework already provides in this with the ContentType class found in the System.Net.Mime namespace. You can validate a content-type as follows:
try
{
new
ContentType(myContentType);
}
catch (FormatException)
{
}
Relationship Types
A relationship type is basically a URI, no more, no less.
Uri.IsWellFormedUriString(uri, UriKind.RelativeOrAbsolute);
Part Locations
The last important bit was the location of a part in the package. This is also a URI, and as such these paths are best validated using the Uri class. If you decide to use the Path class take note of the fact that a Uri uses the / slash and a path the \ slash character.
Hope it helps.