XObject.BaseUri 屬性

定義

取得此 XObject 的基底 URI。Gets the base URI for this XObject.

public:
 property System::String ^ BaseUri { System::String ^ get(); };
public string BaseUri { get; }
member this.BaseUri : string
Public ReadOnly Property BaseUri As String

屬性值

String

String,包含這個 XObject 的基底 URI。A String that contains the base URI for this XObject.

範例

下列範例會在載入檔案時載入基底 URI 和行資訊。The following example loads the base URI and line information as it loads the file. 然後,它會列印基底 URI 和行資訊。It then prints the base URI and the line information.

此範例使用下列 XML 文件︰範例 XML 檔:典型採購訂單 (LINQ to XML)This example uses the following XML document: Sample XML File: Typical Purchase Order (LINQ to XML).

XElement po = XElement.Load("PurchaseOrder.xml",  
    LoadOptions.SetBaseUri | LoadOptions.SetLineInfo);  
string[] splitUri = po.BaseUri.Split('/');  
Console.WriteLine("BaseUri: {0}", splitUri[splitUri.Length - 1]);  
Console.WriteLine();  
Console.WriteLine("{0}{1}{2}",  
    "Element Name".PadRight(20),  
    "Line".PadRight(5),  
    "Position");  
Console.WriteLine("{0}{1}{2}",  
    "------------".PadRight(20),  
    "----".PadRight(5),  
    "--------");  
foreach (XElement e in po.DescendantsAndSelf())  
    Console.WriteLine("{0}{1}{2}",  
        ("".PadRight(e.Ancestors().Count() * 2) + e.Name).PadRight(20),  
        ((IXmlLineInfo)e).LineNumber.ToString().PadRight(5),  
        ((IXmlLineInfo)e).LinePosition);  
Dim po As XElement = XElement.Load("PurchaseOrder.xml", LoadOptions.SetBaseUri Or LoadOptions.SetLineInfo)  
Dim splitUri() As String = po.BaseUri.Split("/"c)  
Console.WriteLine("BaseUri: {0}", splitUri(splitUri.Length - 1))  
Console.WriteLine()  
Console.WriteLine("{0}{1}{2}", _  
    "Element Name".PadRight(20), _  
    "Line".PadRight(5), _  
    "Position")  
Console.WriteLine("{0}{1}{2}", _  
    "------------".PadRight(20), _  
    "----".PadRight(5), _  
    "--------")  
For Each e As XElement In po.DescendantsAndSelf()  
    Console.WriteLine("{0}{1}{2}", _  
        ("".PadRight(e.Ancestors().Count() * 2) & e.Name.ToString).PadRight(20), _  
        DirectCast(e, IXmlLineInfo).LineNumber.ToString().PadRight(5), _  
        DirectCast(e, IXmlLineInfo).LinePosition)  
Next  

這個範例會產生下列輸出:This example produces the following output:

BaseUri: PurchaseOrder.xml  

Element Name        Line Position  
------------        ---- --------  
PurchaseOrder       2    2  
  Address           3    4  
    Name            4    6  
    Street          5    6  
    City            6    6  
    State           7    6  
    Zip             8    6  
    Country         9    6  
  Address           11   4  
    Name            12   6  
    Street          13   6  
    City            14   6  
    State           15   6  
    Zip             16   6  
    Country         17   6  
  DeliveryNotes     19   4  
  Items             20   4  
    Item            21   6  
      ProductName   22   8  
      Quantity      23   8  
      USPrice       24   8  
      Comment       25   8  
    Item            27   6  
      ProductName   28   8  
      Quantity      29   8  
      USPrice       30   8  
      ShipDate      31   8  

備註

您可以使用 LINQ to XML,在許多 fashions 中還原序列化 XML。Using LINQ to XML, you can deserialize XML in a number of fashions. 您可以從字串剖析、從檔案載入,或從 TextReader 或讀取 XmlReaderYou can parse it from a string, load it from a file, or read it from a TextReader or an XmlReader. 在上述所有情況下,LINQ to XML 會使用的一或多個具體子類別 XmlReaderIn all of these cases, LINQ to XML uses one or another of the concrete subclasses of XmlReader.

有時候 XmlReader 會有基底 URI,有時則不會。Sometimes the XmlReader has the base URI, and sometimes it does not. 比方說,從檔案載入時, XmlReader 會知道基底 uri,但是從 XmlReader 因為呼叫方法而建立的來讀取時 Parse ,並不可能有 XmlReader 報告基底 uri; XML 是在字串中。For instance, when loading from a file, the XmlReader knows the base URI, but when reading from an XmlReader that was created because of calling the Parse method, there is no possibility of the XmlReader reporting a base URI; the XML was in a string.

當剖析或載入 XML 時,如果您指定 SetBaseUri ,LINQ to XML 將會要求每個節點的基底 URI,因為會傳回 XmlReader 節點。If, when parsing or loading the XML, you specify SetBaseUri, LINQ to XML will request the base URI for each node as the XmlReader returns the node. 如果讀取器具有基底 URI,LINQ to XML 會將資訊儲存在 LINQ to XML 節點中。If the reader has the base URI, LINQ to XML will save the information with the LINQ to XML node. 這個屬性會傳回該資訊。This property returns that information. 如果基礎沒有 XmlReader 基底 URI,則這個屬性會傳回空字串。If the underlying XmlReader does not have the base URI, then this property will return an empty string.

設定 SetBaseUri 載入 XML 樹狀結構時,會導致剖析速度較慢。Setting SetBaseUri when loading an XML tree will result in slower parsing.

設定 XML 樹狀結構的基底 URI 時,LINQ to XML 會將批註放在樹狀結構的根。When setting the base URI for an XML tree, LINQ to XML puts an annotation on the root of the tree. 這個屬性是計算的屬性,而且會向上流覽樹狀結構以尋找基底 URI。This property is a computed property, and navigates up the tree to find the base URI.

適用於

另請參閱