System.Configuration.ConfigXmlDocument Class

Assembly: System.dll
Namespace: System.Configuration
This type supports the Shared Source CLI infrastructure and is not intended to be used directly from your code.
Summary
C# Syntax:
public sealed class ConfigXmlDocument : XmlDocument
See also:
System.Configuration Namespace

System.Configuration.ConfigXmlDocument Member List:

Public Constructors
ctor #1 Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Public Properties
Attributes
(inherited from System.Xml.XmlNode)
Read-only

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


Gets an XmlAttributeCollection containing the attributes of this node.
BaseURI
(inherited from System.Xml.XmlDocument)
Read-only

See base class member description: System.Xml.XmlDocument.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.
DocumentElement
(inherited from System.Xml.XmlDocument)
Read-only

See base class member description: System.Xml.XmlDocument.DocumentElement


Gets the root XmlElement for the document.
DocumentType
(inherited from System.Xml.XmlDocument)
Read-only

See base class member description: System.Xml.XmlDocument.DocumentType


Gets the node containing the DOCTYPE declaration.
Filename Read-only

FirstChild
(inherited from System.Xml.XmlNode)
Read-only

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


Gets the first child of the node.
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.
Implementation
(inherited from System.Xml.XmlDocument)
Read-only

See base class member description: System.Xml.XmlDocument.Implementation


Gets the XmlImplementation object for the current document.
InnerText
(inherited from System.Xml.XmlNode)
Read-write

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


Gets or sets the concatenated values of the node and all its children.
InnerXml
(inherited from System.Xml.XmlDocument)
Read-write

See base class member description: System.Xml.XmlDocument.InnerXml


Gets or sets the markup representing the children of the current node.
IsReadOnly
(inherited from System.Xml.XmlDocument)
Read-only

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


Gets a value indicating whether the current 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.
LineNumber Read-only

LocalName
(inherited from System.Xml.XmlDocument)
Read-only

See base class member description: System.Xml.XmlDocument.LocalName


Gets the local name of the node.
Name
(inherited from System.Xml.XmlDocument)
Read-only

See base class member description: System.Xml.XmlDocument.Name


Gets the qualified name of the node.
NamespaceURI
(inherited from System.Xml.XmlNode)
Read-only

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


Gets the namespace URI of this node.
NameTable
(inherited from System.Xml.XmlDocument)
Read-only

See base class member description: System.Xml.XmlDocument.NameTable


Gets the XmlNameTable associated with this implementation.
NextSibling
(inherited from System.Xml.XmlNode)
Read-only

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


Gets the node immediately following this node.
NodeType
(inherited from System.Xml.XmlDocument)
Read-only

See base class member description: System.Xml.XmlDocument.NodeType


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
(inherited from System.Xml.XmlDocument)
Read-only

See base class member description: System.Xml.XmlDocument.OwnerDocument


Gets the XmlDocument to which the current 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
(inherited from System.Xml.XmlNode)
Read-write

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


Gets or sets the namespace prefix of this node.
PreserveWhitespace
(inherited from System.Xml.XmlDocument)
Read-write

See base class member description: System.Xml.XmlDocument.PreserveWhitespace


Gets or sets a value indicating whether to preserve white space.
PreviousSibling
(inherited from System.Xml.XmlNode)
Read-only

See base class member description: System.Xml.XmlNode.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.
XmlResolver
(inherited from System.Xml.XmlDocument)
Write-only

See base class member description: System.Xml.XmlDocument.XmlResolver


Sets the XmlResolver to use for resolving external resources.
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
(inherited from System.Xml.XmlDocument)
See base class member description: System.Xml.XmlDocument.CloneNode


Creates a duplicate of this node.
CreateAttribute
(inherited from System.Xml.XmlDocument)
Overloaded:
CreateAttribute(string name)

See base class member description: System.Xml.XmlDocument.CreateAttribute


Creates an XmlAttribute with the specified XmlDocument.Name.
CreateAttribute
(inherited from System.Xml.XmlDocument)
Overloaded:
CreateAttribute(string qualifiedName, string namespaceURI)

See base class member description: System.Xml.XmlDocument.CreateAttribute


Creates an XmlAttribute with the specified qualified name and XmlNode.NamespaceURI.
CreateAttribute Overloaded:
CreateAttribute(string prefix, string localName, string namespaceUri)

Overridden:
CreateCDataSection Overridden:
CreateComment Overridden:
CreateDocumentFragment
(inherited from System.Xml.XmlDocument)
See base class member description: System.Xml.XmlDocument.CreateDocumentFragment


Creates an XmlDocumentFragment.
CreateDocumentType
(inherited from System.Xml.XmlDocument)
See base class member description: System.Xml.XmlDocument.CreateDocumentType


Returns a new XmlDocumentType object.
CreateElement
(inherited from System.Xml.XmlDocument)
Overloaded:
CreateElement(string name)

See base class member description: System.Xml.XmlDocument.CreateElement


Creates an element with the specified name.
CreateElement
(inherited from System.Xml.XmlDocument)
Overloaded:
CreateElement(string qualifiedName, string namespaceURI)

See base class member description: System.Xml.XmlDocument.CreateElement


Creates an XmlElement with the qualified name and XmlNode.NamespaceURI.
CreateElement Overloaded:
CreateElement(string prefix, string localName, string namespaceUri)

Overridden:
CreateEntityReference
(inherited from System.Xml.XmlDocument)
See base class member description: System.Xml.XmlDocument.CreateEntityReference


Creates an XmlEntityReference with the specified name.
CreateNavigator
(inherited from System.Xml.XmlNode)
Overloaded:
CreateNavigator()

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


Creates an XPathNavigator for navigating this object.
CreateNode
(inherited from System.Xml.XmlDocument)
Overloaded:
CreateNode(string nodeTypeString, string name, string namespaceURI)

See base class member description: System.Xml.XmlDocument.CreateNode


Creates an XmlNode with the specified node type, XmlDocument.Name, and XmlNode.NamespaceURI.
CreateNode
(inherited from System.Xml.XmlDocument)
Overloaded:
CreateNode(XmlNodeType type, string name, string namespaceURI)

See base class member description: System.Xml.XmlDocument.CreateNode


Creates an XmlNode with the specified XmlNodeType, XmlDocument.Name, and XmlNode.NamespaceURI.
CreateNode
(inherited from System.Xml.XmlDocument)
Overloaded:
CreateNode(XmlNodeType type, string prefix, string name, string namespaceURI)

See base class member description: System.Xml.XmlDocument.CreateNode


Creates a XmlNode with the specified XmlNodeType, XmlNode.Prefix, XmlDocument.Name, and XmlNode.NamespaceURI.
CreateProcessingInstruction
(inherited from System.Xml.XmlDocument)
See base class member description: System.Xml.XmlDocument.CreateProcessingInstruction


Creates an XmlProcessingInstruction with the specified name and data.
CreateSignificantWhitespace Overridden:
CreateTextNode Overridden:
CreateWhitespace Overridden:
CreateXmlDeclaration
(inherited from System.Xml.XmlDocument)
See base class member description: System.Xml.XmlDocument.CreateXmlDeclaration


Creates an XmlDeclaration node with the specified values.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

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


Gets the XmlElement with the specified ID.
GetElementsByTagName
(inherited from System.Xml.XmlDocument)
Overloaded:
GetElementsByTagName(string name)

See base class member description: System.Xml.XmlDocument.GetElementsByTagName


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

See base class member description: System.Xml.XmlDocument.GetElementsByTagName


Returns an XmlNodeList containing a list of all descendant elements that match the specified XmlDocument.LocalName and XmlNode.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.
ImportNode
(inherited from System.Xml.XmlDocument)
See base class member description: System.Xml.XmlDocument.ImportNode


Imports a node from another document to the current document.
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.
Load
(inherited from System.Xml.XmlDocument)
Overloaded:
Load(Stream inStream)

See base class member description: System.Xml.XmlDocument.Load


Loads the XML document from the specified stream.
Load Overloaded:
Load(string filename)

Overridden:
Load
(inherited from System.Xml.XmlDocument)
Overloaded:
Load(TextReader txtReader)

See base class member description: System.Xml.XmlDocument.Load


Loads the XML document from the specified TextReader.
Load
(inherited from System.Xml.XmlDocument)
Overloaded:
Load(XmlReader reader)

See base class member description: System.Xml.XmlDocument.Load


Loads the XML document from the specified XmlReader.
LoadSingleElement
LoadXml
(inherited from System.Xml.XmlDocument)
See base class member description: System.Xml.XmlDocument.LoadXml


Loads the XML document from the specified string.
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.
ReadNode
(inherited from System.Xml.XmlDocument)
See base class member description: System.Xml.XmlDocument.ReadNode


Creates an XmlNode object based on the information in the XmlReader. The reader must be positioned on a node or attribute.
RemoveAll
(inherited from System.Xml.XmlNode)
See base class member description: System.Xml.XmlNode.RemoveAll


Removes all the children and/or attributes of the current node.
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.
Save
(inherited from System.Xml.XmlDocument)
Overloaded:
Save(Stream outStream)

See base class member description: System.Xml.XmlDocument.Save


Saves the XML document to the specified stream.
Save
(inherited from System.Xml.XmlDocument)
Overloaded:
Save(string filename)

See base class member description: System.Xml.XmlDocument.Save


Saves the XML document to the specified file.
Save
(inherited from System.Xml.XmlDocument)
Overloaded:
Save(TextWriter writer)

See base class member description: System.Xml.XmlDocument.Save


Saves the XML document to the specified TextWriter.
Save
(inherited from System.Xml.XmlDocument)
Overloaded:
Save(XmlWriter w)

See base class member description: System.Xml.XmlDocument.Save


Saves the XML document to the specified XmlWriter.
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.
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
(inherited from System.Xml.XmlDocument)
See base class member description: System.Xml.XmlDocument.WriteContentTo


Saves all the children of the XmlDocument node to the specified XmlWriter.
WriteTo
(inherited from System.Xml.XmlDocument)
See base class member description: System.Xml.XmlDocument.WriteTo


Saves the XmlDocument node to the specified XmlWriter.
Public Events
NodeChanged
(inherited from System.Xml.XmlDocument)
See base class member description: System.Xml.XmlDocument.NodeChanged


Occurs when the XmlNode.Value of a node belonging to this document has been changed.
NodeChanging
(inherited from System.Xml.XmlDocument)
See base class member description: System.Xml.XmlDocument.NodeChanging


Occurs when the XmlNode.Value of a node belonging to this document is about to be changed.
NodeInserted
(inherited from System.Xml.XmlDocument)
See base class member description: System.Xml.XmlDocument.NodeInserted


Occurs when a node belonging to this document has been inserted into another node.
NodeInserting
(inherited from System.Xml.XmlDocument)
See base class member description: System.Xml.XmlDocument.NodeInserting


Occurs when a node belonging to this document is about to be inserted into another node.
NodeRemoved
(inherited from System.Xml.XmlDocument)
See base class member description: System.Xml.XmlDocument.NodeRemoved


Occurs when a node belonging to this document has been removed from its parent.
NodeRemoving
(inherited from System.Xml.XmlDocument)
See base class member description: System.Xml.XmlDocument.NodeRemoving


Occurs when a node belonging to this document is about to be removed from the document.
Protected Methods
CreateDefaultAttribute
(inherited from System.Xml.XmlDocument)
See base class member description: System.Xml.XmlDocument.CreateDefaultAttribute


Creates a default attribute with the specified prefix, local name and namespace URI.
CreateNavigator
(inherited from System.Xml.XmlDocument)
Overloaded:
CreateNavigator(XmlNode node)

See base class member description: System.Xml.XmlDocument.CreateNavigator


Creates an XPathNavigator object for navigating this document.
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.Configuration.ConfigXmlDocument Member Details

ctor #1
Summary:
Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public ConfigXmlDocument();

Return to top


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

Summary
Gets an XmlAttributeCollection containing the attributes of this node.
C# Syntax:
public virtual XmlAttributeCollection Attributes {get;}
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


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

Summary
Gets the base URI of the current node.
C# Syntax:
public override 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.

For Document nodes, BaseURI returns the location of the XML document. For example, if the XmlDocument was loaded using the following call doc.Load("http://server/mydata.xml"), the BaseURI for the document node is http://server/mydata.xml .

This property is a Microsoft extension to the Document Object Model (DOM). For additional information on BaseURI and how it behaves with other node types, see the XmlNode.BaseURI property in the XmlNode class.

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: DocumentElement (read-only)
Inherited
See base class member description: System.Xml.XmlDocument.DocumentElement

Summary
Gets the root XmlElement for the document.
C# Syntax:
public XmlElement DocumentElement {get;}
Example
The following example displays the root element of the XML document.


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

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

    //Display the document element.
    Console.WriteLine(doc.DocumentElement.OuterXml);
 }
}

    

Return to top


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

Summary
Gets the node containing the DOCTYPE declaration.
C# Syntax:
public virtual XmlDocumentType DocumentType {get;}
Remarks
An XmlDocument may have one and only one child with XmlNodeType equal to DocumentType.
Example
The following example gets and displays the DOCTYPE declaration for the document.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    // Create the XmlDocument.
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<!DOCTYPE book [<!ENTITY h 'hardcover'>]>" +
                "<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "<style>&h;</style>" +
                "</book>");

    // Display the DocumentType.
    Console.WriteLine(doc.DocumentType.OuterXml);

  }
}

    
See also:
XmlDocumentType

Return to top


Property: Filename (read-only)
This type supports the Shared Source CLI infrastructure and is not intended to be used directly from your code.
C# Syntax:
public string Filename {get;}

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: 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


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

Summary
Gets the XmlImplementation object for the current document.
C# Syntax:
public XmlImplementation Implementation {get;}
Remarks
XmlDocument objects created from the same XmlImplementation share the same XmlNameTable. This allows users to compare attribute and element names as objects rather than strings.

Although the XmlDocument objects share the same implementation, to move nodes from one document to another you must use the XmlDocument.ImportNode method.

Example
The following example creates a new XmlDocument using another document's implementation.
XmlDocument doc1 = new XmlDocument();
 doc1.Load("books.xml");
 XmlDocument doc2 = doc1.Implementation.CreateDocument();

    

Return to top


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

Summary
Gets or sets the concatenated values of the node and all its children.
C# Syntax:
public virtual string InnerText {get; set;}
Remarks
Setting this property replaces all the children with the parsed contents of the given string.

For leaf nodes, InnerText returns the same content as the XmlNode.Value property.

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

Example
The following example compares the InnerText and XmlNode.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>");

    XmlNode elem = 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: InnerXml (read-write)
Inherited
See base class member description: System.Xml.XmlDocument.InnerXml

Summary
Gets or sets the markup representing the children of the current 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.

InnerXml removes redundant namespace declarations. As a result, numerous cut and paste operations do not increase the size of your document with redundant namespace declarations. Consider the following XSL document:

              <xsl:stylesheet version="1.0" 
                   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                  <xsl:template match="stock">
                       ...
                  </xsl:template>
               </xsl:stylesheet>
            

The InnerXml property on the stylesheet node returns the following string:

              <xsl:template match="stock" 
                   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                   ...
               </xsl:template>
            

Notice the additional xmlns:xsl namespace declaration which is used to preserve the node identity. If you re-insert this inner XML string, you will get back your original document. In other words, InnerXml recognizes that the xmlns:xsl namespace declaration is redundant, given that the parent xsl:stylesheet element already has the xmlns:xsl namespace delcaration, and therefore removes it.

If you move InnerXml from a document with no default namespace to a document with a default namespace, the behavior is a little different. Consider the following XML string:

              <test>
                   <item>123</item>
               </test>
            

InnerXml returns a plain XML string with no namespace declarations:

              <item>123</item>
            

If you then insert this string into a document that does have a default namespace, such as the following:

              <test2 xmlns="urn:1">
               </test>
            

InnerXml parses the string in context, and the new nodes pick up the urn:1 namespace. The result looks like this:

              <test2 xmlns="urn:1">
                   <item>123</item>
               </test>
            

Now when you ask for the InnerXml you get back the following:

              <item xmlns="urn:1">123</item>
            

If you explicitly want the inserted item to preserve the fact that it came from a document that had no namespace then you need to manually add an xmlns= "" declaration and insert the resulting string:

              <item xmlns="">123</item>
            

The net effect of all this is as follows:

  1. Cutting and pasting InnerXml among documents that do not use namespaces is clean and simple and does not create "xmlns" redundancy in your strings.
  2. InnerXml can also be used to cut and paste between documents which have more than one namespace (i.e. XSL stylesheets).
  3. xmlns:xsl declarations automatically appear in your strings and disappear in your node hierarchies.
  4. In the case where you are moving InnerXml from a document that has no default namespace to a document that does have a default namespace, the new nodes will pick up the new default namespace.

If InnerXml is set with text containing entity references that are not currently defined in the document, the resulting tree will contain empty EntityReference nodes.

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

Return to top


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

Summary
Gets a value indicating whether the current node is read-only.
C# Syntax:
public override 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.

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

Example
The following example shows how to use the IsReadOnly property.


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

public class Sample
{
  public static void Main()
  {
    //Create the XmlDocument.
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<!DOCTYPE book [<!ENTITY h 'hardcover'>]>" +
                "<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "<style>&h;</style>" +
                "</book>");

    //Check if the node is read-only.
    if (doc.DocumentElement.LastChild.FirstChild.IsReadOnly)
       Console.WriteLine("Entity reference nodes are always read-only");

  }
}

    

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


Property: LineNumber (read-only)
This type supports the Shared Source CLI infrastructure and is not intended to be used directly from your code.
C# Syntax:
public int LineNumber {get;}

Return to top


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

Summary
Gets the local name of the node.
C# Syntax:
public override string LocalName {get;}
Remarks
The local name returned depends on the XmlDocument.NodeType of the node. To see a table describing the local name returned for each of the node types, see the XmlNode.LocalName property in the XmlNode class.

Return to top


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

Summary
Gets the qualified name of the node.
C# Syntax:
public override string Name {get;}
Remarks
The name returned depends on the XmlDocument.NodeType of the node. To see a table describing the name returned for each of the node types, see the XmlNode.Name property in the XmlNode class.

Return to top


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

Summary
Gets the namespace URI of this node.
C# Syntax:
public virtual 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">

An attribute does not inherit its namespace from the element it is attached to. If an attribute is not explicitly given a namespace, it simply has no namespace.

Return to top


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

Summary
Gets the XmlNameTable associated with this implementation.
C# Syntax:
public XmlNameTable NameTable {get;}
Remarks
Each XmlDocument object has a NameTable object. Element and attribute names are stored in the NameTable as atomized strings. This means that even if a name is referenced in the document multiple times it is stored only once in the NameTable. For example, if the document had multiple elements with the name "Customer", NameTable returns the same object whenever it receives a request for that name. As a result, users can write code using object comparisons on these strings rather than the more expensive string comparisons.

For more information on atomized strings, see XmlNameTable.

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

See also:
XmlNameTable

Return to top


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

Summary
Gets the node immediately following this node.
C# Syntax:
public virtual XmlNode NextSibling {get;}
Example
The following example displays all the books in the XML document.

 
 using System;
 using System.IO;
 using System.Xml;
 
 public class Sample
 {
   public static void Main()
   {
      Sample test = new Sample();
   }
 
   public Sample()
   {
     try
     {
       XmlDocument doc = new XmlDocument();
       doc.LoadXml("<?xml version=\"1.0\"?>" +
           "<!-- Sample XML document -->" +
           "<bookstore>" +
           "  <book genre=\"novel\" publicationdate=\"1997\" " +
           "        ISBN=\"1-861001-57-5\">" +
           "    <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\" " +
           "        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>" +
           "</bookstore>"); 
 
       XmlNode currNode = doc.DocumentElement;
 
       //print out books
       if (currNode.HasChildNodes){
          XmlNode book = currNode.FirstChild;
          Console.WriteLine(book.OuterXml + "\n");
          book = currNode.FirstChild.NextSibling;
          Console.WriteLine(book.OuterXml); 
       }
 
     }
     catch (Exception e)
     {
       Console.WriteLine ("Exception: {0}", e.ToString());
     }
 
   }
 }

    

Return to top


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

Summary
Gets the type of the current node.
C# Syntax:
public override XmlNodeType NodeType {get;}
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


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

Summary
Gets the XmlDocument to which the current node belongs.
C# Syntax:
public override XmlDocument OwnerDocument {get;}

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


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

Summary
Gets or sets the namespace prefix of this node.
C# Syntax:
public virtual 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 specified prefix is "xml" and the namespaceURI of this node is different from "http://www.w3.org/XML/1998/namespace".

This node is an attribute and the specified prefix is "xmlns" and the namespaceURI of this node is different from "http://www.w3.org/2000/xmlns/."

This node is an attribute and the qualifiedName of this node is "xmlns".

Remarks
Setting this property, when permitted, changes the XmlNode.Name property, which holds the qualified name, on Element and Attribute nodes. Setting this property on node types that cannot have a prefix (such as Text, Comment, EntityReference, CDATA, ProcessingInstruction, Document, and DocumentFragment) has no effect. Changing the prefix of an attribute that is known to have a default value, does not make a new attribute with the default value and the original prefix appear, since the namespace URI and local name do not change.

Notes to inheritors: When overriding Prefix in a derived class, this property will not be settable.

Return to top


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

Summary
Gets or sets a value indicating whether to preserve white space.
C# Syntax:
public bool PreserveWhitespace {get; set;}
Example
The following example shows how to strip white space from a file.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    //Load XML data which includes white space, but ignore
    //any white space in the file.
    XmlDocument doc = new XmlDocument();
    doc.PreserveWhitespace = false;
    doc.Load("book.xml");

    //Save the document as is (no white space).
    Console.WriteLine("Display the modified XML...");
    doc.PreserveWhitespace = true;
    doc.Save(Console.Out);

   }
}

    

The example uses the file book.xml as input.

<!--sample XML fragment-->
<book genre='novel' ISBN='1-861003-78' misc='sale-item'>
  <title>The Handmaid's Tale</title>
  <price>14.95</price>
</book>

    

Return to top


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

Summary
Gets the node immediately preceding this node.
C# Syntax:
public virtual XmlNode PreviousSibling {get;}
Example
The following example displays all the books in the XML document.

 
 using System;
 using System.IO;
 using System.Xml;
 
 public class Sample
 {
   public static void Main()
   {
      Sample test = new Sample();
   }
 
   public Sample()
   {
     try
     {
       XmlDocument doc = new XmlDocument();
       doc.LoadXml("<?xml version=\"1.0\"?>" +
           "<!-- Sample XML document -->" +
           "<bookstore>" +
           "  <book genre=\"novel\" publicationdate=\"1997\" " +
           "        ISBN=\"1-861001-57-5\">" +
           "    <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\" " +
           "        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>" +
           "</bookstore>"); 
 
       XmlNode currNode = doc.DocumentElement;
 
       //print out books in reverse order
       if (currNode.HasChildNodes){
          XmlNode book = currNode.LastChild;
          Console.WriteLine(book.OuterXml + "\n");
          book = currNode.LastChild.PreviousSibling;
          Console.WriteLine(book.OuterXml);
       }
 
     }
     catch (Exception e)
     {
       Console.WriteLine ("Exception: {0}", e.ToString());
     }
 
   }
 }

    

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


Property: XmlResolver (write-only)
Inherited
See base class member description: System.Xml.XmlDocument.XmlResolver

Summary
Sets the XmlResolver to use for resolving external resources.
C# Syntax:
XmlResolver XmlResolver {set;}
Exceptions
Exception Type Condition
XmlException This property is set to null and an external DTD or entity is encountered.
Remarks
The XmlResolver can be used to load DTDs or expand entity references. Using the XmlResolver.Credentials property, you can set credentials on the XmlResolver to access resources stored on a secure network resource.

If an XmlResolver is not specified, a default XmlUrlResolver with default credentials is used.



Note If the XmlDocument is loaded using an XmlReader which had an XmlResolver set to it, the XmlResolver on the XmlReader is not cached by the XmlDocument after XmlDocument.Load completes.

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

See also:
XmlUrlResolver.Credentials

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


Method: CloneNode(
   bool deep
)
Inherited
See base class member description: System.Xml.XmlDocument.CloneNode

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.

Return Value:
The cloned XmlDocument node.
Remarks
This method serves as a copy constructor for nodes. The cloned node has no parent ( XmlNode.ParentNode returns null).

If deep is true, the cloned node includes all the child nodes, otherwise only the XmlDocument node is cloned. See the XmlNode.CloneNode property in the XmlNode class to see how it behaves on other node types.

Example
The following example shows the difference between a deep and shallow clone.


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

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

    //Create a deep clone.  The cloned node 
    //includes the child node.
    XmlDocument deep = (XmlDocument) doc.CloneNode(true);
    Console.WriteLine(deep.ChildNodes.Count);

    //Create a shallow clone.  The cloned node does not 
    //include the child node.
    XmlDocument shallow = (XmlDocument) doc.CloneNode(false);
    Console.WriteLine(shallow.Name + shallow.OuterXml);
    Console.WriteLine(shallow.ChildNodes.Count);
    
  }
}

    

Return to top


Overloaded Method: CreateAttribute(
   string name
)
Inherited
See base class member description: System.Xml.XmlDocument.CreateAttribute

Summary
Creates an XmlAttribute with the specified XmlDocument.Name.
C# Syntax:
public XmlAttribute CreateAttribute(
   string name
);
Parameters:

name

The qualified name of the attribute. If the name contains a colon, the XmlNode.Prefix property reflects the part of the name preceding the first colon and the XmlDocument.LocalName property reflects the part of the name following the first colon. The XmlNode.NamespaceURI remains empty unless the prefix is a recognized built-in prefix such as xmlns. In this case NamespaceURI has a value of http://www.w3.org/2000/xmlns/ .

Return Value:
The new XmlAttribute.
Remarks
The XmlAttribute can be added to an XmlElement using the XmlElement.SetAttributeNode method.
Example
The following creates an attribute and adds it to 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>");

    //Create an attribute.
    XmlAttribute attr = doc.CreateAttribute("publisher");
    attr.Value = "WorldWide Publishing";
          
    //Add the new node to the document. 
    doc.DocumentElement.SetAttributeNode(attr);
        
    Console.WriteLine("Display the modified XML...");        
    doc.Save(Console.Out);
  }
}

    

Return to top


Overloaded Method: CreateAttribute(
   string qualifiedName,
   string namespaceURI
)
Inherited
See base class member description: System.Xml.XmlDocument.CreateAttribute

Summary
Creates an XmlAttribute with the specified qualified name and XmlNode.NamespaceURI.
C# Syntax:
public XmlAttribute CreateAttribute(
   string qualifiedName,
   string namespaceURI
);
Parameters:

qualifiedName

The qualified name of the attribute. If the name contains a colon then the XmlNode.Prefix property will reflect the part of the name preceding the colon and the XmlDocument.LocalName property will reflect the part of the name after the colon.

namespaceURI

The namespaceURI of the attribute. If the qualified name includes a prefix of xmlns, then this parameter must be http://www.w3.org/2000/xmlns/ .

Return Value:
The new XmlAttribute.
Remarks
The XmlAttribute can be added to an XmlElement using the XmlElement.SetAttributeNode method.

Return to top


Overloaded Method: CreateAttribute(
   string prefix,
   string localName,
   string namespaceUri
)
This type supports the Shared Source CLI infrastructure and is not intended to be used directly from your code.
C# Syntax:
public override XmlAttribute CreateAttribute(
   string prefix,
   string localName,
   string namespaceUri
);
Parameters:

prefix

localName

namespaceUri

Return to top


Overridden Method: CreateCDataSection(
   string data
)
This type supports the Shared Source CLI infrastructure and is not intended to be used directly from your code.
C# Syntax:
public override XmlCDataSection CreateCDataSection(
   string data
);
Parameters:

data

Return to top


Overridden Method: CreateComment(
   string data
)
This type supports the Shared Source CLI infrastructure and is not intended to be used directly from your code.
C# Syntax:
public override XmlComment CreateComment(
   string data
);
Parameters:

data

Return to top


Method: CreateDefaultAttribute(
   string prefix,
   string localName,
   string namespaceURI
)
Inherited
See base class member description: System.Xml.XmlDocument.CreateDefaultAttribute

Summary
Creates a default attribute with the specified prefix, local name and namespace URI.
C# Syntax:
protected internal virtual XmlAttribute CreateDefaultAttribute(
   string prefix,
   string localName,
   string namespaceURI
);
Parameters:

prefix

The prefix of the attribute (if any).

localName

The local name of the attribute.

namespaceURI

The namespace URI of the attribute (if any).

Return Value:
The new XmlAttribute.
Remarks
This method is a Microsoft extension to the Document Object Model (DOM).

Return to top


Method: CreateDocumentFragment()
Inherited
See base class member description: System.Xml.XmlDocument.CreateDocumentFragment

Summary
Creates an XmlDocumentFragment.
C# Syntax:
public virtual XmlDocumentFragment CreateDocumentFragment();
Return Value:
The new XmlDocumentFragment.
Remarks
DocumentFragment nodes cannot be inserted into a document. However, you can insert children of the DocumentFragment node into a document.
Example
The following example adds new nodes to an XML document.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    //Create the XmlDocument.
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<items/>");

    //Create a document fragment.
    XmlDocumentFragment docFrag = doc.CreateDocumentFragment();
 
    //Set the contents of the document fragment.
    docFrag.InnerXml ="<item>widget</item>";

   //Add the children of the document fragment to the
   //original document.
   doc.DocumentElement.AppendChild(docFrag);

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

    

Return to top


Method: CreateDocumentType(
   string name,
   string publicId,
   string systemId,
   string internalSubset
)
Inherited
See base class member description: System.Xml.XmlDocument.CreateDocumentType

Summary
Returns a new XmlDocumentType object.
C# Syntax:
public virtual XmlDocumentType CreateDocumentType(
   string name,
   string publicId,
   string systemId,
   string internalSubset
);
Parameters:

name

Name of the document type.

publicId

The public identifier of the document type or null.

systemId

The system identifier of the document type or null.

internalSubset

The DTD internal subset of the document type or null.

Return Value:
The new XmlDocumentType.
Remarks
The returned node will have parsed XmlDocumentType.Entities and XmlDocumentType.Notations collections.

Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. To add the new object, you must explicitly call one of the node insert methods.

According to the W3C Extensible Markup Language (XML) 1.0 recommendation (www.w3.org/TR/1998/REC-xml-19980210), DocumentType nodes are only allowed within Document nodes. Each XmlDocument can have only one DocumentType node. The DocumentType node must also be inserted before the root element of the XmlDocument (i.e if the document already has a root element, you cannot add a DocumentType node).

If the passed parameters do not combine to build a valid XmlDocumentType, an exception is thrown.

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

Example
The following example creates a DocumentType node and adds it to an XML document.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    //Create the XmlDocument.
    XmlDocument doc = new XmlDocument();

    //Create a document type node and  
    //add it to the document.
    XmlDocumentType doctype;
    doctype = doc.CreateDocumentType("book", null, null, "<!ELEMENT book ANY>");
    doc.AppendChild(doctype);

    //Create the root element and 
    //add it to the document.
    doc.AppendChild(doc.CreateElement("book"));

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

    

Return to top


Overloaded Method: CreateElement(
   string name
)
Inherited
See base class member description: System.Xml.XmlDocument.CreateElement

Summary
Creates an element with the specified name.
C# Syntax:
public XmlElement CreateElement(
   string name
);
Parameters:

name

The qualified name of the element. If the name contains a colon then the XmlNode.Prefix property reflects the part of the name preceding the colon and the XmlDocument.LocalName property reflects the part of the name after the colon. The qualified name cannot include a prefix of'xmlns'.

Return Value:
The new XmlElement.
Remarks
Note that the instance returned implements the XmlElement interface, so default attributes would be created directly on the returned object.

Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. To add the new object, you must explicitly call one of the node insert methods.

According to the W3C Extensible Markup Language (XML) 1.0 recommendation (www.w3.org/TR/1998/REC-xml-19980210), Element nodes are allowed within Document and Element nodes, and in EntityReference nodes when the EntityReference node is not a child of an Attribute node.

Example
The following example creates a new element and adds it to the document.


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

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

    //Create a new node and add it to the document.
    //The text node is the content of the price element.
    XmlElement elem = doc.CreateElement("price");
    XmlText text = doc.CreateTextNode("19.95");
    doc.DocumentElement.AppendChild(elem);
    doc.DocumentElement.LastChild.AppendChild(text);

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

  }
}

    

Return to top


Overloaded Method: CreateElement(
   string qualifiedName,
   string namespaceURI
)
Inherited
See base class member description: System.Xml.XmlDocument.CreateElement

Summary
Creates an XmlElement with the qualified name and XmlNode.NamespaceURI.
C# Syntax:
public XmlElement CreateElement(
   string qualifiedName,
   string namespaceURI
);
Parameters:

qualifiedName

The qualified name of the element. If the name contains a colon then the XmlNode.Prefix property will reflect the part of the name preceding the colon and the XmlDocument.LocalName property will reflect the part of the name after the colon. The qualified name cannot include a prefix of'xmlns'.

namespaceURI

The namespace URI of the element.

Return Value:
The new XmlElement.
Remarks
The following C# code
              XmlElement elem;
              elem=doc.CreateElement("item:bar", "urn:abc");
                 
            

results in an element that is equivalent to the following XML text.

<item:bar xmlns:item="urn:abc"/>

Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. To add the new object, you must explicitly call one of the node insert methods.

According to the W3C Extensible Markup Language (XML) 1.0 recommendation (www.w3.org/TR/1998/REC-xml-19980210), Element nodes are allowed within Document and Element nodes, and in EntityReference nodes when the EntityReference node is not a child of an Attribute node.

Return to top


Overloaded Method: CreateElement(
   string prefix,
   string localName,
   string namespaceUri
)
This type supports the Shared Source CLI infrastructure and is not intended to be used directly from your code.
C# Syntax:
public override XmlElement CreateElement(
   string prefix,
   string localName,
   string namespaceUri
);
Parameters:

prefix

localName

namespaceUri

Return to top


Method: CreateEntityReference(
   string name
)
Inherited
See base class member description: System.Xml.XmlDocument.CreateEntityReference

Summary
Creates an XmlEntityReference with the specified name.
C# Syntax:
public virtual XmlEntityReference CreateEntityReference(
   string name
);
Parameters:

name

The name of the entity reference.

Return Value:
The new XmlEntityReference.
Exceptions
Exception Type Condition
ArgumentException The name is invalid (for example, names starting with'#' are invalid.)
Remarks
If the referenced entity is known, the child list of the XmlEntityReference node is made the same as that of the corresponding XmlEntity node.

The namespaces used in the replacement text for the entity reference are bound at the time the parent of the entity reference node is first set (for example, when the entity reference node is inserted into the document). For example, given the following entity:

<!ENTITY a "<b>test</b>">

If you call CreateEntityReference("a") you get back a single node of type EntityReference with no children. If you append this node as a child of the following node,

<item xmlns="urn:1"/>

then, at the time of calling XmlNode.AppendChild, the parent of the newly created entity reference node is set and the children are expanded in this namespace context. The child element node b will have NamespaceURI equal to urn:1. The child nodes of the entity reference remain the same even if you move the entity reference to a place in the document that has a different default namespace context. This does not happen for existing entity reference nodes when you remove and insert them or for entity references that you clone with XmlDocument.CloneNode. It only happens for newly created entity references.

If the corresponding entity is not defined in the DocumentType when the entity reference node is added, because the entity reference is not defined, its only child node will be an empty text node.

The built-in entities amp, lt, gt, apos, and quot are also allowed, and they will have a child text node with the appropriate expanded character value.

Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. To add the new object, you must explicitly call one of the node insert methods.

According to the W3C Extensible Markup Language (XML) 1.0 recommendation (www.w3.org/TR/1998/REC-xml-19980210), EntityReference nodes are only allowed within Element, Attribute and EntityReference nodes.

Example
The following example creates two entity reference nodes and inserts them into an XML document.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    //Create the XmlDocument.
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<!DOCTYPE book [<!ENTITY h 'hardcover'>]>" +
                "<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "<misc/>" +
                "</book>"); 

    //Create an entity reference node. The child count should be 0 
    //since the node has not been expanded.
    XmlEntityReference entityref = doc.CreateEntityReference("h");
    Console.WriteLine(entityref.ChildNodes.Count ); 

    //After the the node has been added to the document, its parent node
    //is set and the entity reference node is expanded.  It now has a child
    //node containing the entity replacement text. 
    doc.DocumentElement.LastChild.AppendChild(entityref);
    Console.WriteLine(entityref.FirstChild.InnerText);

    //Create and insert an undefined entity reference node.  When the entity
    //reference node is expanded, because the entity reference is undefined
    //the child is an empty text node.
    XmlEntityReference entityref2 = doc.CreateEntityReference("p");
    doc.DocumentElement.LastChild.AppendChild(entityref2);
    Console.WriteLine(entityref2.FirstChild.InnerText);
    
  }
}

    

Return to top


Overloaded 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


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

Summary
Creates an XPathNavigator object for navigating this document.
C# Syntax:
protected internal virtual XPathNavigator CreateNavigator(
   XmlNode node
);
Parameters:

node

The XmlNode you want the navigator initially positioned on.

Return Value:
An XPathNavigator object.

Return to top


Overloaded Method: CreateNode(
   string nodeTypeString,
   string name,
   string namespaceURI
)
Inherited
See base class member description: System.Xml.XmlDocument.CreateNode

Summary
Creates an XmlNode with the specified node type, XmlDocument.Name, and XmlNode.NamespaceURI.
C# Syntax:
public virtual XmlNode CreateNode(
   string nodeTypeString,
   string name,
   string namespaceURI
);
Parameters:

nodeTypeString

String version of the XmlNodeType of the new node. This parameter must be one of the values listed in the table below.

name

The qualified name of the new node. If the name contains a colon, it is parsed into XmlNode.Prefix and XmlDocument.LocalName components.

namespaceURI

The namespace URI of the new node.

Return Value:
The new XmlNode.
Exceptions
Exception Type Condition
ArgumentException The name was not provided and the XmlNodeType requires a name; or nodeTypeString is not one of the strings listed below.
Remarks
The nodeTypeString parameter is case sensitive and must be one of the values in the following table.

nodeTypeString XmlNodeType
attribute Attribute
cdatasection CDATA
comment Comment
document Document
documentfragment DocumentFragment
documenttype DocumentType
element Element
entityreference EntityReference
processinginstruction ProcessingInstruction
significantwhitespace SignificantWhitespace
text Text
whitespace Whitespace

Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. To add the new object, you must explicitly call one of the node insert methods.

The following table shows you what NodeType[row] is allowed inside another NodeType[column] according to the W3C Extensible Markup Language (XML) 1.0 recommendation (www.w3.org/TR/1998/REC-xml-19980210 ).



Document DocumentType XmlDeclaration Element Attribute Text CDATA Markup EntityReference
Document no no no no no no no no no
DocumentType yes no no no no no no no no
XmlDeclaration yes* no no no no no no no no
Element yes no no yes no no no no yes***
Attribute no no no yes**** no no no no no
Text no no no yes yes no no no yes
CDATA no no no yes no no no no yes***
Markup** yes no no yes no no no no no
EntityReference no no no yes yes no no no yes

* The XmlDeclaration node must be the first child of the Document node.

** Markup includes ProcessingInstruction and Comment nodes.

*** Element and CDATA nodes are only allowed in EntityReference nodes when the EntityReference node is not a child of an Attribute node.

**** Attributes are not children of an Element node. Attributes are contained inside an attribute collection that belongs to an Element node.

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

Example
The following example creates a new element and inserts it into the document.

 
 using System;
 using System.IO;
 using System.Xml;
 
 public class Sample
 {
   public static void Main()
   {
     Sample test = new Sample();
   }
   public Sample()
   {
     try
     {
       XmlDocument doc = new XmlDocument();
       doc.LoadXml("<book genre=\"novel\" publicationdate=\"1997\" " +
                   "      ISBN=\"1-861001-57-5\">" +
                   "  <title>Pride And Prejudice</title>" +
                   "  <author>" +
                   "    <first-name>Jane</first-name>" +
                   "    <last-name>Austen</last-name>" +
                   "  </author>" +
                   "  <price>24.95</price>" +
                   "</book>"); 
 
       XmlNode newElem=doc.CreateNode("element", "pages", "");  
       newElem.InnerText = "290";
     
       //add the new node to the document 
       Console.WriteLine("Add the new element to the existing document...");
       XmlElement root = doc.DocumentElement;
       root.AppendChild(newElem);
     
       Console.WriteLine("Display the modified xml document...");
       Console.WriteLine(doc.OuterXml);
 
     }
     catch (Exception e)
     {
       Console.WriteLine ("Exception: {0}", e.ToString());
     }
 
   }
 }

    

Return to top


Overloaded Method: CreateNode(
   XmlNodeType type,
   string name,
   string namespaceURI
)
Inherited
See base class member description: System.Xml.XmlDocument.CreateNode

Summary
Creates an XmlNode with the specified XmlNodeType, XmlDocument.Name, and XmlNode.NamespaceURI.
C# Syntax:
public virtual XmlNode CreateNode(
   XmlNodeType type,
   string name,
   string namespaceURI
);
Parameters:

type

The XmlNodeType of the new node.

name

The qualified name of the new node. If the name contains a colon then it is parsed into XmlNode.Prefix and XmlDocument.LocalName components.

namespaceURI

The namespace URI of the new node.

Return Value:
The new XmlNode.
Exceptions
Exception Type Condition
ArgumentException The name was not provided and the XmlNodeType requires a name.
Remarks
Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. To add the new object, you must explicitly call one of the node insert methods.

The following table shows you what NodeType[row] is allowed inside another NodeType[column] according to the W3C Extensible Markup Language (XML) 1.0 recommendation (www.w3.org/TR/1998/REC-xml-19980210 ).



Document DocumentType XmlDeclaration Element Attribute Text CDATA Markup EntityReference
Document no no no no no no no no no
DocumentType yes no no no no no no no no
XmlDeclaration yes* no no no no no no no no
Element yes no no yes no no no no yes***
Attribute no no no yes**** no no no no no
Text no no no yes yes no no no yes
CDATA no no no yes no no no no yes***
Markup** yes no no yes no no no no no
EntityReference no no no yes yes no no no yes

* The XmlDeclaration node must be the first child of the Document node.

** Markup includes ProcessingInstruction and Comment nodes.

*** Element and CDATA nodes are only allowed in EntityReference nodes when the EntityReference node is not a child of an Attribute node.

**** Attributes are not children of an Element node. Attributes are contained inside an attribute collection that belongs to an Element node.

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

Example
The following example creates a new element and inserts it into an XML document.


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

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

    //Create a new node and add it to the document.
    XmlNode elem = doc.CreateNode(XmlNodeType.Element, "price", null);
    elem.InnerText = "19.95";
    doc.DocumentElement.AppendChild(elem); 
    
    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);

  }
}

    

Return to top


Overloaded Method: CreateNode(
   XmlNodeType type,
   string prefix,
   string name,
   string namespaceURI
)
Inherited
See base class member description: System.Xml.XmlDocument.CreateNode

Summary
Creates a XmlNode with the specified XmlNodeType, XmlNode.Prefix, XmlDocument.Name, and XmlNode.NamespaceURI.
C# Syntax:
public virtual XmlNode CreateNode(
   XmlNodeType type,
   string prefix,
   string name,
   string namespaceURI
);
Parameters:

type

The XmlNodeType of the new node.

prefix

The prefix of the new node.

name

The local name of the new node.

namespaceURI

The namespace URI of the new node.

Return Value:
The new XmlNode.
Exceptions
Exception Type Condition
ArgumentException The name was not provided and the XmlNodeType requires a name.
Remarks
Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. To add the new object, you must explicitly call one of the node insert methods.

The following table shows you what NodeType[row] is allowed inside another NodeType[column] according to the W3C Extensible Markup Language (XML) 1.0 recommendation (www.w3.org/TR/1998/REC-xml-19980210 ).



Document DocumentType XmlDeclaration Element Attribute Text CDATA Markup EntityReference
Document no no no no no no no no no
DocumentType yes no no no no no no no no
XmlDeclaration yes* no no no no no no no no
Element yes no no yes no no no no yes***
Attribute no no no yes**** no no no no no
Text no no no yes yes no no no yes
CDATA no no no yes no no no no yes***
Markup** yes no no yes no no no no no
EntityReference no no no yes yes no no no yes

* The XmlDeclaration node must be the first child of the Document node.

** Markup includes ProcessingInstruction and Comment nodes.

*** Element and CDATA nodes are only allowed in EntityReference nodes when the EntityReference node is not a child of an Attribute node.

**** Attributes are not children of an Element node. Attributes are contained inside an attribute collection that belongs to the Element node.

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

Example
The following example adds a new element to the document.

 
 using System;
 using System.IO;
 using System.Xml;
 
 public class Sample
 {
   public static void Main()
   {
     Sample test = new Sample();
   }
   public Sample()
   {
     try
     {
       XmlDocument doc = new XmlDocument();
       doc.LoadXml("<book genre=\"novel\" publicationdate=\"1997\" " +
                   "      ISBN=\"1-861001-57-5\">" +
                   "  <title>Pride And Prejudice</title>" +
                   "  <author>" +
                   "    <first-name>Jane</first-name>" +
                   "    <last-name>Austen</last-name>" +
                   "  </author>" +
                   "  <price>24.95</price>" +
                   "</book>"); 
 
       XmlNode newElem=doc.CreateNode(XmlNodeType.Element, "pages", "");  
       newElem.InnerText = "290";
     
       //add the new node to the document 
       Console.WriteLine("Add the new element to the existing document...");
       XmlElement root = doc.DocumentElement;
       root.AppendChild(newElem);
     
       Console.WriteLine("Display the modified xml document...");
       Console.WriteLine(doc.OuterXml);
 
     }
     catch (Exception e)
     {
       Console.WriteLine ("Exception: {0}", e.ToString());
     }
 
   }
 }

    

Return to top


Method: CreateProcessingInstruction(
   string target,
   string data
)
Inherited
See base class member description: System.Xml.XmlDocument.CreateProcessingInstruction

Summary
Creates an XmlProcessingInstruction with the specified name and data.
C# Syntax:
public virtual XmlProcessingInstruction CreateProcessingInstruction(
   string target,
   string data
);
Parameters:

target

The name of the processing instruction.

data

The data for the processing instruction.

Return Value:
The new XmlProcessingInstruction.
Remarks
Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. To add the new object, you must explicitly call one of the node insert methods. According to the W3C Extensible Markup Language (XML) 1.0 recommendation (www.w3.org/TR/1998/REC-xml-19980210), ProcessingInstruction nodes are only allowed within Document, Element, and EntityReference nodes, when the EntityReference node is not a child of an Attribute node.
Example
The following example creates a ProcessingInstruction node and adds it to the document.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    XmlDocument doc = new XmlDocument();

    // Create a procesing instruction.
    XmlProcessingInstruction newPI;
    String PItext = "type='text/xsl' href='book.xsl'";
    newPI = doc.CreateProcessingInstruction("xml-stylesheet", PItext);

    // Display the target and data information.
    Console.WriteLine("<?{0} {1}?>", newPI.Target, newPI.Data);

    // Add the processing instruction node to the document.
    doc.AppendChild(newPI);

  }
}

    

Return to top


Overridden Method: CreateSignificantWhitespace(
   string data
)
This type supports the Shared Source CLI infrastructure and is not intended to be used directly from your code.
C# Syntax:
public override XmlSignificantWhitespace CreateSignificantWhitespace(
   string data
);
Parameters:

data

Return to top


Overridden Method: CreateTextNode(
   string text
)
This type supports the Shared Source CLI infrastructure and is not intended to be used directly from your code.
C# Syntax:
public override XmlText CreateTextNode(
   string text
);
Parameters:

text

Return to top


Overridden Method: CreateWhitespace(
   string data
)
This type supports the Shared Source CLI infrastructure and is not intended to be used directly from your code.
C# Syntax:
public override XmlWhitespace CreateWhitespace(
   string data
);
Parameters:

data

Return to top


Method: CreateXmlDeclaration(
   string version,
   string encoding,
   string standalone
)
Inherited
See base class member description: System.Xml.XmlDocument.CreateXmlDeclaration

Summary
Creates an XmlDeclaration node with the specified values.
C# Syntax:
public virtual XmlDeclaration CreateXmlDeclaration(
   string version,
   string encoding,
   string standalone
);
Parameters:

version

The version must be "1.0".

encoding

The value of the encoding attribute. This is the encoding that is used when you save the XmlDocument to a file or a stream; therefore, it must be set to a string supported by the Encoding class, otherwise XmlDocument.Save fails. If this is null or String.Empty, the Save method does not write an encoding attribute on the XML declaration and therefore the default encoding, UTF-8, is used.

Note: If the XmlDocument is saved to either a TextWriter or an XmlTextWriter, this encoding value is discarded. Instead, the encoding of the TextWriter or the XmlTextWriter is used. This ensures that the XML written out can be read back using the correct encoding.

The value of the encoding attribute. This is the encoding that is used when you save the XmlDocument to a file or a stream; therefore, it must be set to a string supported by the Encoding class, otherwise XmlDocument.Save fails. If this is null or String.Empty, the Save method does not write an encoding attribute on the XML declaration and therefore the default encoding, UTF-8, is used.

Note: If the XmlDocument is saved to either a TextWriter or an XmlTextWriter, this encoding value is discarded. Instead, the encoding of the TextWriter or the XmlTextWriter is used. This ensures that the XML written out can be read back using the correct encoding.

standalone

The value must be either "yes" or "no". If this is null or String.Empty, the Save method does not write a standalone attribute on the XML declaration.

Return Value:
The new XmlDeclaration node.
Exceptions
Exception Type Condition
ArgumentException The values of version or standalone are something other than the ones specified above.
Remarks
The attributes are exposed as special properties on the XmlDeclaration node, and not as XmlAttribute nodes.

Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. To add the new object, you must explicitly call one of the node insert methods.

According to the W3C Extensible Markup Language (XML) 1.0 recommendation (www.w3.org/TR/1998/REC-xml-19980210), the XmlDeclaration node must be the first node in the document.

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

Example
The following example creates an XML declaration and adds it to the 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>");

    //Create an XML declaration. 
    XmlDeclaration xmldecl;
    xmldecl = doc.CreateXmlDeclaration("1.0",null,null);

    //Add the new node to the document.
    XmlElement root = doc.DocumentElement;
    doc.InsertBefore(xmldecl, root);
        
    Console.WriteLine("Display the modified XML...");        
    doc.Save(Console.Out);
  }
}

    
See also:
XmlDeclaration

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:
~ConfigXmlDocument();

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

Return to top


Method: GetElementById(
   string elementId
)
Inherited
See base class member description: System.Xml.XmlDocument.GetElementById

Summary
Gets the XmlElement with the specified ID.
C# Syntax:
public virtual XmlElement GetElementById(
   string elementId
);
Parameters:

elementId

The attribute ID to match.

Return Value:
The XmlElement with the matching ID or null if no matching element is found.
Remarks
If the document has multiple elements with the matching ID, this method returns the first matching element in the document.

Note The DOM implementation must have information which defines which attributes are of type ID. Although attributes of type ID can be defined in either XSD schemas or DTDs, this version of the product only supports those defined in DTDs. Attributes with the name "ID" are not of type ID unless so defined in the DTD. Implementations where it is unknown whether the attributes are of type ID are expected to return null.
Example
The following example uses the GetElementById method.

using System;
using System.Xml;

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

    //Get the first element with an attribute of type ID and value of A111.
    //This displays the node <Person SSN="A111" Name="Fred"/>.
    XmlElement elem = doc.GetElementById("A111");
    Console.WriteLine( elem.OuterXml );

    //Get the first element with an attribute of type ID and value of A222.
    //This displays the node <Person SSN="A222" Name="Tom"/>.
    elem = doc.GetElementById("A222");
    Console.WriteLine( elem.OuterXml ); 

  }
}

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

<!DOCTYPE root [
  <!ELEMENT root ANY> 
  <!ELEMENT Person ANY> 
  <!ELEMENT Customer EMPTY>
  <!ELEMENT Team EMPTY>
  <!ATTLIST Person SSN ID #REQUIRED>
  <!ATTLIST Customer id IDREF #REQUIRED >
  <!ATTLIST Team members IDREFS #REQUIRED>]>
<root>
  <Person SSN='A111' Name='Fred'/>
  <Person SSN='A111'/>
  <Person SSN='A222' Name='Tom'/>
  <Customer id='A111'/>
  <Customer id='A222334444'/>
  <Team members='A222334444 A333445555'/>
</root>

    

Return to top


Overloaded Method: GetElementsByTagName(
   string name
)
Inherited
See base class member description: System.Xml.XmlDocument.GetElementsByTagName

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

name

The qualified name to match. It is matched against the Name property of the matching node. The special value "*" 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 the document.
Example
The following example creates a XmlDocument object and uses the GetElmentsByTagName method and the resulting XmlNodeList object to display all the book titles.


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

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

    //Display all the book titles.
    XmlNodeList elemList = doc.GetElementsByTagName("title");
    for (int i=0; i < elemList.Count; i++)
    {   
      Console.WriteLine(elemList[i].InnerXml);
    }  

  }
}

    

Return to top


Overloaded Method: GetElementsByTagName(
   string localName,
   string namespaceURI
)
Inherited
See base class member description: System.Xml.XmlDocument.GetElementsByTagName

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

localName

The LocalName to match. The special value "*" matches all tags.

namespaceURI

NamespaceURI 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 the document 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


Method: ImportNode(
   XmlNode node,
   bool deep
)
Inherited
See base class member description: System.Xml.XmlDocument.ImportNode

Summary
Imports a node from another document to the current document.
C# Syntax:
public virtual XmlNode ImportNode(
   XmlNode node,
   bool deep
);
Parameters:

node

The node being imported.

deep

true to perform a deep clone; otherwise, false.

Return Value:
The imported XmlNode.
Exceptions
Exception Type Condition
InvalidOperationException Calling this method on a node type which cannot be imported.
Remarks
The returned node has no parent. The source node is not altered or removed from the original document; ImportNode creates a copy of the source node.

Importing a node creates an XmlNode object owned by the importing document, with XmlNode.Name and XmlNode.NodeType identical to the source node. The new object also has the attributes related to namespaces ( XmlNode.Prefix, XmlNode.LocalName, and XmlNode.NamespaceURI).

Depending on the node type of the imported node and the value of the deep parameter, additional information is copied as appropriate. This method attempts to mirror the behavior expected if a fragment of XML or HTML source was copied from one document to another (recognizing that, in the XML case, the two documents may have different DTDs).

The following table describes the specific behavior for each XmlNodeType.



XmlNodeType ImportNode(true) ImportNode(false)
Attribute The XmlAttribute.Specified property is set to true on the generated XmlAttribute . The descendants of the source XmlAttribute are recursively imported and the resulting nodes reassembled to form the corresponding subtree. The parameter does not apply to XmlAttribute nodes; they always carry their children with them when imported.
CData Copies the node, including its data. Copies the node, including its data.
Comment Copies the node, including its data. Copies the node, including its data.
DocumentFragment The descendants of the source node are recursively imported and the resulting nodes reassembled to form the corresponding subtree. An empty XmlDocumentFragment is generated.
DocumentType Copies the node, including its data.* Copies the node, including its data.*
Element The descendants of the source element and its specified attribute nodes are recursively imported and the resulting nodes reassembled to form the corresponding subtree.Note: Default attributes are not copied. If the document being imported into defines default attributes for this element name, those are assigned. Specified attribute nodes of the source element are imported, and the generated XmlAttribute nodes are attached to the generated XmlElement.Note: Default attributes are not copied. If the document being imported into defines default attributes for this element name, those are assigned.
EntityReference Because the source and destination documents could have the entities defined differently, this method only copies the XmlEntityReference node. The replacement text is not included. If the destination document has the entity defined, its value is assigned. Because the source and destination documents could have the entities defined differently, this method only copies the XmlEntityReference node. The replacement text is not included. If the destination document has the entity defined, its value is assigned.
ProcessingInstruction Copies the target and data value from the imported node. Copies the target and data value from the imported node.
Text Copies the node, including its data. Copies the node, including its data.
SignificantWhitespace Copies the node, including its data. Copies the node, including its data.
Whitespace Copies the node, including its data. Copies the node, including its data.
XmlDeclaration Copies the target and data value from the imported node. Copies the target and data value from the imported node.
All other node types. These node types cannot be imported. These node types cannot be imported.
*Although DocumentType nodes can be imported, a document can only have one DocumentType. If the document currently has a DocumenType node, it must be removed before adding a new one.
Example
The following example imports a book node from a second XML document into the original XML document.


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

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

    //Create another XmlDocument which holds a list of books.
    XmlDocument doc2 = new XmlDocument();
    doc2.Load("books.xml");

    //Import the last book node from doc2 into the original document.
    XmlNode newBook = doc.ImportNode(doc2.DocumentElement.LastChild, true);
    doc.DocumentElement.AppendChild(newBook); 
    
    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);

  }
}

    
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: 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


Overloaded Method: Load(
   Stream inStream
)
Inherited
See base class member description: System.Xml.XmlDocument.Load

Summary
Loads the XML document from the specified stream.
C# Syntax:
public virtual void Load(
   Stream inStream
);
Parameters:

inStream

The stream containing the XML document to load.

Exceptions
Exception Type Condition
XmlException There is a load or parse error in the XML. In this case, the document remains empty.
Remarks


Note The Load method always preserves significant white space. The XmlDocument.PreserveWhitespace property determines whether or not white space is preserved. The default is false, whites space is not preserved. This method does not do DTD or schema validation. If you want validation to occur, you need to pass an XmlValidatingReader, specify the appropriate XmlValidatingReader.ValidationType, and provide a XmlValidatingReader.ValidationEventHandler. You are then notified of all validation errors found during Load, and unless your ValidationEventHandler throws an exception to stop the process, the document will still be loaded.

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

Return to top


Overloaded Method: Load(
   string filename
)
This type supports the Shared Source CLI infrastructure and is not intended to be used directly from your code.
C# Syntax:
public override void Load(
   string filename
);
Parameters:

filename

Return to top


Overloaded Method: Load(
   TextReader txtReader
)
Inherited
See base class member description: System.Xml.XmlDocument.Load

Summary
Loads the XML document from the specified TextReader.
C# Syntax:
public virtual void Load(
   TextReader txtReader
);
Parameters:

txtReader

The TextReader used to feed the XML data into the document.

Exceptions
Exception Type Condition
XmlException There is a load or parse error in the XML. In this case, the document remains empty.
Remarks


Note The Load method always preserves significant white space. The XmlDocument.PreserveWhitespace property determines whether or not white space is preserved. The default is false, whites space is not preserved. This method does not do DTD or schema validation. If you want validation to occur, you need to pass an XmlValidatingReader, specify the appropriate XmlValidatingReader.ValidationType, and provide a XmlValidatingReader.ValidationEventHandler. You are then notified of all validation errors found during Load, and unless your ValidationEventHandler throws an exception to stop the process, the document will still be loaded.

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

Return to top


Overloaded Method: Load(
   XmlReader reader
)
Inherited
See base class member description: System.Xml.XmlDocument.Load

Summary
Loads the XML document from the specified XmlReader.
C# Syntax:
public virtual void Load(
   XmlReader reader
);
Parameters:

reader

The XmlReader used to feed the XML data into the document.

Exceptions
Exception Type Condition
XmlException There is a load or parse error in the XML. In this case, the document remains empty.
Remarks


Note The Load method always preserves significant white space. The XmlDocument.PreserveWhitespace property determines whether or not white space is preserved. The default is false, white space is not preserved. If the reader is in the initial state ( XmlReader.ReadState =ReadState.Initial), Load consumes the entire contents of the reader and builds the DOM from what it finds.

If the reader is already positioned on some node at depth "n", this method loads that node and all subsequent siblings up to the end tag that closes depth "n". This has the following results.

If the current node and its siblings look like the following:

<!--comment--><element1>one</element1><element2>two</element2>

Load throws an exception because a document cannot have two root level elements. If the current node and its siblings look like the following:

<!--comment--><?process instruction?><!--comment--></endtag>

Load succeeds, but you have an incomplete DOM tree because there is no root level element. Before you save the document, you must add a root level element, otherwise XmlDocument.Save will throw an exception.

If the reader is positioned on a leaf node that is invalid for the root level of a document, for example a whitespace or attribute node, the reader continues to read until it is positioned on a node that can be used for the root. The document begins loading at this point.

This method does not do DTD or schema validation. If you want validation to occur, you need to pass an XmlValidatingReader, specify the appropriate XmlValidatingReader.ValidationType, and provide a XmlValidatingReader.ValidationEventHandler. You are then notified of all validation errors found during Load, and unless your ValidationEventHandler throws an exception to stop the process, the document will still be loaded.

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

Example
The following example loads the last book node of the books.xml file into the XML document.


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

public class Sample
{
  public static void Main()
  {
    //Create the XmlDocument.
    XmlDocument doc = new XmlDocument();

    //Load the the document with the last book node.
    XmlTextReader reader = new XmlTextReader("books.xml");
    reader.WhitespaceHandling = WhitespaceHandling.None;
    reader.MoveToContent();
    reader.Read();
    reader.Skip(); //Skip the first book.
    reader.Skip(); //Skip the second book.
    doc.Load(reader);

    doc.Save(Console.Out);
  }
}

    
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>

    
See also:
XmlValidatingReader

Return to top


Method: LoadSingleElement(
   string filename,
   XmlTextReader sourceReader
)
This type supports the Shared Source CLI infrastructure and is not intended to be used directly from your code.
C# Syntax:
public void LoadSingleElement(
   string filename,
   XmlTextReader sourceReader
);
Parameters:

filename

sourceReader

Return to top


Method: LoadXml(
   string xml
)
Inherited
See base class member description: System.Xml.XmlDocument.LoadXml

Summary
Loads the XML document from the specified string.
C# Syntax:
public virtual void LoadXml(
   string xml
);
Parameters:

xml

String containing the XML document to load.

Exceptions
Exception Type Condition
XmlException There is a load or parse error in the XML. In this case, the document remains empty.
Remarks
By default the LoadXml method does not preserve white space nor significant white space. This method does not do DTD or Schema validation. If you want validation to occur, use the XmlDocument.Load method and pass it an XmlValidatingReader.

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

Example
The following example loads XML into an XmlDocument object and saves it out to a file.


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

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

    //Save the document to a file.
    doc.Save("data.xml");
  }
}

    

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


Method: ReadNode(
   XmlReader reader
)
Inherited
See base class member description: System.Xml.XmlDocument.ReadNode

Summary
Creates an XmlNode object based on the information in the XmlReader. The reader must be positioned on a node or attribute.
C# Syntax:
public virtual XmlNode ReadNode(
   XmlReader reader
);
Parameters:

reader

The Xml source

Return Value:
The new XmlNode or null if no more nodes exist.
Exceptions
Exception Type Condition
InvalidOperationException The reader is positioned on a node type that does not translate to a valid DOM node (for example, EndElement or EndEntity).
Remarks
Reads one XmlNode from the given reader and positions the reader on the next node. This method creates the type of XmlNode matching the XmlNode.NodeType on which the reader is currently positioned. (If the reader is in the initial state, ReadNode advances the reader to the first node and then operates on that node.)

If the reader is positioned on the start of an element, ReadNode reads all the attributes and any child nodes, up to and including the end tag of the current node. The XmlNode returned contains the sub-tree representing everything read. The reader is positioned immediately after the end tag.

ReadNode can also read attributes, but in this case it does not advance the reader to the next attribute. This allows you to write the following C# code:

              XmlDocument doc = new XmlDocument();
              while (reader.MoveToNextAttribute())
              {
                XmlNode a = doc.ReadNode(reader);
                // Do some more processing.
              }
            

ReadNode does consume the attribute value though, which means after calling ReadNode on an attribute, XmlReader.ReadAttributeValue returns false.

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

Example
The following example uses ReadNode to create a new node and then inserts the new node into the document.


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

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

    //Create a reader.
    XmlTextReader reader = new XmlTextReader("cd.xml");
    reader.MoveToContent(); //Move to the cd element node.

    //Create a node representing the cd element node.
    XmlNode cd = doc.ReadNode(reader);

    //Insert the new node into the document.
    doc.DocumentElement.AppendChild(cd); 
    
    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);

  }
}

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

<!-- sample CD -->
<cd genre='alternative'>
  <title>Americana</title>
  <artist>Offspring</artist>
</cd>

    
See also:
XmlReader

Return to top


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

Summary
Removes all the children and/or attributes of the current node.
C# Syntax:
public virtual void RemoveAll();
Remarks
If a 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.

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



Notes to inheritors: When overriding RemoveAll in a derived class, in order for events to be fired correctly, you must call the base class's RemoveAll method.
Example
The following example removes all child and attribute nodes from the root node.


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 all attribute and child nodes.
    root.RemoveAll();

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

  }
}

    

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: Save(
   Stream outStream
)
Inherited
See base class member description: System.Xml.XmlDocument.Save

Summary
Saves the XML document to the specified stream.
C# Syntax:
public virtual void Save(
   Stream outStream
);
Parameters:

outStream

The stream to which you want to save.

Exceptions
Exception Type Condition
XmlException The operation would not result in a well formed XML document (for example, no document element or duplicate XML declarations).
Remarks
White space is preserved only if XmlDocument.PreserveWhitespace is set to true.

When the document is saved, xmlns attributes are generated to persist the node identity (LocalName + NamespaceURI) correctly. For example, the following C# code

              XmlDocument doc = new XmlDocument();
               doc.AppendChild(doc.CreateElement("item","urn:1"));
               doc.Save(Console.Out);
            

generates this xmls attribute:

<item xmls="urn:1"/>

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

Return to top


Overloaded Method: Save(
   string filename
)
Inherited
See base class member description: System.Xml.XmlDocument.Save

Summary
Saves the XML document to the specified file.
C# Syntax:
public virtual void Save(
   string filename
);
Parameters:

filename

The location of the file where you want to save the document.

Exceptions
Exception Type Condition
XmlException The operation would not result in a well formed XML document (for example, no document element or duplicate XML declarations).
Remarks
White space is preserved in the output file only if XmlDocument.PreserveWhitespace is set to true.

When the document is saved, xmlns attributes are generated to persist the node identity (LocalName + NamespaceURI) correctly. For example, the following C# code

              XmlDocument doc = new XmlDocument();
               doc.AppendChild(doc.CreateElement("item","urn:1"));
               doc.Save(Console.Out);
            

generates this xmls attribute:

<item xmls="urn:1"/>

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

Example
The following example loads XML into an XmlDocument object, modifies it, and then saves it to a file.

 
 using System;
 using System.IO;
 using System.Xml;
 
 public class Sample
 {
   private const String saved_Doc = "output.xml";
   public static void Main()
   {
     Sample test = new Sample();
   }
   public Sample()
   {
     try
     {
       XmlDocument doc = new XmlDocument();
       doc.LoadXml("<book genre=\"novel\" publicationdate=\"1997\" " +
                   "      ISBN=\"1-861001-57-5\">" +
                   "  <title>Pride And Prejudice</title>" +
                   "  <author>" +
                   "    <first-name>Jane</first-name>" +
                   "    <last-name>Austen</last-name>" +
                   "  </author>" +
                   "  <price>24.95</price>" +
                   "</book>");
 
       XmlElement newElem = doc.CreateElement("availability");
 
       Console.WriteLine("Create some attributes and then add them to the element...");
       XmlAttribute newAttr = doc.CreateAttribute("outofstock");
       XmlAttribute newAttr1 = doc.CreateAttribute("style");
       newElem.SetAttributeNode(newAttr);
       newElem.SetAttribute("outofstock", "", "false");
       newElem.SetAttributeNode("style","");
       newElem.SetAttribute("style", "hardcover"); 
 
       //add the new element to the document
       XmlElement root = doc.DocumentElement;
       root.AppendChild(newElem);
 
       //save the output to a file     
       doc.Save(saved_Doc);
       Console.WriteLine();
       Console.WriteLine("Updated XML document saved in {0}", saved_Doc);
 
     }
     catch (Exception e)
     {
       Console.WriteLine ("Exception: {0}", e.ToString());
     }
 
   }
 }

    

Return to top


Overloaded Method: Save(
   TextWriter writer
)
Inherited
See base class member description: System.Xml.XmlDocument.Save

Summary
Saves the XML document to the specified TextWriter.
C# Syntax:
public virtual void Save(
   TextWriter writer
);
Parameters:

writer

The TextWriter to which you want to save.

Exceptions
Exception Type Condition
XmlException The operation would not result in a well formed XML document (for example, no document element or duplicate XML declarations).
Remarks
The encoding on the TextWriter determines the encoding that is written out (The encoding of the XmlDeclaration node is also replaced by the encoding of the TextWriter). If there was no encoding specified on the TextWriter, the XmlDocument is saved without an encoding attribute.

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

Return to top


Overloaded Method: Save(
   XmlWriter w
)
Inherited
See base class member description: System.Xml.XmlDocument.Save

Summary
Saves the XML document to the specified XmlWriter.
C# Syntax:
public virtual void Save(
   XmlWriter w
);
Parameters:

w

The XmlWriter to which you want to save.

Exceptions
Exception Type Condition
XmlException The operation would not result in a well formed XML document (for example, no document element or duplicate XML declarations).
Remarks
White space is preserved only if XmlDocument.PreserveWhitespace is set to true.

The encoding on the XmlWriter determines the encoding that is written out (The encoding of the XmlDeclaration node is also replaced by the encoding of the TextWriter). If there was no encoding specified on the XmlWriter, the XmlDocument is saved without an encoding attribute.

When the document is saved, xmlns attributes are generated to persist the node identity (LocalName + NamespaceURI) correctly. For example, the following C# code

              XmlDocument doc = new XmlDocument();
              doc.AppendChild(doc.CreateElement("item","urn:1"));
              doc.Save(Console.Out);
            

generates this xmls attribute:

<item xmls="urn:1"/>

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

Example
The following example loads XML into an XmlDocument object and saves it out to a file.


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

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

    //Save the document to a file.
    doc.Save("data.xml");
  }
}

    
See also:
XmlTextWriter.#ctor

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


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


Method: WriteContentTo(
   XmlWriter xw
)
Inherited
See base class member description: System.Xml.XmlDocument.WriteContentTo

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

xw

The XmlWriter to which you want to save.

Remarks
This method is a Microsoft extension to the Document Object Model (DOM). It is functionally equivalent to the XmlDocument.InnerXml property.

The XmlDeclaration.Encoding property determines the encoding that is written out. If the Encoding property does not have a value, the XmlDocument is written out without an encoding attribute.

Example
The following example displays the document onscreen.
public static void WriteXml( XmlDocument doc )
 {
    XmlTextWriter writer = new XmlTextWriter(Console.Out);
    writer.Formatting = Formatting.Indented;
    doc.WriteContentTo( writer );
    writer.Flush();
    Console.WriteLine();
 }

    

Return to top


Method: WriteTo(
   XmlWriter w
)
Inherited
See base class member description: System.Xml.XmlDocument.WriteTo

Summary
Saves the XmlDocument 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). It is functionally equivalent to the XmlNode.OuterXml property.

The XmlDeclaration.Encoding property determines the encoding that is written out. If the Encoding property does not have a value, the XmlDocument is written out without an encoding attribute.

Example
The following example displays the document onscreen.
public static void WriteXml( XmlDocument doc )
 {
    XmlTextWriter writer = new XmlTextWriter(Console.Out);
    writer.Formatting = Formatting.Indented;
    doc.WriteTo( writer );
    writer.Flush();
    Console.WriteLine();
 }

    

Return to top


Event: NodeChanged
Inherited
See base class member description: System.Xml.XmlDocument.NodeChanged

Summary
Occurs when the XmlNode.Value of a node belonging to this document has been changed.
C# Syntax:
public event XmlNodeChangedEventHandler NodeChanged;
Remarks
This event only applies to nodes that have a value.

All nodes created by this document, whether or not they have been inserted into the document, are included in this event.

Return to top


Event: NodeChanging
Inherited
See base class member description: System.Xml.XmlDocument.NodeChanging

Summary
Occurs when the XmlNode.Value of a node belonging to this document is about to be changed.
C# Syntax:
public event XmlNodeChangedEventHandler NodeChanging;
Remarks
This event allows the user to do extra checking and, if necessary, throw an exception to stop the operation. If an exception is thrown the XmlDocument returns to its original state. This event only applies to nodes that have a value.

All nodes created by this document, whether or not they have been inserted into the document, are included in this event.

Return to top


Event: NodeInserted
Inherited
See base class member description: System.Xml.XmlDocument.NodeInserted

Summary
Occurs when a node belonging to this document has been inserted into another node.
C# Syntax:
public event XmlNodeChangedEventHandler NodeInserted;
Remarks
All nodes created by this document, whether or not they have been inserted into the document, are included in this event.

Return to top


Event: NodeInserting
Inherited
See base class member description: System.Xml.XmlDocument.NodeInserting

Summary
Occurs when a node belonging to this document is about to be inserted into another node.
C# Syntax:
public event XmlNodeChangedEventHandler NodeInserting;
Remarks
This event allows the user to do extra checking and, if necessary, throw an exception to stop the operation. If an exception is thrown the XmlDocument returns to its original state.

All nodes created by this document, whether or not they have been inserted into the document, are included in this event.

Return to top


Event: NodeRemoved
Inherited
See base class member description: System.Xml.XmlDocument.NodeRemoved

Summary
Occurs when a node belonging to this document has been removed from its parent.
C# Syntax:
public event XmlNodeChangedEventHandler NodeRemoved;
Remarks
All nodes created by this document, whether or not they have been inserted into the document, are included in this event.

Return to top


Event: NodeRemoving
Inherited
See base class member description: System.Xml.XmlDocument.NodeRemoving

Summary
Occurs when a node belonging to this document is about to be removed from the document.
C# Syntax:
public event XmlNodeChangedEventHandler NodeRemoving;
Remarks
This event allows the user to do extra checking and, if necessary, throw an exception to stop the operation. If an exception is thrown the XmlDocument returns to its original state.

All nodes created by this document, whether or not they have been inserted into the document, are included in this event.

Return to top


Top of page

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