CultureAndRegionInfoBuilder.CreateFromLdml(String) 方法

定義

從包含物件表示的指定 XML 檔案,重新組成 CultureAndRegionInfoBuilder 物件。

public:
 static System::Globalization::CultureAndRegionInfoBuilder ^ CreateFromLdml(System::String ^ xmlFileName);
public static System.Globalization.CultureAndRegionInfoBuilder CreateFromLdml (string xmlFileName);
static member CreateFromLdml : string -> System.Globalization.CultureAndRegionInfoBuilder
Public Shared Function CreateFromLdml (xmlFileName As String) As CultureAndRegionInfoBuilder

參數

xmlFileName
String

包含 CultureAndRegionInfoBuilder 物件之 XML 表示的檔案名稱。

傳回

新物件,相當於存放在 xmlFileName 參數中的資訊。

例外狀況

xmlFileNamenull

xmlFileName 是一個空字串 (""),或是無效的檔案或路徑名稱。

xmlFileName 中的資料不是有效的 XML 格式。

-或-

當存取 xmlFileName 時,發生 I/O 錯誤。

xmlFileName 中的資料不是有效的 LDML 格式。

範例

下列範例示範 SaveCreateFromLdml 方法。

// This example demonstrates the CultureAndRegionInfoBuilder.Save and
// CreateFromLdml methods.
// Compile this example with a reference to sysglobl.dll.

using System;
using System.Globalization;
using System.IO;
using System.Xml;

class Sample
{
    public static void Main()
    {
    string savedCARIB = "mySavedCARIB.xml";
    string msg1 = "The name of the original CultureAndRegionInfoBuilder" +
                  " is \"{0}\".";
    string msg2 = "Reconstituting the CultureAndRegionInfoBuilder object " +
                  "from \"{0}\".";
    string msg3 = "The name of the reconstituted CultureAndRegionInfoBuilder" +
                  " is \"{0}\".";

// Construct a new, privately used culture that extends the en-US culture
// provided by the .NET Framework. In this sample, the CultureAndRegion-
// Types.Specific parameter creates a minimal CultureAndRegionInfoBuilder
// object that you must populate with culture and region information.

    CultureAndRegionInfoBuilder cib1 = null;
    CultureAndRegionInfoBuilder cib2 = null;
    try {
        cib1 = new CultureAndRegionInfoBuilder(
                           "x-en-US-sample", CultureAndRegionModifiers.None);
        }
    catch (ArgumentException ae)
        {
        Console.WriteLine(ae);
        return;
        }

// Populate the new CultureAndRegionInfoBuilder object with culture information.
    CultureInfo ci = new CultureInfo("en-US");
    cib1.LoadDataFromCultureInfo(ci);

// Populate the new CultureAndRegionInfoBuilder object with region information.
    RegionInfo  ri = new RegionInfo("US");
    cib1.LoadDataFromRegionInfo(ri);

// Display a property of the new custom culture.
    Console.Clear();
    Console.WriteLine(msg1, cib1.CultureName);

// Save the new CultureAndRegionInfoBuilder object in the specified file in
// LDML format. The file is saved in the same directory as the application
// that calls the Save method.

    Console.WriteLine("Saving the custom culture to a file...");
    try {
        cib1.Save( savedCARIB );
        }
    catch (IOException exc)
        {
        Console.WriteLine("** I/O exception: {0}", exc.Message);
        return;
        }

// Create a new CultureAndRegionInfoBuilder object from the persisted file.
    Console.WriteLine(msg2, savedCARIB);
    try {
        cib2 = CultureAndRegionInfoBuilder.CreateFromLdml( savedCARIB );
        }
    catch (XmlException xe)
        {
        Console.WriteLine("** XML validation exception: {0}", xe.Message);
        return;
        }

// Display a property of the resonstituted custom culture.
    Console.WriteLine(msg3, cib2.CultureName);

// At this point you could call the Register method and make the reconstituted
// custom culture available to other applications. The mySavedCARIB.xml file
// remains on your computer.
    }
}

/*
This code example produces the following results:

The name of the original CultureAndRegionInfoBuilder is "x-en-US-sample".
Saving the custom culture to a file...
Reconstituting the CultureAndRegionInfoBuilder object from "mySavedCARIB.xml".
The name of the reconstituted CultureAndRegionInfoBuilder is "x-en-US-sample".

*/
' This example demonstrates the CultureAndRegionInfoBuilder.Save and 
' CreateFromLdml methods.
' Compile this example with a reference to sysglobl.dll.

Imports System.Globalization
Imports System.IO
Imports System.Xml

Class Sample
    Public Shared Sub Main() 
        Dim savedCARIB As String = "mySavedCARIB.xml"
        Dim msg1 As String = "The name of the original CultureAndRegionInfoBuilder" & _
                             " is ""{0}""."
        Dim msg2 As String = "Reconstituting the CultureAndRegionInfoBuilder object " & _
                             "from ""{0}""."
        Dim msg3 As String = "The name of the reconstituted CultureAndRegionInfoBuilder" & _
                             " is ""{0}""."

        ' Construct a new, privately used culture that extends the en-US culture 
        ' provided by the .NET Framework. In this sample, the CultureAndRegion-
        ' Types.Specific parameter creates a minimal CultureAndRegionInfoBuilder 
        ' object that you must populate with culture and region information.

        Dim cib1 As CultureAndRegionInfoBuilder = Nothing
        Dim cib2 As CultureAndRegionInfoBuilder = Nothing
        
        Try
            cib1 = New CultureAndRegionInfoBuilder("x-en-US-sample", _
                                        CultureAndRegionModifiers.None)
        Catch ae As ArgumentException
            Console.WriteLine(ae)
            Return
        End Try
        
        ' Populate the new CultureAndRegionInfoBuilder object with culture information.
        Dim ci As New CultureInfo("en-US")
        cib1.LoadDataFromCultureInfo(ci)
        
        ' Populate the new CultureAndRegionInfoBuilder object with region information.
        Dim ri As New RegionInfo("US")
        cib1.LoadDataFromRegionInfo(ri)
        
        ' Display a property of the new custom culture.
        Console.Clear()
        Console.WriteLine(msg1, cib1.CultureName)
        
        ' Save the new CultureAndRegionInfoBuilder object in the specified file in
        ' LDML format. The file is saved in the same directory as the application 
        ' that calls the Save method.

        Console.WriteLine("Saving the custom culture to a file...")
        Try
            cib1.Save(savedCARIB)
        Catch exc As IOException
            Console.WriteLine("** I/O exception: {0}", exc.Message)
            Return
        End Try
        
        ' Create a new CultureAndRegionInfoBuilder object from the persisted file.
        Console.WriteLine(msg2, savedCARIB)
        Try
            cib2 = CultureAndRegionInfoBuilder.CreateFromLdml(savedCARIB)
        Catch xe As XmlException
            Console.WriteLine("** XML validation exception: {0}", xe.Message)
            Return
        End Try
        
        ' Display a property of the resonstituted custom culture.
        Console.WriteLine(msg3, cib2.CultureName)

        ' At this point you could call the Register method and make the reconstituted
        ' custom culture available to other applications. The mySavedCARIB.xml file
        ' remains on your computer.

    End Sub
End Class

'This code example produces the following results:
'
'The name of the original CultureAndRegionInfoBuilder is "x-en-US-sample".
'Saving the custom culture to a file...
'Reconstituting the CultureAndRegionInfoBuilder object from "mySavedCARIB.xml".
'The name of the reconstituted CultureAndRegionInfoBuilder is "x-en-US-sample".
'

備註

參數 xmlFileName 會指定檔案名,其中包含 物件的 XML 表示 CultureAndRegionInfoBuilder 。 XML 檔案的格式稱為地區設定資料標記語言 (LDML) 1.1 版。 系統會使用 中的資料 xmlFileName 建立並初始化新的 CultureAndRegionInfoBuilder 物件。 方法 Save 會執行 方法的 CreateFromLdml 反向作業。

在 LDML 檔案中,文化特性的屬性會指定為 區段中元素的 <special><identity> 子項目。 屬性值通常是由元素的 type 屬性指定。 例如,下列來自 LDML 檔案的摘錄會將文化特性的父系定義為英文中性文化特性。

<identity>
   <version number="1.1">ldml version 1.1</version>
   <generation date="2012-05-16" />
   <special xmlns:msLocale="http://schemas.microsoft.com/globalization/2004/08/carib/ldml">
      <!-content removed -->
      <msLocale:parentName type="en" />
      <!-content removed -->
   </special>
</identity>

如需 LDML 標準的詳細資訊,請參閱 Unicode 聯盟網站上的 Unicode 技術標準 #35「地區設定資料標記語言 (LDML) 」

適用於

另請參閱