XmlAttribute.OwnerElement Property

 

Gets the XmlElement to which the attribute belongs.

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

public virtual XmlElement OwnerElement { get; }

Property Value

Type: System.Xml.XmlElement

The XmlElement that the attribute belongs to or null if this attribute is not part of an XmlElement.

The following example creates an attribute and displays the OwnerElement of the attribute.

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

public class Sample
{
  public static void Main(){

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    //Create an attribute.
    XmlAttribute attr;
    attr = doc.CreateAttribute("bk","genre","urn:samples");
    attr.Value = "novel";

    //Try to display the attribute's owner element.
    if (attr.OwnerElement==null)
      Console.WriteLine("The attribute has not been added to an element\r\n");
    else
      Console.WriteLine(attr.OwnerElement.OuterXml);

    //Add the attribute to an element.
    doc.DocumentElement.SetAttributeNode(attr);

    //Display the attribute's owner element.
    Console.WriteLine("Display the owner element...");
    Console.WriteLine(attr.OwnerElement.OuterXml);

   }
}

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Return to top
Show: