XmlDocumentFragment Class

System.Xml

Represents a fragment of XML that can be inserted into an XmlDocument.

Inheritance

XmlDocumentFragment : XmlNode : object

Syntax

public sealed class XmlDocumentFragment : XmlNode

Constructors

Name Description
XmlDocumentFragment(XmlDocument ownerDocument) Initializes a new instance of the XmlDocumentFragment class.

Properties

Name Type Description
BaseURI string Gets the base URI of the current node.
ChildNodes XmlNodeList Gets a XmlNodeList containing the children of the current node.
FirstChild XmlNode Gets the first child of the current node.
InnerXml string Gets or sets a value indicating whether the current node is an attribute.
IsReadOnly bool Gets a value indicating whether this node is read-only.
LastChild XmlNode Gets the last child of the current node.
LocalName string Gets the local name of the node.
Name string Gets the qualified name of the node.
NamespaceURI string Gets the namespace URI of the document to which this node belongs.
NextSibling XmlNode Gets the node immediately following this node.
NodeType XmlNodeType Gets the node type of the current node.
OwnerDocument XmlDocument Gets the XmlDocument to which this node belongs.
ParentNode XmlNode Gets the parent of the current node.
Prefix string Gets or sets the namespace prefix of the node.
PreviousSibling XmlNode Gets the node immediately preceding this node.
Value string Gets or sets the value of the current node.

Methods

Name Description
AppendChild(XmlNode newChild) Appends the specified node to the end of the list of children.
CloneNode(bool deep) Creates a duplicate of this node.
InsertAfter(XmlNode newChild, XmlNode refChild) Inserts the specified node after the specified reference node.
InsertBefore(XmlNode newChild, XmlNode refChild) Inserts the specified node before the specified reference node.
Normalize() Ensures that all the nodes in the subtree are in a "normal" form, such that there are no empty text nodes and there are no nodes that are immediately adjacent to each other.
RemoveAll() Removes all the XmlNode objects from the current node.
RemoveChild(XmlNode oldChild) Removes the specified node from the list of children and returns it.
ReplaceChild(XmlNode newChild, XmlNode oldChild) Replaces the node specified by oldChild with the node specified by newChild.
WriteContentTo(XmlWriter w) Saves all the children of the current node to the specified XmlWriter.
WriteTo(XmlWriter w) Saves the current node and all its children to the specified XmlWriter.

Remarks

An XmlDocumentFragment is a lightweight object that can hold a part of an XML document. It is useful for scenarios where you need to create XML nodes programmatically and then insert them into an existing XmlDocument.

Unlike an XmlDocument, an XmlDocumentFragment does not have to be well-formed. For example, it can contain text nodes, elements, or both.

When an XmlDocumentFragment is inserted into an XmlDocument, its children are appended to the target node. The XmlDocumentFragment itself is not inserted.

This class is a part of the System.Xml namespace and is fundamental for manipulating XML structures in .NET.

Example

The following example demonstrates how to create an XmlDocumentFragment and add elements to it.


using System;
using System.Xml;

public class Example
{
    public static void Main(string[] args)
    {
        XmlDocument doc = new XmlDocument();

        // Create an XmlDocumentFragment
        XmlDocumentFragment fragment = doc.CreateDocumentFragment();

        // Create some elements and append them to the fragment
        XmlElement element1 = doc.CreateElement("book");
        element1.SetAttribute("id", "bk101");
        XmlElement title = doc.CreateElement("title");
        title.InnerText = "XML Developer's Guide";
        element1.AppendChild(title);

        XmlElement element2 = doc.CreateElement("author");
        element2.InnerText = "Gambardella, Matthew";
        fragment.AppendChild(element1);
        fragment.AppendChild(element2);

        // Now insert the fragment into the document
        XmlElement root = doc.CreateElement("catalog");
        doc.AppendChild(root);
        root.AppendChild(fragment); // The children of the fragment are inserted

        Console.WriteLine("XML Document after inserting fragment:");
        Console.WriteLine(doc.OuterXml);
    }
}
            

Output of the example:

<catalog><book id="bk101"><title>XML Developer's Guide</title></book><author>Gambardella, Matthew</author></catalog>