public abstract class XmlLinkedNode : XmlNode
|
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.XmlNode) |
Read-only See base class member description: System.Xml.XmlNode.BaseURI Gets the base URI of the current node. |
ChildNodes (inherited from System.Xml.XmlNode) |
Read-only See base class member description: System.Xml.XmlNode.ChildNodes Gets all the children of the node. |
FirstChild (inherited from System.Xml.XmlNode) |
Read-only See base class member description: System.Xml.XmlNode.FirstChild Gets the first child of the node. |
HasChildNodes (inherited from System.Xml.XmlNode) |
Read-only See base class member description: System.Xml.XmlNode.HasChildNodes Gets a value indicating whether this node has any child nodes. |
InnerText (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.XmlNode) |
Read-write See base class member description: System.Xml.XmlNode.InnerXml Gets or sets the markup representing just the children of this node. |
IsReadOnly (inherited from System.Xml.XmlNode) |
Read-only See base class member description: System.Xml.XmlNode.IsReadOnly Gets a value indicating whether the node is read-only. |
Item (inherited from System.Xml.XmlNode) |
Read-only Overloaded: Item[string name] {get See base class member description: System.Xml.XmlNode.ItemGets 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.ItemGets the first child element with the specified XmlNode.LocalName and XmlNode.NamespaceURI. |
LastChild (inherited from System.Xml.XmlNode) |
Read-only See base class member description: System.Xml.XmlNode.LastChild Gets the last child of the node. |
LocalName (inherited from System.Xml.XmlNode) |
Read-only See base class member description: System.Xml.XmlNode.LocalName When overridden in a derived class, gets the local name of the node. |
Name (inherited from System.Xml.XmlNode) |
Read-only See base class member description: System.Xml.XmlNode.Name When overridden in a derived class, 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. |
NextSibling | Read-only Overridden: Gets the node immediately following this node. |
NodeType (inherited from System.Xml.XmlNode) |
Read-only See base class member description: System.Xml.XmlNode.NodeType When overridden in a derived class, 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.XmlNode) |
Read-only See base class member description: System.Xml.XmlNode.OwnerDocument Gets the XmlDocument to which this node belongs. |
ParentNode (inherited from System.Xml.XmlNode) |
Read-only See base class member description: System.Xml.XmlNode.ParentNode Gets the parent of this node (for nodes that can have parents). |
Prefix (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. |
PreviousSibling | Read-only Overridden: 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. |
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.XmlNode) |
See base class member description: System.Xml.XmlNode.CloneNode When overridden in a derived class, creates a duplicate of the node. |
CreateNavigator (inherited from System.Xml.XmlNode) |
See base class member description: System.Xml.XmlNode.CreateNavigator Creates an XPathNavigator for navigating this object. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
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. |
InsertAfter (inherited from System.Xml.XmlNode) |
See base class member description: System.Xml.XmlNode.InsertAfter Inserts the specified node immediately after the specified reference node. |
InsertBefore (inherited from System.Xml.XmlNode) |
See base class member description: System.Xml.XmlNode.InsertBefore Inserts the specified node immediately before the specified reference node. |
Normalize (inherited from System.Xml.XmlNode) |
See base class member description: System.Xml.XmlNode.Normalize Puts all XmlText nodes in the full depth of the sub-tree underneath this XmlNode into a "normal" form where only markup (that is, tags, comments, processing instructions, CDATA sections, and entity references) separates XmlText nodes, that is, there are no adjacent XmlText nodes. |
PrependChild (inherited from System.Xml.XmlNode) |
See base class member description: System.Xml.XmlNode.PrependChild Adds the specified node to the beginning of the list of children of this node. |
RemoveAll (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. |
SelectNodes (inherited from System.Xml.XmlNode) |
Overloaded:SelectNodes(string xpath) See base class member description: System.Xml.XmlNode.SelectNodesSelects 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.SelectNodesSelects 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.SelectSingleNodeSelects 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.SelectSingleNodeSelects 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.XmlNode) |
See base class member description: System.Xml.XmlNode.WriteContentTo When overridden in a derived class, saves all the children of the node to the specified XmlWriter. |
WriteTo (inherited from System.Xml.XmlNode) |
See base class member description: System.Xml.XmlNode.WriteTo When overridden in a derived class, saves the current node to the specified XmlWriter. |
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:
public virtual XmlAttributeCollection Attributes {get;}
|
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); } }
public virtual string BaseURI {get;}
|
The value of this property varies depending on the node type. For example, Document nodes return the location of the XmlDocument object. Nodes which are children of external EntityReference nodes return the location of the entity itself. For example, consider the following XML document:
<!DOCTYPE item [ <!ENTITY bar SYSTEM "a/b.xml"> ]> <item num='123'>&bar;</item>
where the external entity a/b.xml contains the XML text:
<test>123</test>
.
If the document is loaded from http://server/mydata.xml , BaseURI returns the following:
NodeType | Name | BaseURI |
---|---|---|
Attribute | num | http://server/mydata.xml |
Document | #document | http://server/mydata.xml |
DocumentType | item | http://server/mydata.xml |
Entity | bar | http://server/mydata.xml |
Element | item | http://server/mydata.xml |
EntityReference | bar | http://server/mydata.xml |
Element | test | http://server/a/b.xml |
Text | #text | http://server/a/b.xml |
BaseURI looks for entity reference boundaries, so if entities are expanded this information is not preserved and this property returns the location of the XmlDocument object in all cases.
As a second example, given the following XML document:
<!DOCTYPE Mydata SYSTEM "http://localhost/doctype.dtd"> <baa>&bar;</baa>where the DTD file contains the following:
<!ENTITY bar <E1>My Data</E1> <!ELEMENT baa #PCDATA> <!ATTLIST baa attr1 "woof">
If the XML document is loaded from http://localhost/mydata.xml, BaseURI returns the following for each of the nodes:
NodeType | Name | BaseURI |
---|---|---|
Document | #document | http://localhost/mydata.xml |
DocumentType | Mydata | http://localhost/doctype.dtd |
Element | baa | http://localhost/mydata.xml |
Entity | bar | http://localhost/doctype.dtd |
EntityReference | bar | http://localhost/mydata.xml |
Attribute | woof | http://localhost/mydata.xml |
This property is a Microsoft extension to the Document Object Model (DOM).
public virtual XmlNodeList ChildNodes {get;}
|
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); } } } }
public virtual XmlNode FirstChild {get;}
|
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); } }
public virtual bool HasChildNodes {get;}
|
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); } } } }
public virtual string InnerText {get; set;}
|
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).
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 ); } }
public virtual string InnerXml {get; set;}
|
This property is a Microsoft extension to the Document Object Model (DOM).
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 ); } }
public virtual bool IsReadOnly {get;}
|
This property is a Microsoft extension to the Document Object Model (DOM).
public virtual XmlElement this[string name] {get;}
|
name
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); } }
public virtual XmlElement this[string localname, string ns] {get;}
|
localname
ns
public virtual XmlNode LastChild {get;}
|
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); } }
public abstract string LocalName {get;}
|
public abstract string Name {get;}
|
public virtual string NamespaceURI {get;}
|
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.
public override XmlNode NextSibling {get;}
|
using System; using System.IO; using System.Xml; public class Sample { public static void Main() { XmlDocument doc = new XmlDocument(); doc.Load("books.xml"); // Display the first two book nodes. XmlNode book = doc.DocumentElement.FirstChild; Console.WriteLine(book.OuterXml); Console.WriteLine(); Console.WriteLine(book.NextSibling.OuterXml); } }
public abstract XmlNodeType NodeType {get;}
|
public virtual string OuterXml {get;}
|
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); } }
public virtual XmlDocument OwnerDocument {get;}
|
public virtual XmlNode ParentNode {get;}
|
public virtual string Prefix {get; set;}
|
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". |
public override XmlNode PreviousSibling {get;}
|
public virtual string Value {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentException | The node is read-only. |
InvalidOperationException | The node is not supposed to have a value (for example, an Element node). |
newChild
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. |
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); } }
public virtual XmlNode Clone(); |
Clone is equivalent to calling
CloneNode(true)
.
This method is a Microsoft extension to the Document Object Model (DOM).
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); } }
deep
Exception Type | Condition |
---|---|
InvalidOperationException | Calling this method on a node type which cannot be cloned. |
The following table describes the specific behavior for each XmlNodeType.
XmlNodeType | CloneNode(true) | CloneNode(false) |
---|---|---|
Attribute | Clones the attribute node, including child nodes. | Clones the attribute node, including child nodes. |
CData | Clones the CData node, including its data content. | Clones the CData node, including its data content. |
Comment | Clones the comment node, including its text content. | Clones the comment node, including its text content. |
Document | Clones the document node, including any child nodes. | Clones the document node. |
DocumentFragment | Clones the document fragment node, including any child nodes. | Clones the document fragment node. |
DocumentType | Clones the document type node. | Clones the document type node. |
Element | Clones the element node, its attributes, and any child nodes. | Clones the element node and its attributes, including any default attributes. |
Entity | Entity nodes cannot be cloned. | Entity nodes cannot be cloned. |
EntityReference | Clones the entity reference node. The replacement text is not included. | Clones the entity reference node. The replacement text is not included. |
Notation | Notation nodes cannot be cloned. | Notation nodes cannot be cloned. |
ProcessingInstruction | Clones the processing instruction node, including its target and data. | Clones the processing instruction node, including its target and data. |
SignificantWhitespace | Clones the significant white space node, including its data value. | Clones the significant white space node, including its data value. |
Text | Clones the text node, including its data value. | Clones the text node, including its data value. |
Whitespace | Clones the white space node, including its data value. | Clones the white space node, including its data value. |
XmlDeclaration | Clones the Xml declaration node, including its data value. | Clones the XmlDeclaration node, including its data value. |
All other node types. | These node types cannot be cloned. | These node types cannot be cloned. |
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; //Create a deep clone. The cloned node //includes the child nodes. XmlNode deep = root.CloneNode(true); Console.WriteLine(deep.OuterXml); //Create a shallow clone. The cloned node does not //include the child nodes, but does include its attribute. XmlNode shallow = root.CloneNode(false); Console.WriteLine(shallow.OuterXml); } }
public XPathNavigator CreateNavigator(); |
This method is a Microsoft extension to the Document Object Model (DOM).
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);
~XmlLinkedNode(); |
public IEnumerator GetEnumerator(); |
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>
public virtual int GetHashCode(); |
prefix
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); } }
namespaceURI
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); } }
public Type GetType(); |
newChild
refChild
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. |
This method is a Microsoft extension to the Document Object Model (DOM).
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); } }
newChild
refChild
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. |
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); } }
protected object MemberwiseClone(); |
public virtual void Normalize(); |
newChild
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. |
This method is a Microsoft extension to the Document Object Model (DOM).
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); } }
public virtual void RemoveAll(); |
This method is a Microsoft extension to the Document Object Model (DOM).
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); } }
oldChild
Exception Type | Condition |
---|---|
ArgumentException | The oldChild is not a child of this node. Or this node is read-only. |
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); } }
newChild
oldChild
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. |
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); } }
public XmlNodeList SelectNodes( |
xpath
Exception Type | Condition |
---|---|
XPathException | The XPath expression contains a prefix. |
This method is a Microsoft extension to the Document Object Model (DOM).
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>
public XmlNodeList SelectNodes( |
xpath
nsmgr
Exception Type | Condition |
---|---|
XPathException | The XPath expression contains a prefix which is not defined in the XmlNamespaceManager. |
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).
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>
xpath
Exception Type | Condition |
---|---|
XPathException | The XPath expression contains a prefix. |
This method is a Microsoft extension to the Document Object Model (DOM).
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>
public XmlNode SelectSingleNode( |
xpath
nsmgr
Exception Type | Condition |
---|---|
XPathException | The XPath expression contains a prefix which is not defined in the XmlNamespaceManager. |
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).
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>
feature
version
Feature | Version |
---|---|
XML | 1.0 |
XML | 2.0 |
public virtual string ToString(); |
public abstract void WriteContentTo( |
w
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; //Note that because WriteContentTo saves only the children of the node //to the writer none of the attributes are displayed. Console.WriteLine("Display the contents of the node..."); XmlTextWriter writer = new XmlTextWriter(Console.Out); writer.Formatting = Formatting.Indented; root.WriteContentTo(writer); } }
public abstract void WriteTo( |
w
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; Console.WriteLine("Display the root node..."); XmlTextWriter writer = new XmlTextWriter(Console.Out); writer.Formatting = Formatting.Indented; root.WriteTo(writer); } }