Export (0) Print
Expand All

XObject.BaseUri Property

Gets the base URI for this XObject.

Namespace:  System.Xml.Linq
Assembly:  System.Xml.Linq (in System.Xml.Linq.dll)

public string BaseUri { get; }

Property Value

Type: System.String
A String that contains the base URI for this XObject.

Using LINQ to XML, you can deserialize XML in a number of fashions. You can parse it from a string, load it from a file, or read it from a TextReader or an XmlReader. In all of these cases, LINQ to XML uses one or another of the concrete subclasses of XmlReader.

Sometimes the XmlReader has the base URI, and sometimes it does not. 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.

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. 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. If the underlying XmlReader does not have the base URI, then this property will return an empty string.

Setting SetBaseUri when loading an XML tree will result in slower parsing.

When setting the base URI for an XML tree, LINQ to XML puts an annotation on the root of the tree. This property is a computed property, and navigates up the tree to find the base URI.

The following example loads the base URI and line information as it loads the file. It then prints the base URI and the line information.

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);

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

.NET Framework

Supported in: 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft