Visio 2013 File Formats
Visualize your ideas with Visio 2013 Visio makes it easier to create custom diagrams using built-in templates and shapes. Explore features, resources, and support options for Visio 2013. Get helpful tips for organizing your ideas and data into versatile flowcharts and diagrams. Are you ready to take data visualization to the next level?
With Visio Pro for Office 365, you can take advantage of all the features in Visio Professional 2016 on up to five PCs per user. You get regular security and feature updates, integration with other Office 365 services, and more.
Previous versions of Visio saved files in a proprietary binary file format (.vsd) or a single-document Visio XML Drawing file format (.vdx). Visio 2013 introduces a new file format (.vsdx), which is based on XML and ZIP archive technologies.
Just as in previous versions of Visio, files are saved in a single container. Unlike legacy files, however, the new file format can be opened, read, updated, changed, and constructed without automating the Visio 2013 application.
Developers who are familiar with manipulating XML or working with the namespace can quickly get started working with the new file format programmatically. Developers who have worked with the Visio XML Drawing format from previous versions can find that many of the structures from that format have been retained in the new file format. In this article, we examine how to work with the Visio 2013 file format programmatically, using the Microsoft.NET Framework 4.5, C# or Visual Basic, and Visual Studio 2012. You can see how to open a Visio 2013 file, select document parts within the file, change data in parts, and create a new document part. Note The code samples in this article assume that you have a rudimentary understanding of the classes in the and namespaces. This article also assumes that you understand the concepts and terminology of the Open Packaging Conventions.
Nov 13, 2012 Microsoft Visio 2013 Viewer allows anyone to view Visio drawings inside their. Visio users can freely distribute Visio drawings (files with a.vsdx. Microsoft Visio 2013 Viewer allows anyone to view Visio drawings inside their Microsoft. Visio users can freely distribute Visio drawings (files.
You should have some familiarity with the concepts of packages, document parts or package parts, and relationships. For more information, see. The code demonstrates how to create LINQ (Language-Integrated Query) queries to select XML. Most of the code samples use the query syntax for building LINQ queries. You can rewrite any of the LINQ queries provided in the code by using the LINQ method syntax, if necessary. For more information about LINQ query syntax and method syntax, see Table 1 shows the essential topics that you should be familiar with before you work through this article.
Visio File Sample
![Visio 2013 File Formats Visio 2013 File Formats](https://image.slidesharecdn.com/microsoftvisio2013-150224074439-conversion-gate01/95/microsoft-visio-2013-23-638.jpg?cb=1424764455)
Article title Description This high-level overview describes some of the major features of the Visio 2013 file format. It discusses the Open Packaging Conventions (OPC) as they have been applied to the Visio 2013 file format. It also lists some differences between the Visio 2013 file format and the previous Visio XML Drawing file format (.vdx). This MSDN Magazine article describes the Open Packaging Conventions as a concept.
These two articles discuss how the Open Packaging Conventions have been applied to Microsoft Office files. They contain descriptions of how relationships work in a package and also include some code examples.
Open Visio 2013. Create a new document based on the Basic Flowchart template by choosing CATEGORIES, Flowchart, Basic Flowchart, Create. From the Shapes window, drag a Start/End shape onto the canvas. Select the new Start/End shape on the drawing canvas and type ' Begin Process'. From the Shapes window, drag a Process shape onto the canvas.
Select the new Process shape on the drawing canvas and type ' Perform some task'. On the shortcut menu for the Start/End shape, select Add One Connector to the Page, and then draw a connector between the Start/End and Process shapes on the canvas, as shown in Figure 1. Simple Visio 2013 drawing.
Save the file to your Desktop as a.vsdx file by choosing File, Save As, Computer, Desktop. In the Save As dialog box, enter Visio Package in the File name box', select Visio Drawing (.vsdx) in the Save as type list, and then choose the Save button. Close the file and then close Visio 2013. Tip Sometimes Visio opens a file successfully even if there are issues with the file.
To ensure that Visio notifies you of any file issues, you should enable file open warnings when testing solutions that manipulate Visio files at the file package level. To enable file open warnings, in Visio 2013, choose File, Options, Advanced. Under Save/Open, select Show file open warnings. These procedures use a Windows console application to manipulate the 'Visio Package.vsdx' file. Use the following procedure to create and set up a new Windows console application in Visual Studio 2012. To create a new solution in Visual Studio 2012.
On the File menu, choose New, Project. In the New Project dialog box, expand either Visual C# or Visual Basic, and then choose Windows, Console Application. In the Name box, enter ' VisioFileAccessor', select a location for the project, and then choose the OK button. On the Project menu, choose Add Reference. In the Reference Manager dialog box, under Assemblies, choose Framework, and then add a reference to the System.Xml and WindowsBase components. In the Program.cs or Module1.vb file for the project, add the following using directives ( Imports statements in Visual Basic). Tip As a best practice, use a using block to open the Visio file in the Package object so that you don't have to explicitly close the file package when you're done with it.
You can also explicitly call the Package.Close method in the finally block of a try/catch/finally construction. Use the following code to get the full path for the 'Visio Package.vsdx' file by using a object, pass the path as an argument to the Package.Open method, and then return a Package object to the calling code. To open a.vsdx file as a package. Once you have the Visio 2013 file open as a package, you can access the document parts within it using the class included in the System.IO.Packaging namespace. PackagePart objects can be instantiated individually or as a collection.
The Package class exposes a method and a method for getting PackagePart objects out of the Package. The Package.GetParts method returns an instance of the class, which you can then interact with like any other collection that implements the interface. Use the code in the following procedure to get a PackagePartCollection object from the Package as a collection, iterate through the PackagePart objects in the collection, and write the URI and content type of each PackagePart to the console.
To iterate through the package parts in a package. // Write the URI and content type of each package part to the console. IteratePackageParts(visioPackage);. Choose the F5 key to debug the solution. When the program has completed running, choose any key to exit.
Note You can also get a reference to a specific PackagePart by using just the Package.GetPart method and the URI of the PackagePart, bypassing the step where you get the package part's relationships. However, some package parts in the Visio file package can be saved to locations other than their default locations in a package. You cannot assume that a package part is always located at the same URI for every file. Instead, it is a best practice to use relationships to access individual PackagePart objects.
Use the following procedure to get a PackagePart (the Visio Document part) by using the PackageRelationship from the Package that references the part. To select a specific package part in the package by relationship. // Get a reference to the Visio Document part contained in the file package. PackagePart documentPart = GetPackagePart(visioPackage, 'As mentioned previously, you can also get PackagePart objects by using their relationship to other PackagePart objects. This is important because, for a Visio document of any complexity, most of the PackagePart objects don't have a relationship to the Package. For example, an individual Page Content part in the file package (that is, /visio/pages/page1.xml) has a relationship to the Page Index part (that is, /visio/pages/pages.xml) but not to the file package itself.
If you don't have the exact URI of the individual page in the package, you can use its relationship to the Page Index part to get a reference to it. The PackagePart class exposes a method that you can use to return a PackageRelationshipCollection object that contains only one type of PackageRelationship object. Once you have the PackageRelationshipCollection, you can select the PackageRelationship that you need from the collection and then reference the PackagePart object. Use the following code to get a PackagePart from the Package by using its relationship to (getting a PackageRelationship object from) another PackagePart. To select a specific package part through its relationship to another package part.
// Get a reference to the collection of pages in the document, // and then to the first page in the document. PackagePart pagesPart = GetPackagePart(visioPackage, documentPart, 'PackagePart pagePart = GetPackagePart(visioPackage, pagesPart, 'Before you can make changes to the XML included in a document part, you need to first load the XML document into an object that allows you to read the XML, using the class or class. Both classes expose methods for tasks such as selecting XML elements contained within the XML documents; creating, reading, and writing attributes; and inserting new XML elements into a document.
Of the two, the XDocument class allows you to query the XML using LINQ. With LINQ, you can easily select individual elements from an XML document by creating queries, rather than iterating over all of the elements in a collection and testing for the elements that you need.
Visio 2013 File Format
For these reasons, the following procedures in this article use the XDocument class and other classes of the System.Xml.Linq namespace for working with XML. Use the following procedure to open a PackagePart as an XML document in an XDocument object.
To read the XML in a package part. Once you have loaded a document part into an XDocument object, you can use LINQ to select XML elements and make changes to the XML document. You can change XML data, add or remove data, and then save the XML document back to the document part. The most common task for manipulating the Visio file format is selecting specific XML elements or collections of elements in the document.
The System.Xml.Linq namespace includes the class, which represents an XML element. The XElement class gives you access to the data contained in the Visio file at a granular level, from individual Shape elements to ValidationRule elements (as examples). Use the following code to select the Shape elements from an XDocument (containing a Page Contents part) and to then select a specific Shape element.
To select a specific element in a package part. // Get all of the shapes from the page by getting // all of the Shape elements from the pageXML document. IEnumerable shapesXML = GetXElementsByName(pageXML, 'Shape'); // Select a Shape element from the shapes on the page by // its name. You can modify this code to select elements // by other attributes and their values. XElement startEndShapeXML = GetXElementByAttribute(shapesXML, 'NameU', 'Start/End'); Once you have gotten a reference to a XElement object contained within an XDocument object, you can manipulate it like any other XML data and, thereby, change the data contained in the Visio file. For example, if a shape has text when it is opened in Visio, the corresponding Shape element will contain at least one Text element. If you change the value of that Text element, the shape's text is changed when the file is viewed in Visio.
Add the following code to the using block in the Main method of the Program class (the Using block of the Main method in Module1 in Visual Basic) to change the text in the Start/End shape from 'Begin process' to 'Start process'. Caution In the previous code example, the existing shape text and the string used to replace it have the same number of characters. Also note that the LINQ query changes the value of the last child node of the returned element (which, in this case, is a text node). This is done to avoid changing the settings of the cp element that is a child of the Text element. It is possible to cause file instability if you alter shape text programmatically by overwriting all children of the Text element.
As in the example above, the text formatting is represented by cp elements under the Text element in the file. The definition of the formatting is stored in the parent Section element. If these two pieces of information become inconsistent, then the file may not behave as expected. Visio heals many inconsistencies, but it is better to ensure that any programmatic changes are consistent so that you are controlling the ultimate behavior of the file. When you make changes to the XML of a document part, those changes exist in memory only.
To persist the changes in the file, you must save the XML back to the document part. The following code uses the class and class to write the XML back to the package part. Although you can use the method to save the XML back to the part, the XmlWriter and XmlWriterSettings classes allow you finer control over the output, including specifying the type of encoding. The XDocument class exposes a method that takes an XmlWriter object and writes XML back to a stream. Use the following procedure to save the XML from the Visio page back to the Page Contents part.
To save the changed XML back to the package. Some changes to the data in a file may require Visio to recalculate the document when it opens the file. Visio provides a lot of logic for a diagram, particularly for shape relationships (that is, when one shape depends on another) and connecting shapes.
If any of the data that the custom logic relies upon is changed, Visio needs to propagate the changes to all of the affected calculated data in the file. The Visio 2013 file format includes a couple of techniques that you can use to recalculate data in the file.
There are three types of scenarios that you must consider when you decide whether you need to recalculate the Visio file and how to do it:. The changes to the data do not affect any other values in the file format. You don't need to add any additional instructions to Visio to recalculate the document. As demonstrated previously, you can often change a shape's text without needing to recalculate the document.
The changes to the data are limited to changing the values of ShapeSheet cells in the XML, and there are other ShapeSheet values that depend on this data. In this case, you must add an XML processing instruction (using the class) to the Cell element that has been changed. For example, the ThemeIndex cell for a shape affects the values of several other ShapeSheet cells contained in the shape. If you change the ThemeIndex cell within the file itself (for example, the Cell element with an N value of 'ThemeIndex'), you will need to add a processing instruction to the Cell element so that the dependent values are updated.
The changes to the data affect the location of a connector or connection points. Another situation is when there are many changes to the ShapeSheet data and you want to recalculate the whole document with one instruction (rather than adding individual processing instructions for each change). In this case you can instruct Visio to recalculate the entire document when it is opened. You do this by adding a RecalcDocument property to the Custom File Properties part (/docProps/custom.xml) of the Visio package.
Adjusting the position or size of shapes in a connected diagram is an example of this type of scenario. Keep in mind that this is the most costly option from a performance standpoint. Use the following procedure to insert a Cell element into a Shape element, where other Cell elements in the same Shape need to be recalculated because of the new value. The new Cell element includes a processing instruction as a child element, to inform Visio that it needs to perform some local recalculation. To recalculate values for a single shape.
// Insert a new Cell element in the Start/End shape that adds an arbitrary // local ThemeIndex value. This code assumes that the shape does not // already have a local ThemeIndex cell. StartEndShapeXML.Add( new XElement( 'Cell', new XAttribute( 'N', 'ThemeIndex'), new XAttribute( 'V', '25'), new XProcessingInstruction( 'NewValue', 'V'))); // Save the XML back to the Page Contents part. SaveXDocumentToPart(pagePart, pageXML);. Choose the F5 key to debug the solution.
When the program has completed running, choose any key to exit. Open the Visio Package.vsdx file in Visio 2013.
The Start/End shape should now have a different fill color. The shape's color relies on the value of the ThemeIndex cell—it determines which of the active themes the shape inherits from.
In the previous example, the shape is set to inherit from a different theme (the ThemeIndex cell is set to a value of '25'). If you don't use a processing instruction, the shape's text color—which is also affected by the ThemeIndex cell—isn't recalculated. The shape's fill color would change to white, but its text would remain white—leaving the text unreadable. Also, without the processing instruction, it is possible that Visio may update the shape at a later date, leaving the file in an unstable state where the formatting values of the shape could be updated unpredictably. If you change data in the file that requires Visio to recalculate the document (for example, changing a connected shape's position and, therefore, forcing the connectors to reroute), you must add a recalculation instruction to the Visio file. The instruction is created by adding a property element with a name attribute value of 'RecalcDocument' to the XML of the Custom File Properties part of the Visio file package.
As a best practice, you should check the Custom File Properties part to be sure that a 'RecalcDocument' instruction hasn't already been registered in the file. Use the following code to change the value of the PinY cell of the Start/End shape from the previous examples. The code selects the Cell element that contains the PinY cell data as an XElement object by using the value of its N attribute. The code then adds a recalculation instruction to the Custom File Properties part of the Visio file. // Change the shape's horizontal position on the page // by getting a reference to the Cell element for the PinY // ShapeSheet cell and changing the value of its V attribute. XElement pinYCellXML = GetXElementByAttribute( startEndShapeXML.Elements, 'N', 'PinY'); pinYCellXML.SetAttributeValue( 'V', '2'); // Add instructions to Visio to recalculate the entire document // when it is next opened. RecalcDocument(visioPackage); // Save the XML back to the Page Contents part.
SaveXDocumentToPart(pagePart, pageXML);. Choose the F5 key to debug the solution. When the program has completed running, choose any key to exit. Open the Visio Package.vsdx file in Visio 2013.
The Start/End shape should now be 2 inches from the bottom edge of the drawing. The connector between the Start/End shape and the Process shape should have rerouted to accommodate the change in layout. If the RecalcDocument property had not been added to the file, the shape position would have been changed, but the connector would not have rerouted to the new location of the shape. One of the most common scenarios for modifying a file package is adding a new document part to the package. For example, if you want to add a page to the Visio drawing by adding content to the package, you need to add a Page Contents part to the package. The process for adding a new part to the package is straightforward:. You create the XML document with the data for the PackagePart.
You need to pay special attention to the XML namespaces that govern the schema for the specific type of XML document that you create. You create a new file to contain the XML and save the file to a location in the Package. You create the necessary relationships between the new PackagePart and the Package or other PackagePart objects. You update any existing parts that need to reference the new part. For example, if you add a new Page Contents part (a new page) to the file, you also need to update the Page Index part (/visio/pages/pages.xml file) to include the correct information about the new page. Use the following procedure to create a new Ribbon Extensibility part in the Visio file.
The new Ribbon Extensibility part adds to the ribbon a new tab with a single group that contains a single button. To create a new package part. // Create a new Ribbon Extensibility part and add it to the file. XDocument customUIXML = CreateCustomUI; CreateNewPackagePart(visioPackage, customUIXML, new Uri( '/customUI/customUI1.xml', UriKind.Relative), 'application/xml', '.
Choose the F5 key to debug the solution. When the program has completed running, choose any key to exit. Open the Visio Package.vsdx file in Visio 2013, and then choose the CUSTOM tab.
The custom ribbon looks like Figure 2 when the file is opened in Visio 2013. Custom tab in the Visio 2013 ribbon The XML created by the CreateCustomUI method looks like the following. We would like to recognize the contribution and input of Visio MVP Al Edlund in creating the code examples contained in this technical article. Al is a recognized expert in manipulating the Visio file format, including both the Visio XML Drawing format (.vdx) and the new Visio file format (.vsdx). Al has created projects that explore the Visio file formats programmatically and exposes the structures inside.
For more information about Al's work with the Visio file format, see the links in the Additional Resources section that follows.