CachedDataItem.Xml 속성

CachedDataItem으로 표시되는 캐시된 데이터 개체의 XML 표현을 가져오거나 설정합니다.

네임스페이스:  Microsoft.VisualStudio.Tools.Applications
어셈블리:  Microsoft.VisualStudio.Tools.Applications.ServerDocument(Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll)

구문

‘선언
Public Property Xml As String
    Get
    Set
public string Xml { get; set; }

속성 값

형식: System.String
CachedDataItem 으로 표시되는 캐시된 데이터 개체의 XML 표현입니다.

설명

캐시된 데이터 개체의 값을 가져오려면 Xml 속성을 사용하여, 캐시된 데이터의 XML 표현을 캐시된 데이터 개체의 새 인스턴스로 deserialize합니다. 그런 다음 이 복사본을 변경하고 변경 내용을 데이터 캐시로 다시 serialize합니다.

대부분의 경우 SerializeDataInstance 메서드를 사용하여 변경된 개체를 데이터 캐시로 serialize할 수 있습니다. 고유한 방식으로 변경 사항을 캐시된 데이터로 serialize하려는 경우 Xml 속성에 직접 쓸 수도 있습니다. 하지만 DataSet 또는 DataTable을 변경하거나 DataAdapter를 사용하여 데이터베이스에 업데이트될 형식화된 데이터 집합을 변경하는 경우에는 캐시된 데이터에 변경 사항을 쓸 때 DiffGram 형식을 지정합니다. 그렇지 않으면 DataSet 또는 DataTable의 변경 사항이 데이터베이스에 변경된 행이 아니라 새 행으로 추가됩니다. 자세한 내용은 서버에 있는 문서의 데이터 액세스을 참조하십시오.

예제

다음 코드 예제에서는 Xml 속성을 사용하여 Excel 통합 문서의 워크시트에 캐시된 문자열의 값을 가져옵니다. 이 예제에서는 값을 메시지 상자에 표시합니다.

이 예제에는 다음 사항이 필요합니다.

  • ExcelWorkbook1 네임스페이스에서 Sheet1 클래스가 있는 Excel의 문서 수준 사용자 지정 및 CachedString라는 Sheet1 클래스의 캐시된 문자열입니다.

  • 콘솔 응용 프로그램 프로젝트 또는 다른 비 Office 프로젝트입니다.

  • 다음 어셈블리에 대한 참조:

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 및 Microsoft.VisualStudio.Tools.Applications.Runtime.dll(.NET Framework 4를 대상으로 하는 프로젝트의 경우)

      또는

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll 및 Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll(.NET Framework 3.5를 대상으로 하는 프로젝트의 경우)

  • 코드 파일 상단에 있는 Microsoft.VisualStudio.Tools.ApplicationsMicrosoft.VisualStudio.Tools.Applications.Runtime 네임스페이스에 대해 Imports(Visual Basic의 경우) 또는 using(C#의 경우) 문.

Private Sub ReadCachedStringValue(ByVal documentPath As String)
    Dim runtimeVersion As Integer = 0
    Dim serverDocument1 As ServerDocument = Nothing

    Try
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath)
        If runtimeVersion <> 3 Then
            MessageBox.Show("This document does not have a Visual Studio Tools for Office " & _
                "customization, or it has a customization that was created with a version of " & _
                "the runtime that is incompatible with this version of the ServerDocument class.")
            Return
        End If

        If ServerDocument.IsCacheEnabled(documentPath) Then
            serverDocument1 = New ServerDocument(documentPath)
            Dim hostItem1 As CachedDataHostItem = _
                serverDocument1.CachedData.HostItems("ExcelWorkbook1.Sheet1")
            Dim dataItem1 As CachedDataItem = hostItem1.CachedData("CachedString")

            If dataItem1 IsNot Nothing AndAlso _
                Type.GetType(dataItem1.DataType).Equals(GetType(String)) Then

                Using stringReader As New System.IO.StringReader(dataItem1.Xml)
                    Dim serializer As New System.Xml.Serialization.XmlSerializer(GetType(String))
                    Dim cachedString As String = serializer.Deserialize(stringReader)
                    MessageBox.Show("The value of CachedString is: " + cachedString)
                End Using
            End If
        Else
            MessageBox.Show("The specified document does not have cached data.")
        End If

    Catch ex As System.IO.FileNotFoundException
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.")
    Catch ex As UnknownCustomizationFileException
        System.Windows.Forms.MessageBox.Show("The specified document has a file " & _
            "extension that is not supported by Visual Studio Tools for Office.")
    Finally
        If Not (serverDocument1 Is Nothing) Then
            serverDocument1.Close()
        End If
    End Try
End Sub
private void ReadCachedStringValue(string documentPath)
{
    int runtimeVersion = 0;
    ServerDocument serverDocument1 = null;

    try
    {
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);

        if (runtimeVersion != 3)
        {
            MessageBox.Show("This document does not have a Visual Studio Tools for " +
                "Office customization, or it has a customization that was created with " +
                "a version of the runtime that is incompatible with this version of the " +
                "ServerDocument class.");
            return;
        }

        if (ServerDocument.IsCacheEnabled(documentPath))
        {
            serverDocument1 = new ServerDocument(documentPath);
            CachedDataHostItem hostItem1 = 
                serverDocument1.CachedData.HostItems["ExcelWorkbook1.Sheet1"];
            CachedDataItem dataItem1 = hostItem1.CachedData["CachedString"];

            if (dataItem1 != null && 
                Type.GetType(dataItem1.DataType) == typeof(string))
            {
                using (System.IO.StringReader stringReader =
                    new System.IO.StringReader(dataItem1.Xml))
                {
                    System.Xml.Serialization.XmlSerializer serializer =
                        new System.Xml.Serialization.XmlSerializer(typeof(string));
                    string cachedString = serializer.Deserialize(stringReader) as string;
                    MessageBox.Show("The value of CachedString is: " + cachedString);
                }
            }
        }
        else
        {
            MessageBox.Show("The specified document does not have cached data.");
        }
    }
    catch (System.IO.FileNotFoundException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
    }
    catch (UnknownCustomizationFileException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document has a file " +
            "extension that is not supported by Visual Studio Tools for Office.");
    }
    finally
    {
        if (serverDocument1 != null)
            serverDocument1.Close();
    }
}

Xml 속성을 사용하여 수정한 다음 변경 내용을 캐시된 DataSet에 serialize하는 방법을 설명하는 코드 예제를 보려면 방법: 서버에 있는 통합 문서에서 캐시된 데이터 변경을 참조하십시오.

.NET Framework 보안

  • 직접 실행 호출자의 경우 완전히 신뢰합니다. 이 멤버는 부분적으로 신뢰할 수 있는 코드에서 사용할 수 없습니다. 자세한 내용은 부분 신뢰 코드에서 라이브러리 사용을 참조하십시오.

참고 항목

참조

CachedDataItem 클래스

Microsoft.VisualStudio.Tools.Applications 네임스페이스