mscorlib(4.0.0.0) API with additions
XmlDocumentFragment.cs
1 using System.Collections;
2 using System.Xml.XPath;
3 
4 namespace System.Xml
5 {
7  public class XmlDocumentFragment : XmlNode
8  {
9  private XmlLinkedNode lastChild;
10 
13  public override string Name => OwnerDocument.strDocumentFragmentName;
14 
17  public override string LocalName => OwnerDocument.strDocumentFragmentName;
18 
21  public override XmlNodeType NodeType => XmlNodeType.DocumentFragment;
22 
25  public override XmlNode ParentNode => null;
26 
29  public override XmlDocument OwnerDocument => (XmlDocument)parentNode;
30 
34  public override string InnerXml
35  {
36  get
37  {
38  return base.InnerXml;
39  }
40  set
41  {
42  RemoveAll();
43  XmlLoader xmlLoader = new XmlLoader();
44  xmlLoader.ParsePartialContent(this, value, XmlNodeType.Element);
45  }
46  }
47 
48  internal override bool IsContainer => true;
49 
50  internal override XmlLinkedNode LastNode
51  {
52  get
53  {
54  return lastChild;
55  }
56  set
57  {
58  lastChild = value;
59  }
60  }
61 
62  internal override XPathNodeType XPNodeType => XPathNodeType.Root;
63 
66  protected internal XmlDocumentFragment(XmlDocument ownerDocument)
67  {
68  if (ownerDocument == null)
69  {
70  throw new ArgumentException(Res.GetString("Xdom_Node_Null_Doc"));
71  }
72  parentNode = ownerDocument;
73  }
74 
79  public override XmlNode CloneNode(bool deep)
80  {
81  XmlDocument ownerDocument = OwnerDocument;
82  XmlDocumentFragment xmlDocumentFragment = ownerDocument.CreateDocumentFragment();
83  if (deep)
84  {
85  xmlDocumentFragment.CopyChildren(ownerDocument, this, deep);
86  }
87  return xmlDocumentFragment;
88  }
89 
90  internal override bool IsValidChildType(XmlNodeType type)
91  {
92  switch (type)
93  {
94  case XmlNodeType.Element:
95  case XmlNodeType.Text:
96  case XmlNodeType.CDATA:
97  case XmlNodeType.EntityReference:
98  case XmlNodeType.ProcessingInstruction:
99  case XmlNodeType.Comment:
100  case XmlNodeType.Whitespace:
101  case XmlNodeType.SignificantWhitespace:
102  return true;
103  case XmlNodeType.XmlDeclaration:
104  {
105  XmlNode firstChild = FirstChild;
106  if (firstChild == null || firstChild.NodeType != XmlNodeType.XmlDeclaration)
107  {
108  return true;
109  }
110  return false;
111  }
112  default:
113  return false;
114  }
115  }
116 
117  internal override bool CanInsertAfter(XmlNode newChild, XmlNode refChild)
118  {
119  if (newChild.NodeType == XmlNodeType.XmlDeclaration)
120  {
121  if (refChild == null)
122  {
123  return LastNode == null;
124  }
125  return false;
126  }
127  return true;
128  }
129 
130  internal override bool CanInsertBefore(XmlNode newChild, XmlNode refChild)
131  {
132  if (newChild.NodeType == XmlNodeType.XmlDeclaration)
133  {
134  if (refChild != null)
135  {
136  return refChild == FirstChild;
137  }
138  return true;
139  }
140  return true;
141  }
142 
145  public override void WriteTo(XmlWriter w)
146  {
147  WriteContentTo(w);
148  }
149 
152  public override void WriteContentTo(XmlWriter w)
153  {
154  IEnumerator enumerator = GetEnumerator();
155  try
156  {
157  while (enumerator.MoveNext())
158  {
159  XmlNode xmlNode = (XmlNode)enumerator.Current;
160  xmlNode.WriteTo(w);
161  }
162  }
163  finally
164  {
165  IDisposable disposable = enumerator as IDisposable;
166  if (disposable != null)
167  {
168  disposable.Dispose();
169  }
170  }
171  }
172  }
173 }
virtual XmlDocumentFragment CreateDocumentFragment()
Creates an T:System.Xml.XmlDocumentFragment.
Definition: XmlDocument.cs:839
Represents an XML document. You can use this class to load, validate, edit, add, and position XML in ...
Definition: XmlDocument.cs:13
IEnumerator GetEnumerator()
Get an enumerator that iterates through the child nodes in the current node.
Definition: XmlNode.cs:1111
bool MoveNext()
Advances the enumerator to the next element of the collection.
abstract void WriteTo(XmlWriter w)
Saves the current node to the specified T:System.Xml.XmlWriter, when overridden in a derived class.
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Definition: IDisposable.cs:8
Definition: __Canon.cs:3
XmlNodeType
Specifies the type of node.
Definition: XmlNodeType.cs:5
XPathNodeType
Defines the XPath node types that can be returned from the T:System.Xml.XPath.XPathNavigator class.
Definition: XPathNodeType.cs:4
Represents a writer that provides a fast, non-cached, forward-only way to generate streams or files t...
Definition: XmlWriter.cs:12
override XmlNode ParentNode
Gets the parent of this node (for nodes that can have parents).
override XmlNode CloneNode(bool deep)
Creates a duplicate of this node.
Gets the node immediately preceding or following this node.
Definition: XmlLinkedNode.cs:4
override XmlNodeType NodeType
Gets the type of the current node.
object Current
Gets the element in the collection at the current position of the enumerator.
Definition: IEnumerator.cs:15
override string Name
Gets the qualified name of the node.
internal XmlDocumentFragment(XmlDocument ownerDocument)
Initializes a new instance of the T:System.Xml.XmlDocumentFragment class.
The exception that is thrown when one of the arguments provided to a method is not valid.
override XmlDocument OwnerDocument
Gets the T:System.Xml.XmlDocument to which this node belongs.
virtual XmlNode FirstChild
Gets the first child of the node.
Definition: XmlNode.cs:117
override void WriteContentTo(XmlWriter w)
Saves all the children of the node to the specified T:System.Xml.XmlWriter.
override string InnerXml
Gets or sets the markup representing the children of this node.
override string LocalName
Gets the local name of the node.
virtual void RemoveAll()
Removes all the child nodes and/or attributes of the current node.
Definition: XmlNode.cs:1143
void Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resourc...
override void WriteTo(XmlWriter w)
Saves the node to the specified T:System.Xml.XmlWriter.
Represents a single node in the XML document.
Definition: XmlNode.cs:13
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
abstract XmlNodeType NodeType
Gets the type of the current node, when overridden in a derived class.
Definition: XmlNode.cs:53
Represents a lightweight object that is useful for tree insert operations.