Megosztás a következőn keresztül:


How to: Add custom XML parts to document-level customizations

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

You can store XML data in a Microsoft Office Excel workbook or Microsoft Office Word document by creating a custom XML part in a document-level customization. For more information, see Custom XML parts overview.

Applies to: The information in this topic applies to document-level projects for Excel and Word. For more information, see Features available by Office application and project type.

Note

Visual Studio does not provide document-level projects for Microsoft Office PowerPoint. For information about adding a custom XML part to a PowerPoint presentation by using a VSTO Add-in, see How to: Add custom XML parts to documents by using VSTO Add-ins.

To add a custom XML part to an Excel workbook

  1. Add a new CustomXMLPart object to the CustomXMLParts collection in the workbook. The CustomXMLPart contains the XML string that you want to store in the workbook.

    private void AddCustomXmlPartToWorkbook()
    {
        string xmlString =
            "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
            "<employees xmlns=\"http://schemas.microsoft.com/vsto/samples\">" +
                "<employee>" +
                    "<name>Karina Leal</name>" +
                    "<hireDate>1999-04-01</hireDate>" +
                    "<title>Manager</title>" +
                "</employee>" +
            "</employees>";
    
        Office.CustomXMLPart employeeXMLPart = this.CustomXMLParts.Add(xmlString, missing);
    }
    
    Private Sub AddCustomXmlPartToWorkbook()
        Dim xmlString As String = _
            "<?xml version=""1.0"" encoding=""utf-8"" ?>" & _
                "<employees xmlns=""http://schemas.microsoft.com/vsto/samples"">" & _
                    "<employee>" & _
                        "<name>Karina Leal</name>" & _
                        "<hireDate>1999-04-01</hireDate>" & _
                        "<title>Manager</title>" & _
                    "</employee>" & _
                "</employees>"
        Dim employeeXMLPart As Office.CustomXMLPart = Me.CustomXMLParts.Add(xmlString)
    End Sub
    
  2. Add the AddCustomXmlPartToWorkbook method to the ThisWorkbook class in a document-level project for Excel.

  3. Call the method from other code in your project. For example, to create the custom XML part when the user opens the workbook, call the method from the ThisWorkbook_Startup event handler.

To add a custom XML part to a Word document

  1. Add a new CustomXMLPart object to the CustomXMLParts collection in the document. The CustomXMLPart contains the XML string that you want to store in the document.

    Private Sub AddCustomXmlPartToDocument()
        Dim xmlString As String = _
            "<?xml version=""1.0"" encoding=""utf-8"" ?>" & _
                "<employees xmlns=""http://schemas.microsoft.com/vsto/samples"">" & _
                    "<employee>" & _
                        "<name>Karina Leal</name>" & _
                        "<hireDate>1999-04-01</hireDate>" & _
                        "<title>Manager</title>" & _
                    "</employee>" & _
                "</employees>"
    
        Dim employeeXMLPart As Office.CustomXMLPart = _
            Me.CustomXMLParts.Add(xmlString)
    End Sub
    
    private void AddCustomXmlPartToDocument()
    {
        string xmlString =
            "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
            "<employees xmlns=\"http://schemas.microsoft.com/vsto/samples\">" +
                "<employee>" +
                    "<name>Karina Leal</name>" +
                    "<hireDate>1999-04-01</hireDate>" +
                    "<title>Manager</title>" +
                "</employee>" +
            "</employees>";
    
        Office.CustomXMLPart employeeXMLPart = this.CustomXMLParts.Add(xmlString, missing);
    }
    
  2. Add the AddCustomXmlPartToDocument method to the ThisDocument class in a document-level project for Word.

  3. Call the method from other code in your project. For example, to create the custom XML part when the user opens the document, call the method from the ThisDocument_Startup event handler.

Robust programming

For simplicity, this example uses an XML string that is defined as a local variable in the method. Typically, you should obtain the XML from an external source, such as a file or a database.

See also