System.Xml.XmlElement Class

Assembly: System.Xml.dll
Namespace: System.Xml
Summary
Represents an element.
C# Syntax:
public class XmlElement : XmlLinkedNode
See also:
System.Xml Namespace

System.Xml.XmlElement Member List:

Public Properties
Attributes Read-only

Overridden:
Gets an XmlAttributeCollection containing the list of attributes for this node.
BaseURI
(inherited from System.Xml.XmlNode)
Read-only

See base class member description: System.Xml.XmlNode.BaseURI


Gets the base URI of the current node.
ChildNodes
(inherited from System.Xml.XmlNode)
Read-only

See base class member description: System.Xml.XmlNode.ChildNodes


Gets all the children of the node.
FirstChild
(inherited from System.Xml.XmlNode)
Read-only

See base class member description: System.Xml.XmlNode.FirstChild


Gets the first child of the node.
HasAttributes Read-only

Gets a boolean value indicating whether the current node has any attributes.
HasChildNodes
(inherited from System.Xml.XmlNode)
Read-only

See base class member description: System.Xml.XmlNode.HasChildNodes


Gets a value indicating whether this node has any child nodes.
InnerText Read-write

Overridden:
Gets or sets the concatenated values of the node and all its children.
InnerXml Read-write

Overridden:
Gets or sets the markup representing just the children of this node.
IsEmpty Read-write

Gets or sets the tag format of the element.
IsReadOnly
(inherited from System.Xml.XmlNode)
Read-only

See base class member description: System.Xml.XmlNode.IsReadOnly


Gets a value indicating whether the node is read-only.
Item
(inherited from System.Xml.XmlNode)
Read-only

Overloaded:
Item[string name] {get

See base class member description: System.Xml.XmlNode.Item


Gets the first child element with the specified XmlNode.Name.
Item
(inherited from System.Xml.XmlNode)
Read-only

Overloaded:
Item[string localname, string ns] {get

See base class member description: System.Xml.XmlNode.Item


Gets the first child element with the specified XmlNode.LocalName and XmlNode.NamespaceURI.
LastChild
(inherited from System.Xml.XmlNode)
Read-only

See base class member description: System.Xml.XmlNode.LastChild


Gets the last child of the node.
LocalName Read-only

Overridden:
Gets the local name of the current node.
Name Read-only

Overridden:
Gets the qualified name of the node.
NamespaceURI Read-only

Overridden:
Gets the namespace URI of this node.
NextSibling Read-only

Overridden:
NodeType Read-only

Overridden:
Gets the type of the current node.
OuterXml
(inherited from System.Xml.XmlNode)
Read-only

See base class member description: System.Xml.XmlNode.OuterXml


Gets the markup representing this node and all its children.
OwnerDocument Read-only

Overridden:
Gets the XmlDocument to which this node belongs.
ParentNode
(inherited from System.Xml.XmlNode)
Read-only

See base class member description: System.Xml.XmlNode.ParentNode


Gets the parent of this node (for nodes that can have parents).
Prefix Read-write

Overridden:
Gets or sets the namespace prefix of this node.
PreviousSibling
(inherited from System.Xml.XmlLinkedNode)
Read-only

See base class member description: System.Xml.XmlLinkedNode.PreviousSibling


Gets the node immediately preceding this node.
Value
(inherited from System.Xml.XmlNode)
Read-write

See base class member description: System.Xml.XmlNode.Value


Gets or sets the value of the node.
Public Methods
AppendChild
(inherited from System.Xml.XmlNode)
See base class member description: System.Xml.XmlNode.AppendChild


Adds the specified node to the end of the list of children of this node.
Clone
(inherited from System.Xml.XmlNode)
See base class member description: System.Xml.XmlNode.Clone


Creates a duplicate of this node.
CloneNode Overridden:
Creates a duplicate of this node.
CreateNavigator
(inherited from System.Xml.XmlNode)
See base class member description: System.Xml.XmlNode.CreateNavigator


Creates an XPathNavigator for navigating this object.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetAttribute Overloaded:
GetAttribute(string name)

Returns the value for the attribute with the specified name.
GetAttribute Overloaded:
GetAttribute(string localName, string namespaceURI)

Returns the value for the attribute with the specified local name and namespace URI.
GetAttributeNode Overloaded:
GetAttributeNode(string name)

Returns the XmlAttribute with the specified name.
GetAttributeNode Overloaded:
GetAttributeNode(string localName, string namespaceURI)

Returns the XmlAttribute with the specified local name and namespace URI.
GetElementsByTagName Overloaded:
GetElementsByTagName(string name)

Returns an XmlNodeList containing a list of all descendant elements that match the specified XmlElement.Name.
GetElementsByTagName Overloaded:
GetElementsByTagName(string localName, string namespaceURI)

Returns an XmlNodeList containing a list of all descendant elements that match the specified XmlElement.LocalName and XmlElement.NamespaceURI.
GetEnumerator
(inherited from System.Xml.XmlNode)
See base class member description: System.Xml.XmlNode.GetEnumerator


Provides support for the for each style iteration over the nodes in the XmlNode.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetNamespaceOfPrefix
(inherited from System.Xml.XmlNode)
See base class member description: System.Xml.XmlNode.GetNamespaceOfPrefix


Looks up the closest xmlns declaration for the given prefix that is in scope for the current node and returns the namespace URI in the declaration.
GetPrefixOfNamespace
(inherited from System.Xml.XmlNode)
See base class member description: System.Xml.XmlNode.GetPrefixOfNamespace


Looks up the closest xmlns declaration for the given namespace URI that is in scope for the current node and returns the prefix defined in that declaration.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
HasAttribute Overloaded:
HasAttribute(string name)

Determines whether the current node has an attribute with the specified name.
HasAttribute Overloaded:
HasAttribute(string localName, string namespaceURI)

Determines whether the current node has an attribute with the specified local name and namespace URI.
InsertAfter
(inherited from System.Xml.XmlNode)
See base class member description: System.Xml.XmlNode.InsertAfter


Inserts the specified node immediately after the specified reference node.
InsertBefore
(inherited from System.Xml.XmlNode)
See base class member description: System.Xml.XmlNode.InsertBefore


Inserts the specified node immediately before the specified reference node.
Normalize
(inherited from System.Xml.XmlNode)
See base class member description: System.Xml.XmlNode.Normalize


Puts all XmlText nodes in the full depth of the sub-tree underneath this XmlNode into a "normal" form where only markup (that is, tags, comments, processing instructions, CDATA sections, and entity references) separates XmlText nodes, that is, there are no adjacent XmlText nodes.
PrependChild
(inherited from System.Xml.XmlNode)
See base class member description: System.Xml.XmlNode.PrependChild


Adds the specified node to the beginning of the list of children of this node.
RemoveAll Overridden:
Removes all specified attributes and children of the current node. Default attributes are not removed.
RemoveAllAttributes Removes all specified attributes from the element. Default attributes are not removed.
RemoveAttribute Overloaded:
RemoveAttribute(string name)

Removes an attribute by name.
RemoveAttribute Overloaded:
RemoveAttribute(string localName, string namespaceURI)

Removes an attribute with the specified local name and namespace URI.
RemoveAttributeAt Removes the attribute node with the specified index from the element.
RemoveAttributeNode Overloaded:
RemoveAttributeNode(XmlAttribute oldAttr)

Removes the specified XmlAttribute.
RemoveAttributeNode Overloaded:
RemoveAttributeNode(string localName, string namespaceURI)

Removes the XmlAttribute specified by the local name and namespace URI.
RemoveChild
(inherited from System.Xml.XmlNode)
See base class member description: System.Xml.XmlNode.RemoveChild


Removes specified child node.
ReplaceChild
(inherited from System.Xml.XmlNode)
See base class member description: System.Xml.XmlNode.ReplaceChild


Replaces the child node oldChild with newChild node.
SelectNodes
(inherited from System.Xml.XmlNode)
Overloaded:
SelectNodes(string xpath)

See base class member description: System.Xml.XmlNode.SelectNodes


Selects a list of nodes matching the XPath expression.
SelectNodes
(inherited from System.Xml.XmlNode)
Overloaded:
SelectNodes(string xpath, XmlNamespaceManager nsmgr)

See base class member description: System.Xml.XmlNode.SelectNodes


Selects a list of nodes matching the XPath expression. Any prefixes found in the XPath expression are resolved using the supplied XmlNamespaceManager.
SelectSingleNode
(inherited from System.Xml.XmlNode)
Overloaded:
SelectSingleNode(string xpath)

See base class member description: System.Xml.XmlNode.SelectSingleNode


Selects the first XmlNode that matches the XPath expression.
SelectSingleNode
(inherited from System.Xml.XmlNode)
Overloaded:
SelectSingleNode(string xpath, XmlNamespaceManager nsmgr)

See base class member description: System.Xml.XmlNode.SelectSingleNode


Selects the first XmlNode that matches the XPath expression. Any prefixes found in the XPath expression are resolved using the supplied XmlNamespaceManager.
SetAttribute Overloaded:
SetAttribute(string name, string value)

Sets the value of the attribute with the specified name.
SetAttribute Overloaded:
SetAttribute(string localName, string namespaceURI, string value)

Sets the value of the attribute with the specified local name and namespace URI.
SetAttributeNode Overloaded:
SetAttributeNode(XmlAttribute newAttr)

Adds the specified XmlAttribute.
SetAttributeNode Overloaded:
SetAttributeNode(string localName, string namespaceURI)

Adds the specified XmlAttribute.
Supports
(inherited from System.Xml.XmlNode)
See base class member description: System.Xml.XmlNode.Supports


Test if the DOM implementation implements a specific feature.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
WriteContentTo Overridden:
Saves all the children of the node to the specified XmlWriter.
WriteTo Overridden:
Saves the current node to the specified XmlWriter.
Protected Constructors
ctor #1
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Xml.XmlElement Member Details

ctor #1
Summary
This type supports the Shared Source CLI infrastructure and is not intended to be used directly from your code.
C# Syntax:
protected internal XmlElement(
   string prefix,
   string localName,
   string namespaceURI,
   XmlDocument doc
);
Parameters:

prefix

localName

namespaceURI

doc

Return to top


Overridden Property: Attributes (read-only)
Summary
Gets an XmlAttributeCollection containing the list of attributes for this node.
C# Syntax:
public override XmlAttributeCollection Attributes {get;}
Example
The following example uses changes the value of the first attribute.


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

public class Sample
{
  public static void Main()
  {
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlElement root = doc.DocumentElement;

    //Change the value of the first attribute.
    root.Attributes[0].Value="fiction";

    Console.WriteLine("Display the modified XML...");
    Console.WriteLine(doc.InnerXml);
  }
}

    

Return to top


Property: BaseURI (read-only)
Inherited
See base class member description: System.Xml.XmlNode.BaseURI

Summary
Gets the base URI of the current node.
C# Syntax:
public virtual string BaseURI {get;}
Remarks
A networked XML document is comprised of chunks of data aggregated using various W3C standard inclusion mechanisms and therefore contains nodes that come from different places. The BaseURI tells you where these nodes came from.

The value of this property varies depending on the node type. For example, Document nodes return the location of the XmlDocument object. Nodes which are children of external EntityReference nodes return the location of the entity itself. For example, consider the following XML document:

              <!DOCTYPE item [
               <!ENTITY bar SYSTEM "a/b.xml">
               ]>
               <item num='123'>&bar;</item>
                 
            

where the external entity a/b.xml contains the XML text: <test>123</test> .

If the document is loaded from http://server/mydata.xml , BaseURI returns the following:



NodeType Name BaseURI
Attribute num http://server/mydata.xml
Document #document http://server/mydata.xml
DocumentType item http://server/mydata.xml
Entity bar http://server/mydata.xml
Element item http://server/mydata.xml
EntityReference bar http://server/mydata.xml
Element test http://server/a/b.xml
Text #text http://server/a/b.xml

BaseURI looks for entity reference boundaries, so if entities are expanded this information is not preserved and this property returns the location of the XmlDocument object in all cases.

As a second example, given the following XML document:

              <!DOCTYPE Mydata SYSTEM "http://localhost/doctype.dtd">
              <baa>&bar;</baa>
            
where the DTD file contains the following:
              <!ENTITY bar <E1>My Data</E1>
              <!ELEMENT baa #PCDATA>
              <!ATTLIST baa attr1 "woof">
            

If the XML document is loaded from http://localhost/mydata.xml, BaseURI returns the following for each of the nodes:



NodeType Name BaseURI
Document #document http://localhost/mydata.xml
DocumentType Mydata http://localhost/doctype.dtd
Element baa http://localhost/mydata.xml
Entity bar http://localhost/doctype.dtd
EntityReference bar http://localhost/mydata.xml
Attribute woof http://localhost/mydata.xml


Note The base URI of a default attribute is the same as the base URI of the element to which they belong.

This property is a Microsoft extension to the Document Object Model (DOM).

Return to top


Property: ChildNodes (read-only)
Inherited
See base class member description: System.Xml.XmlNode.ChildNodes

Summary
Gets all the children of the node.
C# Syntax:
public virtual XmlNodeList ChildNodes {get;}
Example
The following example displays all the child nodes of the root element.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "<price>19.95</price>" +
                "</book>");

    XmlNode root = doc.FirstChild;

    //Display the contents of the child nodes.
    if (root.HasChildNodes)
    {
      for (int i=0; i<root.ChildNodes.Count; i++)
      {
        Console.WriteLine(root.ChildNodes[i].InnerText);
      }
    }
  }
}

    

Return to top


Property: FirstChild (read-only)
Inherited
See base class member description: System.Xml.XmlNode.FirstChild

Summary
Gets the first child of the node.
C# Syntax:
public virtual XmlNode FirstChild {get;}
Example
The following example displays the title element.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "<price>19.95</price>" +
                "</book>");

    XmlNode root = doc.FirstChild;

    Console.WriteLine("Display the title element...");
    Console.WriteLine(root.FirstChild.OuterXml);
  }
}

    

Return to top


Property: HasAttributes (read-only)
Summary
Gets a boolean value indicating whether the current node has any attributes.
C# Syntax:
public virtual bool HasAttributes {get;}
Remarks
This property is a Microsoft extension to the Document Object Model (DOM).
Example
The following example removes all attributes from the root element.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlElement root = doc.DocumentElement;

    //Remove all attributes from the root element.
    if (root.HasAttributes)
      root.RemoveAllAttributes();
    
    Console.WriteLine("Display the modified XML...");
    Console.WriteLine(doc.InnerXml);

  }
}

    

Return to top


Property: HasChildNodes (read-only)
Inherited
See base class member description: System.Xml.XmlNode.HasChildNodes

Summary
Gets a value indicating whether this node has any child nodes.
C# Syntax:
public virtual bool HasChildNodes {get;}
Example
The following example displays all the child nodes of the root element.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "<price>19.95</price>" +
                "</book>");

    XmlNode root = doc.FirstChild;

    //Display the contents of the child nodes.
    if (root.HasChildNodes)
    {
      for (int i=0; i<root.ChildNodes.Count; i++)
      {
        Console.WriteLine(root.ChildNodes[i].InnerText);
      }
    }
  }
}

    

Return to top


Overridden Property: InnerText (read-write)
Summary
Gets or sets the concatenated values of the node and all its children.
C# Syntax:
public override string InnerText {get; set;}
Remarks
Setting this property replaces all the children with the parsed contents of the given string.

This property is a Microsoft extension to the Document Object Model (DOM).

Example
The following example compares the InnerText and XmlElement.InnerXml properties.
using System;
using System.Xml;
public class Test {

  public static void Main() {
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<root>"+
                "<elem>some text<child/>more text</elem>" +
                "</root>");

    XmlElement elem = (XmlElement)doc.DocumentElement.FirstChild;

    //Note that InnerText does not include the markup.
    Console.WriteLine("Display the InnerText of the element...");
    Console.WriteLine( elem.InnerText );

    //InnerXml includes the element's markup.
    Console.WriteLine("Display the InnerXml of the element...");
    Console.WriteLine(elem.InnerXml);

    //Set InnerText to a string that includes markup.  
    //The markup is entitized.
    elem.InnerText = "Text containing <markup/> will have char(<) and char(>) entitized.";
    Console.WriteLine( elem.OuterXml );

    //Set InnerXml to a string that includes markup.  
    //The markup is not entitized.
    elem.InnerXml = "Text containing <markup/>.";
    Console.WriteLine( elem.OuterXml );
  }
}

    

Return to top


Overridden Property: InnerXml (read-write)
Summary
Gets or sets the markup representing just the children of this node.
C# Syntax:
public override string InnerXml {get; set;}
Remarks
Setting this property replaces the children of the node with the parsed contents of the given string. The parsing is done in the current namespace context.

This property is a Microsoft extension to the Document Object Model (DOM).

Example
The following example compares the XmlElement.InnerText and InnerXml properties.
using System;
using System.Xml;
public class Test {

  public static void Main() {
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<root>"+
                "<elem>some text<child/>more text</elem>" +
                "</root>");

    XmlElement elem = (XmlElement)doc.DocumentElement.FirstChild;

    //Note that InnerText does not include the markup.
    Console.WriteLine("Display the InnerText of the element...");
    Console.WriteLine( elem.InnerText );

    //InnerXml includes the element's markup.
    Console.WriteLine("Display the InnerXml of the element...");
    Console.WriteLine(elem.InnerXml);

    //Set InnerText to a string that includes markup.  
    //The markup is entitized.
    elem.InnerText = "Text containing <markup/> will have char(<) and char(>) entitized.";
    Console.WriteLine( elem.OuterXml );

    //Set InnerXml to a string that includes markup.  
    //The markup is not entitized.
    elem.InnerXml = "Text containing <markup/>.";
    Console.WriteLine( elem.OuterXml );
  }
}

    

Return to top


Property: IsEmpty (read-write)
Summary
Gets or sets the tag format of the element.
C# Syntax:
public bool IsEmpty {get; set;}
Remarks
This property is a Microsoft extension of the Document Object Model (DOM).
Example
The following example adds content to an empty element.

 
 using System;
 using System.IO;
 using System.Xml;
 
 public class Sample
 {
   public static void Main()
   {
     try
     {
       XmlDocument doc = new XmlDocument();
       doc.LoadXml("<book xmlns:bk=\"urn:samples\">"+ 
                   "  <title>Pride And Prejudice</title>" +
                   "  <author>" +
                   "    <first-name>Jane</first-name>" +
                   "    <last-name>Austen</last-name>" +
                   "  </author>" +
                   "  <price>19.95</price>" +
                   "  <misc/>" +
                   "  <bk:ISBN>1-861001-57-5</bk:ISBN>" +
                   "</book>");     
 
 
       XmlElement currNode = (XmlElement) doc.DocumentElement.ChildNodes.Item(3);
       if (currNode.IsEmpty)
         currNode.InnerXml="<style>hardcover</style>"; 
 
       Console.WriteLine("Display the modified XML...");
       XmlTextWriter writer = new XmlTextWriter(Console.Out);
       writer.Formatting = Formatting.Indented;
       doc.Save(writer);
 
     }
     catch (Exception e)
     {
       Console.WriteLine ("Exception: {0}", e.ToString());
     }
 
   }
 }

    

Return to top


Property: IsReadOnly (read-only)
Inherited
See base class member description: System.Xml.XmlNode.IsReadOnly

Summary
Gets a value indicating whether the node is read-only.
C# Syntax:
public virtual bool IsReadOnly {get;}
Remarks
A read-only node is one whose properties, attributes, or children cannot be changed. You can remove a read-only node from the tree and insert it somewhere else. For example, Entity nodes are always read-only.

This property is a Microsoft extension to the Document Object Model (DOM).

Return to top


Overloaded Property: Item (read-only)
Inherited
See base class member description: System.Xml.XmlNode.Item

Summary
Gets the first child element with the specified XmlNode.Name.
C# Syntax:
public virtual XmlElement this[string name] {get;}
Parameters:

name

The qualified name of the element to retrieve

Remarks
This property is a Microsoft extension to the Document Object Model (DOM).
Example
The following example displays the title element.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "<price>19.95</price>" +
                "</book>");

    XmlNode root = doc.FirstChild;

    Console.WriteLine("Display the title element...");
    Console.WriteLine(root["title"].OuterXml);
  }
}

    

Return to top


Overloaded Property: Item (read-only)
Inherited
See base class member description: System.Xml.XmlNode.Item

Summary
Gets the first child element with the specified XmlNode.LocalName and XmlNode.NamespaceURI.
C# Syntax:
public virtual XmlElement this[string localname, string ns] {get;}
Parameters:

localname

The local name of the element.

ns

The namespace URI of the element.

Remarks
This property is a Microsoft extension to the Document Object Model (DOM).

Return to top


Property: LastChild (read-only)
Inherited
See base class member description: System.Xml.XmlNode.LastChild

Summary
Gets the last child of the node.
C# Syntax:
public virtual XmlNode LastChild {get;}
Example
The following example displays the price element.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "<price>19.95</price>" +
                "</book>");

    XmlNode root = doc.FirstChild;

    Console.WriteLine("Display the price element...");
    Console.WriteLine(root.LastChild.OuterXml);
  }
}

    

Return to top


Overridden Property: LocalName (read-only)
Summary
Gets the local name of the current node.
C# Syntax:
public override string LocalName {get;}
Remarks
If the node does not have a prefix, LocalName is the same as XmlElement.Name.
Example
The following example displays information on the ISBN element.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book xmlns:bk='urn:samples'>" +
                "<bk:ISBN>1-861001-57-5</bk:ISBN>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    //Display information on the ISBN element.
    XmlElement elem = (XmlElement) doc.DocumentElement.FirstChild;
    Console.Write("{0}:{1} = {2}", elem.Prefix, elem.LocalName, elem.InnerText);
    Console.WriteLine("\t namespaceURI=" + elem.NamespaceURI);

  }
}

    

Return to top


Overridden Property: Name (read-only)
Summary
Gets the qualified name of the node.
C# Syntax:
public override string Name {get;}
Example
The following example displays information on the ISBN element.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book xmlns:bk='urn:samples'>" +
                "<bk:ISBN>1-861001-57-5</bk:ISBN>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    //Display information on the ISBN element.
    XmlElement elem = (XmlElement) doc.DocumentElement.FirstChild;
    Console.Write("{0} = {1}", elem.Name, elem.InnerText);
    Console.WriteLine("\t namespaceURI=" + elem.NamespaceURI);

  }
}

    
See also:
XmlElement.LocalName

Return to top


Overridden Property: NamespaceURI (read-only)
Summary
Gets the namespace URI of this node.
C# Syntax:
public override string NamespaceURI {get;}
Remarks
This is the namespace URI specified at creation time. For example, NamespaceURI is urn:samples for the element <bk:book xmlns:bk= "urn:samples">
Example
The following example displays information on the ISBN element.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book xmlns:bk='urn:samples'>" +
                "<bk:ISBN>1-861001-57-5</bk:ISBN>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    //Display information on the ISBN element.
    XmlElement elem = (XmlElement) doc.DocumentElement.FirstChild;
    Console.Write("{0}:{1} = {2}", elem.Prefix, elem.LocalName, elem.InnerText);
    Console.WriteLine("\t namespaceURI=" + elem.NamespaceURI);

  }
}

    

Return to top


Overridden Property: NextSibling (read-only)
Summary
Gets the XmlNode immediately following this element.
C# Syntax:
public override XmlNode NextSibling {get;}

Return to top


Overridden Property: NodeType (read-only)
Summary
Gets the type of the current node.
C# Syntax:
public override XmlNodeType NodeType {get;}
Example
See also:
XmlNodeType

Return to top


Property: OuterXml (read-only)
Inherited
See base class member description: System.Xml.XmlNode.OuterXml

Summary
Gets the markup representing this node and all its children.
C# Syntax:
public virtual string OuterXml {get;}
Remarks
This property is a Microsoft extension to the Document Object Model (DOM).
Example
The following example compares output from the XmlNode.InnerXml and OuterXml properties.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlNode root = doc.DocumentElement;

    //OuterXml includes the markup of current node.
    Console.WriteLine("Display the OuterXml property...");
    Console.WriteLine(root.OuterXml);
            
    //InnerXml does not include the markup of the current node.
    //As a result, the attributes are not displayed.
    Console.WriteLine();
    Console.WriteLine("Display the InnerXml property...");
    Console.WriteLine(root.InnerXml);
           
    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);

  }
}

    

Return to top


Overridden Property: OwnerDocument (read-only)
Summary
Gets the XmlDocument to which this node belongs.
C# Syntax:
public override XmlDocument OwnerDocument {get;}
Remarks
When adding nodes to the current node, use the XmlDocument returned by the OwnerDocument property to create the node.
Example
The following example inserts a new element into an XML document.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlElement root = doc.DocumentElement;

    //Create a new element.
    XmlElement elem = doc.CreateElement("price");
    elem.InnerText="19.95";

    //Display the new element's owner document. Note
    //that although the element has not been inserted
    //into the document, it still has an owner document.
    Console.WriteLine(elem.OwnerDocument.OuterXml);
    
    //Add the new element into the document.
    root.AppendChild(elem);

    Console.WriteLine("Display the modified XML...");
    Console.WriteLine(doc.InnerXml);

  }
}

    

Return to top


Property: ParentNode (read-only)
Inherited
See base class member description: System.Xml.XmlNode.ParentNode

Summary
Gets the parent of this node (for nodes that can have parents).
C# Syntax:
public virtual XmlNode ParentNode {get;}

Return to top


Overridden Property: Prefix (read-write)
Summary
Gets or sets the namespace prefix of this node.
C# Syntax:
public override string Prefix {get; set;}
Exceptions
Exception Type Condition
ArgumentException This node is read-only
XmlException The specified prefix contains an illegal character.

The specified prefix is malformed.

The namespaceURI of this node is null.

The specified prefix is "xml" and the namespaceURI of this node is different from http://www.w3.org/XML/1998/namespace .

Remarks
Setting this property changes the XmlElement.Name property, which holds the qualified name for an XmlElement. However, changing the prefix does not change the namespace URI of the element.
Example
The following example displays information on the ISBN element.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book xmlns:bk='urn:samples'>" +
                "<bk:ISBN>1-861001-57-5</bk:ISBN>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    //Display information on the ISBN element.
    XmlElement elem = (XmlElement) doc.DocumentElement.FirstChild;
    Console.Write("{0}:{1} = {2}", elem.Prefix, elem.LocalName, elem.InnerText);
    Console.WriteLine("\t namespaceURI=" + elem.NamespaceURI);

  }
}

    

Return to top


Property: PreviousSibling (read-only)
Inherited
See base class member description: System.Xml.XmlLinkedNode.PreviousSibling

Summary
Gets the node immediately preceding this node.
C# Syntax:
public override XmlNode PreviousSibling {get;}
See also:
XmlLinkedNode.NextSibling

Return to top


Property: Value (read-write)
Inherited
See base class member description: System.Xml.XmlNode.Value

Summary
Gets or sets the value of the node.
C# Syntax:
public virtual string Value {get; set;}
Exceptions
Exception Type Condition
ArgumentException The node is read-only.
InvalidOperationException The node is not supposed to have a value (for example, an Element node).

Return to top


Method: AppendChild(
   XmlNode newChild
)
Inherited
See base class member description: System.Xml.XmlNode.AppendChild

Summary
Adds the specified node to the end of the list of children of this node.
C# Syntax:
public virtual XmlNode AppendChild(
   XmlNode newChild
);
Parameters:

newChild

The node to add. If it is a XmlDocumentFragment, the entire contents of the document fragment are moved into the child list of this node.

Return Value:
The node added.
Exceptions
Exception Type Condition
InvalidOperationException This node is of a type that does not allow children of the type of the newChild node. Or the node to be added is one of this node's ancestors.
ArgumentException The newChild was created from a different document than the one that created this node.

This node is read-only.

Remarks
If the newChild is already in the tree, it is first removed.

Notes to inheritors: When overriding AppendChild in a derived class, in order for events to be fired correctly, you must call the base class's AppendChild method.
Example
The following example adds a new node to the XML document.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlNode root = doc.DocumentElement;

    //Create a new node.
    XmlElement elem = doc.CreateElement("price");
    elem.InnerText="19.95";

    //Add the node to the document.
    root.AppendChild(elem);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);

  }
}

    
See also:
XmlDocument.ImportNode

Return to top


Method: Clone()
Inherited
See base class member description: System.Xml.XmlNode.Clone

Summary
Creates a duplicate of this node.
C# Syntax:
public virtual XmlNode Clone();
Return Value:
The cloned node.
Remarks
Cloning an XmlElement copies all attributes and their values, including those generated by the XML processor to represent defaulted attributes. This method recursively clones the node and the subtree underneath it.

Clone is equivalent to calling CloneNode(true) .

This method is a Microsoft extension to the Document Object Model (DOM).

Example
The following example clones the root node of the XML document.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "<price>19.95</price>" +
                "</book>");

    XmlNode root = doc.FirstChild;

    //Clone the root node.  The cloned node includes
    //child nodes. This is similar to calling CloneNode(true).
    XmlNode clone = root.Clone();
    Console.WriteLine(clone.OuterXml);
  }
}

    
See also:
XmlNode.CloneNode

Return to top


Overridden Method: CloneNode(
   bool deep
)
Summary
Creates a duplicate of this node.
C# Syntax:
public override XmlNode CloneNode(
   bool deep
);
Parameters:

deep

true to recursively clone the subtree under the specified node; false to clone only the node itself (and its attributes if the node is an XmlElement).

Return Value:
The cloned node.
Remarks
This method serves as a copy constructor for nodes. The duplicate node has no parent ( XmlNode.ParentNode returns null).
Example
The following example creates a new element, clones it, and then adds both elements into an XML document.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.Load("2books.xml");

    //Create a new element.
    XmlElement elem = doc.CreateElement("misc");
    elem.InnerText = "hardcover";
    elem.SetAttribute("publisher", "WorldWide Publishing");

    //Clone the element so we can add one to each of the book nodes.
    XmlNode elem2 = elem.CloneNode(true);

    //Add the new elements.
    doc.DocumentElement.FirstChild.AppendChild(elem);
    doc.DocumentElement.LastChild.AppendChild(elem2);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);

  }
}

    

Return to top


Method: CreateNavigator()
Inherited
See base class member description: System.Xml.XmlNode.CreateNavigator

Summary
Creates an XPathNavigator for navigating this object.
C# Syntax:
public XPathNavigator CreateNavigator();
Return Value:
An XPathNavigator object. The XPathNavigator is positioned on the node from which the method was called. It is not positioned on the root of the document.
Implements:
IXPathNavigable.CreateNavigator
Remarks
The XPathNavigator provides read-only, random access to data. Because it is optimized for XSLT transformations, it provides performance benefits when used as an input mechanism to the XslTransform.Transform method.

This method is a Microsoft extension to the Document Object Model (DOM).

Example
The following example loads and edits an XML document before performing an XSLT transform.
  XmlDocument doc = new XmlDocument();
  doc.Load("books.xml");

  // Modify the XML file.
  XmlElement root = doc.DocumentElement;
  root.FirstChild.LastChild.InnerText = "12.95";

  // Create an XPathNavigator to use for the transform.
  XPathNavigator nav = root.CreateNavigator();
  
  // Transform the file.
  XslTransform xslt = new XslTransform();
  xslt.Load("output.xsl");
  XmlTextWriter writer = new XmlTextWriter("books.html", null);
  xslt.Transform(nav, null, writer);

    
See also:
XslTransform

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~XmlElement();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: GetAttribute(
   string name
)
Summary
Returns the value for the attribute with the specified name.
C# Syntax:
public virtual string GetAttribute(
   string name
);
Parameters:

name

The name of the attribute to retrieve. This is a qualified name. It is matched against the Name property of the matching node.

Return Value:
The value of the specified attribute. An empty string is returned if a matching attribute is not found or if the attribute does not have a specified or default value.
Example
The following example checks to see if the element has the specified attribute.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlElement root = doc.DocumentElement;

    //Check to see if the element has a genre attribute.
    if (root.HasAttribute("genre")){
      String genre = root.GetAttribute("genre");
      Console.WriteLine(genre);
   }

  }
}

    

Return to top


Overloaded Method: GetAttribute(
   string localName,
   string namespaceURI
)
Summary
Returns the value for the attribute with the specified local name and namespace URI.
C# Syntax:
public virtual string GetAttribute(
   string localName,
   string namespaceURI
);
Parameters:

localName

The local name of the attribute to retrieve.

namespaceURI

The namespace URI of the attribute to retrieve.

Return Value:
The value of the specified attribute. An empty string is returned if a matching attribute is not found or if the attribute does not have a specified or default value.

Return to top


Overloaded Method: GetAttributeNode(
   string name
)
Summary
Returns the XmlAttribute with the specified name.
C# Syntax:
public virtual XmlAttribute GetAttributeNode(
   string name
);
Parameters:

name

The name of the attribute to retrieve. This is a qualified name. It is matched against the Name property of the matching node.

Return Value:
The specified XmlAttribute or null if a matching attribute was not found.
Example
The following example checks to see if the element has the specified attribute.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlElement root = doc.DocumentElement;

    //Check to see if the element has a genre attribute.
    if (root.HasAttribute("genre")){
      XmlAttribute attr = root.GetAttributeNode("genre");
      Console.WriteLine(attr.Value);
   }

  }
}

    

Return to top


Overloaded Method: GetAttributeNode(
   string localName,
   string namespaceURI
)
Summary
Returns the XmlAttribute with the specified local name and namespace URI.
C# Syntax:
public virtual XmlAttribute GetAttributeNode(
   string localName,
   string namespaceURI
);
Parameters:

localName

The local name of the attribute.

namespaceURI

The namespace URI of the attribute.

Return Value:
The specified XmlAttribute or null if a matching attribute was not found.
Example

Return to top


Overloaded Method: GetElementsByTagName(
   string name
)
Summary
Returns an XmlNodeList containing a list of all descendant elements that match the specified XmlElement.Name.
C# Syntax:
public virtual XmlNodeList GetElementsByTagName(
   string name
);
Parameters:

name

The name tag to match. This is a qualified name. It is matched against the Name property of the matching node. The asterik (*) is a special value that matches all tags.

Return Value:
An XmlNodeList containing a list of all matching nodes.
Remarks
The nodes are placed in the order in which they would be encountered in a preorder traversal of the XmlElement tree.
Example
The following example gets and displays all the book titles.


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

public class Sample
{
  public static void Main()
  {
     XmlDocument doc = new XmlDocument();
     doc.Load("2books.xml");
                         
     //Get and display all the book titles.
     XmlElement root = doc.DocumentElement;
     XmlNodeList elemList = root.GetElementsByTagName("title");
     for (int i=0; i < elemList.Count; i++)
     {   
        Console.WriteLine(elemList[i].InnerXml);
     } 
    
  }
}

    
The example uses the file, 2books.xml, as input.
<!--sample XML fragment-->
<bookstore>
  <book genre='novel' ISBN='10-861003-324'>
    <title>The Handmaid's Tale</title>
    <price>19.95</price>
  </book>
  <book genre='novel' ISBN='1-861001-57-5'>
    <title>Pride And Prejudice</title>
    <price>24.95</price>
  </book>
</bookstore>

    

Return to top


Overloaded Method: GetElementsByTagName(
   string localName,
   string namespaceURI
)
Summary
Returns an XmlNodeList containing a list of all descendant elements that match the specified XmlElement.LocalName and XmlElement.NamespaceURI.
C# Syntax:
public virtual XmlNodeList GetElementsByTagName(
   string localName,
   string namespaceURI
);
Parameters:

localName

The local name to match. The asterik (*) is a special value that matches all tags.

namespaceURI

The namespace URI to match.

Return Value:
An XmlNodeList containing a list of all matching nodes.
Remarks
The nodes are placed in the order in which they would be encountered in a preorder traversal of the XmlElement tree.

Return to top


Method: GetEnumerator()
Inherited
See base class member description: System.Xml.XmlNode.GetEnumerator

Summary
Provides support for the for each style iteration over the nodes in the XmlNode.
C# Syntax:
public IEnumerator GetEnumerator();
Return Value:
An IEnumerator.
Remarks
This method is a Microsoft extension to the Document Object Model (DOM).
Example
The following example displays all the books in the XML document.


using System;
using System.Collections;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    XmlDocument doc = new XmlDocument();
    doc.Load("books.xml");

    Console.WriteLine("Display all the books...");
    XmlNode root = doc.DocumentElement;
    IEnumerator ienum = root.GetEnumerator();
    XmlNode book;
    while (ienum.MoveNext()) 
    {     
      book = (XmlNode) ienum.Current;
      Console.WriteLine(book.OuterXml);
      Console.WriteLine();
    }

  }
}

    

The example uses the file, books.xml, as input.


<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

    

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetNamespaceOfPrefix(
   string prefix
)
Inherited
See base class member description: System.Xml.XmlNode.GetNamespaceOfPrefix

Summary
Looks up the closest xmlns declaration for the given prefix that is in scope for the current node and returns the namespace URI in the declaration.
C# Syntax:
public virtual string GetNamespaceOfPrefix(
   string prefix
);
Parameters:

prefix

Prefix whose namespace URI you want to find

Return Value:
The namespace URI of the specified prefix.
Remarks
This method is a Microsoft extension to the Document Object Model (DOM).
Example
The following example adds a new attribute to the XML document.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlNode root = doc.FirstChild;

    //Create a new attribute.
    string ns = root.GetNamespaceOfPrefix("bk");
    XmlNode attr = doc.CreateNode(XmlNodeType.Attribute, "genre", ns);
    attr.Value = "novel";

    //Add the attribute to the document.
    root.Attributes.SetNamedItem(attr);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);

  }
}

    

Return to top


Method: GetPrefixOfNamespace(
   string namespaceURI
)
Inherited
See base class member description: System.Xml.XmlNode.GetPrefixOfNamespace

Summary
Looks up the closest xmlns declaration for the given namespace URI that is in scope for the current node and returns the prefix defined in that declaration.
C# Syntax:
public virtual string GetPrefixOfNamespace(
   string namespaceURI
);
Parameters:

namespaceURI

Namespace URI whose prefix you want to find

Return Value:
The prefix for the specified namespace URI.
Remarks
This method is a Microsoft extension to the Document Object Model (DOM).
Example
The following example adds a new element to the XML document.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlNode root = doc.FirstChild;

    //Create a new node.
    string prefix = root.GetPrefixOfNamespace("urn:samples");
    XmlElement elem = doc.CreateElement(prefix, "style", "urn:samples");
    elem.InnerText = "hardcover";

    //Add the node to the document.
    root.AppendChild(elem);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);

  }
}

    

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: HasAttribute(
   string name
)
Summary
Determines whether the current node has an attribute with the specified name.
C# Syntax:
public virtual bool HasAttribute(
   string name
);
Parameters:

name

The name of the attribute to find. This is a qualified name. It is matched against the Name property of the matching node.

Return Value:
true if the current node has the specified attribute; otherwise, false.
Example
The following example checks to see if the element has the specified attribute.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlElement root = doc.DocumentElement;

    //Check to see if the element has a genre attribute.
    if (root.HasAttribute("genre")){
      String genre = root.GetAttribute("genre");
      Console.WriteLine(genre);
   }

  }
}

    

Return to top


Overloaded Method: HasAttribute(
   string localName,
   string namespaceURI
)
Summary
Determines whether the current node has an attribute with the specified local name and namespace URI.
C# Syntax:
public virtual bool HasAttribute(
   string localName,
   string namespaceURI
);
Parameters:

localName

The local name of the attribute to find.

namespaceURI

The namespace URI of the attribute to find.

Return Value:
true if the current node has the specified attribute; otherwise, false.

Return to top


Method: InsertAfter(
   XmlNode newChild,
   XmlNode refChild
)
Inherited
See base class member description: System.Xml.XmlNode.InsertAfter

Summary
Inserts the specified node immediately after the specified reference node.
C# Syntax:
public virtual XmlNode InsertAfter(
   XmlNode newChild,
   XmlNode refChild
);
Parameters:

newChild

The XmlNode to insert.

refChild

The XmlNode that is the reference node. The newNode is placed after the refNode.

Return Value:
The node being inserted.
Exceptions
Exception Type Condition
InvalidOperationException This node is of a type that does not allow children of the type of the newChild node. Or the node to insert is one of this node's ancestors.
ArgumentException The newChild was created from a different document than the one that created this node.

The refChild is not a child of this node.

This node is read-only.

Remarks
If refChild is null, insert newChild at the beginning of the list of children. If newChild is an XmlDocumentFragment object, its children are inserted, in the same order, after refChild. If the newChild is already in the tree, it is first removed.

This method is a Microsoft extension to the Document Object Model (DOM).



Notes to inheritors: When overriding InsertAfter in a derived class, in order for events to be fired correctly, you must call the base class's InsertAfter method.
Example
The following example adds a new node to the XML document.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlNode root = doc.DocumentElement;

    //Create a new node.
    XmlElement elem = doc.CreateElement("price");
    elem.InnerText="19.95";

    //Add the node to the document.
    root.InsertAfter(elem, root.FirstChild);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);

  }
}

    
See also:
XmlDocument.ImportNode

Return to top


Method: InsertBefore(
   XmlNode newChild,
   XmlNode refChild
)
Inherited
See base class member description: System.Xml.XmlNode.InsertBefore

Summary
Inserts the specified node immediately before the specified reference node.
C# Syntax:
public virtual XmlNode InsertBefore(
   XmlNode newChild,
   XmlNode refChild
);
Parameters:

newChild

The XmlNode to insert.

refChild

The XmlNode that is the reference node. The newChild is placed before this node.

Return Value:
The node being inserted.
Exceptions
Exception Type Condition
InvalidOperationException The current node is of a type that does not allow children of the type of the newChild node. Or the node to insert is an ancestor of this node.
ArgumentException The newChild was created from a different document than the one that created this node.

The refChild is not a child of this node.

This node is read-only.

Remarks
If refChild is null, insert newChild at the end of the list of children. If newChild is an XmlDocumentFragment object, its children are inserted, in the same order, before refChild. If the newChild is already in the tree, it is first removed.

Notes to inheritors: When overriding InsertBefore in a derived class, in order for events to be fired correctly, you must call the base class's InsertBefore method.
Example
The following example adds a new node to the XML document.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlNode root = doc.DocumentElement;

    //Create a new node.
    XmlElement elem = doc.CreateElement("price");
    elem.InnerText="19.95";

    //Add the node to the document.
    root.InsertBefore(elem, root.FirstChild);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);

  }
}

    
See also:
XmlDocument.ImportNode

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Normalize()
Inherited
See base class member description: System.Xml.XmlNode.Normalize

Summary
Puts all XmlText nodes in the full depth of the sub-tree underneath this XmlNode into a "normal" form where only markup (that is, tags, comments, processing instructions, CDATA sections, and entity references) separates XmlText nodes, that is, there are no adjacent XmlText nodes.
C# Syntax:
public virtual void Normalize();
Remarks
This method can be used to ensure that the DOM view of a document is the same as if it were saved and re-loaded, and is useful when operations (such as XPointer lookups) that depend on a particular document tree structure are to be used.

Return to top


Method: PrependChild(
   XmlNode newChild
)
Inherited
See base class member description: System.Xml.XmlNode.PrependChild

Summary
Adds the specified node to the beginning of the list of children of this node.
C# Syntax:
public virtual XmlNode PrependChild(
   XmlNode newChild
);
Parameters:

newChild

The node to add. If it is an XmlDocumentFragment, the entire contents of the document fragment are moved into the child list of this node.

Return Value:
The node added.
Exceptions
Exception Type Condition
InvalidOperationException This node is of a type that does not allow children of the type of the newChild node. Or the node to be added is one of this node's ancestors.
ArgumentException The newChild was created from a different document than the one that created this node. Or this node is read-only.
Remarks
If the newChild is already in the tree, it is first removed.

This method is a Microsoft extension to the Document Object Model (DOM).



Notes to inheritors: When overriding PrependChild in a derived class, in order for events to be fired correctly, you must call the base class's PrependChild method.
Example
The following example adds a new node to the XML document.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlNode root = doc.DocumentElement;

    //Create a new node.
    XmlElement elem = doc.CreateElement("price");
    elem.InnerText="19.95";

    //Add the node to the document.
    root.PrependChild(elem);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);

  }
}

    
See also:
XmlDocument.ImportNode

Return to top


Overridden Method: RemoveAll()
Summary
Removes all specified attributes and children of the current node. Default attributes are not removed.
C# Syntax:
public override void RemoveAll();
Example
The following example removes all attributes and child nodes from the root element.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    //Remove all attributes and child nodes from the book element.
    XmlElement root = doc.DocumentElement;
    root.RemoveAll();

    Console.WriteLine("Display the modified XML...");
    Console.WriteLine(doc.InnerXml);
  }
}

    

Return to top


Method: RemoveAllAttributes()
Summary
Removes all specified attributes from the element. Default attributes are not removed.
C# Syntax:
public virtual void RemoveAllAttributes();
Remarks
This method is a Microsoft extension to the Document Object Model (DOM).
Example
The following example removes all attributes from the root element.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlElement root = doc.DocumentElement;

    //Remove all attributes from the root element.
    root.RemoveAllAttributes();
    
    Console.WriteLine("Display the modified XML...");
    Console.WriteLine(doc.InnerXml);

  }
}

    

Return to top


Overloaded Method: RemoveAttribute(
   string name
)
Summary
Removes an attribute by name.
C# Syntax:
public virtual void RemoveAttribute(
   string name
);
Parameters:

name

The name of the attribute to remove.This is a qualified name. It is matched against the Name property of the matching node.

Exceptions
Exception Type Condition
ArgumentException The node is read-only.
Remarks
If the removed attribute is known to have a default value, an attribute immediately appears containing the default value and, if applicable, the corresponding namespace URI, local name, and prefix.
Example
The following example removes an attribute from an element.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlElement root = doc.DocumentElement;

    //Remove the genre attribute.
    root.RemoveAttribute("genre");
    
    Console.WriteLine("Display the modified XML...");
    Console.WriteLine(doc.InnerXml);

  }
}

    

Return to top


Overloaded Method: RemoveAttribute(
   string localName,
   string namespaceURI
)
Summary
Removes an attribute with the specified local name and namespace URI.

Note If the removed attribute has a default value, it is immediately replaced.
C# Syntax:
public virtual void RemoveAttribute(
   string localName,
   string namespaceURI
);
Parameters:

localName

The local name of the attribute to remove.

namespaceURI

The namespace URI of the attribute to remove.

Exceptions
Exception Type Condition
ArgumentException The node is read-only.
Remarks
If the removed attribute is known to have a default value, an attribute immediately appears containing the default value and, if applicable, the corresponding namespace URI, local name, and prefix.
Example
The following example removes an attribute from an element.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlElement root = doc.DocumentElement;

    //Remove the ISBN attribute.
    root.RemoveAttribute("ISBN", "urn:samples");
     
    Console.WriteLine("Display the modified XML...");
    Console.WriteLine(doc.InnerXml);

  }
}

    

Return to top


Method: RemoveAttributeAt(
   int i
)
Summary
Removes the attribute node with the specified index from the element.

Note If the removed attribute has a default value, it is immediately replaced.
C# Syntax:
public virtual XmlNode RemoveAttributeAt(
   int i
);
Parameters:

i

The index of the node to remove. The first node has index 0.

Return Value:
The attribute node removed or null if there is no node at the given index.
Remarks
This method is a Microsoft extension to the Document Object Model (DOM).
Example
The following example removes an attribute from an element.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlElement root = doc.DocumentElement;

    //Remove the genre attribute.
    root.RemoveAttributeAt(0);
    
    Console.WriteLine("Display the modified XML...");
    Console.WriteLine(doc.InnerXml);

  }
}

    

Return to top


Overloaded Method: RemoveAttributeNode(
   XmlAttribute oldAttr
)
Summary
Removes the specified XmlAttribute.
C# Syntax:
public virtual XmlAttribute RemoveAttributeNode(
   XmlAttribute oldAttr
);
Parameters:

oldAttr

The XmlAttribute node to remove. If the removed attribute has a default value, it is immediately replaced.

Return Value:
The removed XmlAttribute or null if oldAttr is not an attribute node of the XmlElement.
Exceptions
Exception Type Condition
ArgumentException This node is read-only.
Example

Return to top


Overloaded Method: RemoveAttributeNode(
   string localName,
   string namespaceURI
)
Summary
Removes the XmlAttribute specified by the local name and namespace URI.

Note If the removed attribute has a default value, it is immediately replaced.
C# Syntax:
public virtual XmlAttribute RemoveAttributeNode(
   string localName,
   string namespaceURI
);
Parameters:

localName

The local name of the attribute.

namespaceURI

The namespace URI of the attribute.

Return Value:
The removed XmlAttribute or null if the XmlElement does not have a matching attribute node.
Exceptions
Exception Type Condition
ArgumentException This node is read-only.
Example
The following example removes an attribute from an element.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlElement root = doc.DocumentElement;

    //Remove the ISBN attribute.
    root.RemoveAttributeNode("ISBN", "urn:samples");
     
    Console.WriteLine("Display the modified XML...");
    Console.WriteLine(doc.InnerXml);

  }
}

    

Return to top


Method: RemoveChild(
   XmlNode oldChild
)
Inherited
See base class member description: System.Xml.XmlNode.RemoveChild

Summary
Removes specified child node.
C# Syntax:
public virtual XmlNode RemoveChild(
   XmlNode oldChild
);
Parameters:

oldChild

The node being removed.

Return Value:
The node removed.
Exceptions
Exception Type Condition
ArgumentException The oldChild is not a child of this node. Or this node is read-only.
Remarks


Notes to inheritors: When overriding RemoveChild in a derived class, in order for events to be fired correctly, you must call the base class's RemoveChild method.
Example
The following example removes a node from the XML document.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlNode root = doc.DocumentElement;

    //Remove the title element.
    root.RemoveChild(root.FirstChild);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);

  }
}

    

Return to top


Method: ReplaceChild(
   XmlNode newChild,
   XmlNode oldChild
)
Inherited
See base class member description: System.Xml.XmlNode.ReplaceChild

Summary
Replaces the child node oldChild with newChild node.
C# Syntax:
public virtual XmlNode ReplaceChild(
   XmlNode newChild,
   XmlNode oldChild
);
Parameters:

newChild

The new node to put in the child list.

oldChild

The node being replaced in the list.

Return Value:
The node replaced.
Exceptions
Exception Type Condition
InvalidOperationException This node is of a type that does not allow children of the type of the newChild node. Or the node to put in is one of this node's ancestors.
ArgumentException The newChild was created from a different document than the one that created this node.

This node is read-only.

The oldChild is not a child of this node.

Remarks
If the newChild is already in the tree, it is first removed.

Notes to inheritors: When overriding ReplaceChild in a derived class, in order for events to be fired correctly, you must call the base class's ReplaceChild method.
Example
The following example replaces the title element in the XML document.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlNode root = doc.DocumentElement;

    //Create a new title element.
    XmlElement elem = doc.CreateElement("title");
    elem.InnerText="The Handmaid's Tale";

    //Replace the title element.
    root.ReplaceChild(elem, root.FirstChild);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);

  }
}

    
See also:
XmlDocument.ImportNode

Return to top


Overloaded Method: SelectNodes(
   string xpath
)
Inherited
See base class member description: System.Xml.XmlNode.SelectNodes

Summary
Selects a list of nodes matching the XPath expression.
C# Syntax:
public XmlNodeList SelectNodes(
   string xpath
);
Parameters:

xpath

The XPath expression.

Return Value:
An XmlNodeList containing a collection of nodes matching the XPath query.
Exceptions
Exception Type Condition
XPathException The XPath expression contains a prefix.
Remarks
If the XPath expression requires namespace resolution, you must use the SelectNodes overload which takes an XmlNamespaceManager as its argument. The XmlNamespaceManager is used to resolve namespaces.

Note If the XPath expression does not include a prefix, it is assumed that the namespace URI is the empty namespace. If your XML includes a default namespace, you must still use the XmlNamespaceManager and add a prefix and namespace URI to it; otherwise, you will not get any nodes selected.

This method is a Microsoft extension to the Document Object Model (DOM).

Example
The following example changes the price on all books by Jane Austen.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.Load("booksort.xml");

    XmlNodeList nodeList;
    XmlNode root = doc.DocumentElement;

    nodeList=root.SelectNodes("descendant::book[author/last-name='Austen']");
 
    //Change the price on the books.
    foreach (XmlNode book in nodeList)
    {
      book.LastChild.InnerText="15.95";
    }

    Console.WriteLine("Display the modified XML document....");
    doc.Save(Console.Out);
    
  }
}

    

The example uses the file, booksort.xml, as input.


<?xml version="1.0"?>
<!-- a fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
  <book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
    <title>Pride And Prejudice</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>24.95</price>
  </book>
  <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
    <title>The Handmaid's Tale</title>
    <author>
      <first-name>Margaret</first-name>
      <last-name>Atwood</last-name>
    </author>
    <price>29.95</price>
  </book>
  <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
    <title>Emma</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
  <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
    <title>Sense and Sensibility</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
</bookstore>

    
See also:
XmlNode.SelectSingleNode

Return to top


Overloaded Method: SelectNodes(
   string xpath,
   XmlNamespaceManager nsmgr
)
Inherited
See base class member description: System.Xml.XmlNode.SelectNodes

Summary
Selects a list of nodes matching the XPath expression. Any prefixes found in the XPath expression are resolved using the supplied XmlNamespaceManager.
C# Syntax:
public XmlNodeList SelectNodes(
   string xpath,
   XmlNamespaceManager nsmgr
);
Parameters:

xpath

The XPath expression.

nsmgr

An XmlNamespaceManager to use for resolving namespaces for prefixes in the XPath expression.

Return Value:
An XmlNodeList containing a collection of nodes matching the XPath query.
Exceptions
Exception Type Condition
XPathException The XPath expression contains a prefix which is not defined in the XmlNamespaceManager.
Remarks
XPath expressions can include namespaces. Namespace resolution is supported using the XmlNamespaceManager. If the XPath expression includes a prefix, the prefix and namespace URI pair must be added to the XmlNamespaceManager.

Note If the XPath expression does not include a prefix, it is assumed that the namespace URI is the empty namespace. If your XML includes a default namespace, you must still add a prefix and namespace URI to the XmlNamespaceManager; otherwise, you will not get any nodes selected.

For example, if you had the following XML:

              <bookstore xmlns="http://www.lucernepublishing.com">
               <book>
                 <title>Pride And Prejudice</title>
               </book>
              </bookstore>
                 
            

The following C# code selects all book nodes:

              XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
              nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");
              XmlNodeList nodelist = doc.SelectNodes("//ab:book", nsmgr);
                 
            

This method is a Microsoft extension to the Document Object Model (DOM).

Example
The following example displays the values of each of the ISBN attributes.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {

      XmlDocument doc = new XmlDocument();
      doc.Load("booksort.xml");

      //Create an XmlNamespaceManager for resolving namespaces.
      XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
      nsmgr.AddNamespace("bk", "urn:samples");

      //Select and display the value of all the ISBN attributes.
      XmlNodeList nodeList;
      XmlElement root = doc.DocumentElement;
      nodeList = root.SelectNodes("/bookstore/book/@bk:ISBN", nsmgr);
      foreach (XmlNode isbn in nodeList){
        Console.WriteLine(isbn.Value);
      }

   }

}

    
The example uses the file, booksort.xml, as input.

<?xml version="1.0"?>
<!-- a fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
  <book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
    <title>Pride And Prejudice</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>24.95</price>
  </book>
  <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
    <title>The Handmaid's Tale</title>
    <author>
      <first-name>Margaret</first-name>
      <last-name>Atwood</last-name>
    </author>
    <price>29.95</price>
  </book>
  <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
    <title>Emma</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
  <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
    <title>Sense and Sensibility</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
</bookstore>

    

Return to top


Overloaded Method: SelectSingleNode(
   string xpath
)
Inherited
See base class member description: System.Xml.XmlNode.SelectSingleNode

Summary
Selects the first XmlNode that matches the XPath expression.
C# Syntax:
public XmlNode SelectSingleNode(
   string xpath
);
Parameters:

xpath

The XPath expression.

Return Value:
The first XmlNode that matches the XPath query or null if no matching node was found.
Exceptions
Exception Type Condition
XPathException The XPath expression contains a prefix.
Remarks
If the XPath expression requires namespace resolution, you must use the SelectSingleNode overload which takes an XmlNamespaceManager as its argument. The XmlNamespaceManager is used to resolve namespaces.

Note If the XPath expression does not include a prefix, it is assumed that the namespace URI is the empty namespace. If your XML includes a default namespace, you must still use the XmlNamespaceManager and add a prefix and namespace URI to it; otherwise, you will not get a selected node.

This method is a Microsoft extension to the Document Object Model (DOM).

Example
The following example changes the price of the first Jane Austen book.


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

public class Sample
{
  public static void Main()
  {
    XmlDocument doc = new XmlDocument();
    doc.Load("booksort.xml");

    XmlNode book;
    XmlNode root = doc.DocumentElement;

    book=root.SelectSingleNode("descendant::book[author/last-name='Austen']");
 
    //Change the price on the book.
    book.LastChild.InnerText="15.95";

    Console.WriteLine("Display the modified XML document....");
    doc.Save(Console.Out);    
  }
}

    
The example uses the file, booksort.xml, as input.

<?xml version="1.0"?>
<!-- a fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
  <book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
    <title>Pride And Prejudice</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>24.95</price>
  </book>
  <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
    <title>The Handmaid's Tale</title>
    <author>
      <first-name>Margaret</first-name>
      <last-name>Atwood</last-name>
    </author>
    <price>29.95</price>
  </book>
  <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
    <title>Emma</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
  <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
    <title>Sense and Sensibility</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
</bookstore>

    
See also:
XmlNode.SelectNodes

Return to top


Overloaded Method: SelectSingleNode(
   string xpath,
   XmlNamespaceManager nsmgr
)
Inherited
See base class member description: System.Xml.XmlNode.SelectSingleNode

Summary
Selects the first XmlNode that matches the XPath expression. Any prefixes found in the XPath expression are resolved using the supplied XmlNamespaceManager.
C# Syntax:
public XmlNode SelectSingleNode(
   string xpath,
   XmlNamespaceManager nsmgr
);
Parameters:

xpath

The XPath expression.

nsmgr

An XmlNamespaceManager to use for resolving namespaces for prefixes in the XPath expression.

Return Value:
The first XmlNode that matches the XPath query or null if no matching node was found.
Exceptions
Exception Type Condition
XPathException The XPath expression contains a prefix which is not defined in the XmlNamespaceManager.
Remarks
XPath expressions can include namespaces. Namespace resolution is supported using the XmlNamespaceManager. If the XPath expression includes a prefix, the prefix and namespace URI pair must be added to the XmlNamespaceManager.

Note If the XPath expression does not include a prefix, it is assumed that the namespace URI is the empty namespace. If your XML includes a default namespace, you must still add a prefix and namespace URI to the XmlNamespaceManager; otherwise, you will not get a node selected.

For example, if you had the following XML:

              <bookstore xmlns="http://www.lucernepublishing.com">
               <book>
                 <title>Pride And Prejudice</title>
               </book>
              </bookstore>
                 
            

The following C# code selects the first book node:

              XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
              nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");
              XmlNode book = doc.SelectSingleNode("//ab:book", nsmgr);
                 
            

This method is a Microsoft extension to the Document Object Model (DOM).

Example
The following example selects the book with the matching ISBN value.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {

      XmlDocument doc = new XmlDocument();
      doc.Load("booksort.xml");

      //Create an XmlNamespaceManager for resolving namespaces.
      XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
      nsmgr.AddNamespace("bk", "urn:samples");

      //Select the book node with the matching attribute value.
      XmlNode book;
      XmlElement root = doc.DocumentElement;
      book = root.SelectSingleNode("descendant::book[@bk:ISBN='1-861001-57-6']", nsmgr);

      Console.WriteLine(book.OuterXml);

  }
}

    
The example uses the file, booksort.xml, as input.

<?xml version="1.0"?>
<!-- a fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
  <book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
    <title>Pride And Prejudice</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>24.95</price>
  </book>
  <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
    <title>The Handmaid's Tale</title>
    <author>
      <first-name>Margaret</first-name>
      <last-name>Atwood</last-name>
    </author>
    <price>29.95</price>
  </book>
  <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
    <title>Emma</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
  <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
    <title>Sense and Sensibility</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
</bookstore>

    

Return to top


Overloaded Method: SetAttribute(
   string name,
   string value
)
Summary
Sets the value of the attribute with the specified name.
C# Syntax:
public virtual void SetAttribute(
   string name,
   string value
);
Parameters:

name

The name of the attribute to create or alter. This is a qualified name. If the name contains a colon it is parsed into prefix and local name components.

value

The value to set for the attribute.

Exceptions
Exception Type Condition
XmlException The specified name contains an invalid character.
ArgumentException The node is read-only.
Remarks
If an attribute with the same name is already present in the element, its value is changed to that of value.value is a simple string. It is not parsed as it is being set. Any markup, such as syntax to be recognized as an entity reference, is treated as literal text and needs to be properly escaped by the implementation when it is written out. In order to assign an attribute value that contains entity references, the user must create an XmlAttribute node plus any XmlText and XmlEntityReference nodes, build the appropriate subtree and use XmlElement.SetAttributeNode to assign it as the value of an attribute.
Example
The following example adds an attribute to an element.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlElement root = doc.DocumentElement;

    //Add a new attribute.
    root.SetAttribute("genre", "novel");

    Console.WriteLine("Display the modified XML...");
    Console.WriteLine(doc.InnerXml);

  }
}

    

Return to top


Overloaded Method: SetAttribute(
   string localName,
   string namespaceURI,
   string value
)
Summary
Sets the value of the attribute with the specified local name and namespace URI.
C# Syntax:
public virtual string SetAttribute(
   string localName,
   string namespaceURI,
   string value
);
Parameters:

localName

The local name of the attribute.

namespaceURI

The namespace URI of the attribute.

value

The value to set for the attribute.

Example
The following example adds an attribute to an element.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlElement root = doc.DocumentElement;

    //Add a new attribute.
    root.SetAttribute("genre", "urn:samples", "novel");

    Console.WriteLine("Display the modified XML...");
    Console.WriteLine(doc.InnerXml);

  }
}

    

Return to top


Overloaded Method: SetAttributeNode(
   XmlAttribute newAttr
)
Summary
Adds the specified XmlAttribute.
C# Syntax:
public virtual XmlAttribute SetAttributeNode(
   XmlAttribute newAttr
);
Parameters:

newAttr

The XmlAttribute node to add to the attribute collection for this element.

Return Value:
If the attribute replaces an existing attribute with the same name, the old XmlAttribute is returned; otherwise, null is returned.
Exceptions
Exception Type Condition
ArgumentException The newAttr was created from a different document than the one that created this node. Or this node is read-only.
InvalidOperationException The newAttr is already an attribute of another XmlElement object. You must explicitly clone XmlAttribute nodes to re-use them in other XmlElement objects.
Remarks
If an attribute with that name is already present in the element, it is replaced by the new one.

Return to top


Overloaded Method: SetAttributeNode(
   string localName,
   string namespaceURI
)
Summary
Adds the specified XmlAttribute.
C# Syntax:
public virtual XmlAttribute SetAttributeNode(
   string localName,
   string namespaceURI
);
Parameters:

localName

The local name of the attribute.

namespaceURI

The namespace URI of the attribute.

Return Value:
The XmlAttribute to add.
Remarks
The XmlAttribute does not have any children. Use XmlAttribute.Value to assign a text value to the attribute or use XmlNode.AppendChild (or a similar method) to add children to the attribute.
Example
The following example adds an attribute to an element.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlElement root = doc.DocumentElement;

    //Add a new attribute.
    XmlAttribute attr = root.SetAttributeNode("genre", "urn:samples");
    attr.Value="novel";

    Console.WriteLine("Display the modified XML...");
    Console.WriteLine(doc.InnerXml);

  }
}

    

Return to top


Method: Supports(
   string feature,
   string version
)
Inherited
See base class member description: System.Xml.XmlNode.Supports

Summary
Test if the DOM implementation implements a specific feature.
C# Syntax:
public virtual bool Supports(
   string feature,
   string version
);
Parameters:

feature

The package name of the feature to test. This name is case-insensitive.

version

This is the version number of the package name to test. If the version is not specified (null), supporting any version of the feature will cause the method to return true.

Return Value:
true if the feature is implemented in the specified version; otherwise, false. The following table describes the combinations that return true.

Feature Version
XML 1.0
XML 2.0

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Overridden Method: WriteContentTo(
   XmlWriter w
)
Summary
Saves all the children of the node to the specified XmlWriter.
C# Syntax:
public override void WriteContentTo(
   XmlWriter w
);
Parameters:

w

The XmlWriter to which you want to save.

Remarks
This method is a Microsoft extension to the Document Object Model (DOM).
Example
The following example displays the contents of the element to the console.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlElement root = doc.DocumentElement;

    //Note that because WriteContentTo saves only the children of the element
    //to the writer none of the attributes are displayed.
    Console.WriteLine("Display the contents of the element...");
    XmlTextWriter writer = new XmlTextWriter(Console.Out);
    writer.Formatting = Formatting.Indented;
    root.WriteContentTo(writer);

  }
}

    
See also:
XmlElement.WriteTo

Return to top


Overridden Method: WriteTo(
   XmlWriter w
)
Summary
Saves the current node to the specified XmlWriter.
C# Syntax:
public override void WriteTo(
   XmlWriter w
);
Parameters:

w

The XmlWriter to which you want to save.

Remarks
This method is a Microsoft extension to the Document Object Model (DOM).
Example
The following example adds an attribute to an element and then displays the modified element.


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

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlElement root = doc.DocumentElement;

    //Add a new attribute.
    root.SetAttribute("genre", "urn:samples", "novel");

    Console.WriteLine("Display the modified XML...");
    XmlTextWriter writer = new XmlTextWriter(Console.Out);
    writer.Formatting = Formatting.Indented;
    root.WriteTo(writer);

  }
}

    

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.