public TransparentXmlFragment( bool init ) { elem1 = "3"; elem3 = "4"; XmlDocument doc = new XmlDocument(); _frag = doc.CreateDocumentFragment(); XmlNode node = _frag.AppendChild( doc.CreateElement( "sub1" ) ); node.AppendChild( doc.CreateElement( "extra" ) ); _frag.AppendChild( doc.CreateElement( "sub2" ) ).InnerText = "2"; }
public void GetInnerXml () { // this will be also tests of TestWriteTo()/TestWriteContentTo() document = new XmlDocument (); fragment = document.CreateDocumentFragment (); fragment.AppendChild (document.CreateElement ("foo")); fragment.AppendChild (document.CreateElement ("bar")); fragment.AppendChild (document.CreateElement ("baz")); AssertEquals ("#Simple", "<foo /><bar /><baz />", fragment.InnerXml); }
public void AppendFragmentToElement () { document = new XmlDocument (); fragment = document.CreateDocumentFragment (); document.LoadXml ("<html><head></head><body></body></html>"); XmlElement body = document.DocumentElement.LastChild as XmlElement; fragment.AppendChild (document.CreateElement ("p")); fragment.AppendChild (document.CreateElement ("div")); // appending fragment to element body.AppendChild (fragment); AssertNotNull ("#AppendFragmentToElement.Exist", body.FirstChild); AssertEquals ("#AppendFragmentToElement.ChildIsElement", XmlNodeType.Element, body.FirstChild.NodeType); AssertEquals ("#AppendFragmentToElement.FirstChild", "p", body.FirstChild.Name); AssertEquals ("#AppendFragmentToElement.LastChild", "div", body.LastChild.Name); }
public void AppendFragmentToElement () { document = new XmlDocument (); fragment = document.CreateDocumentFragment (); document.LoadXml ("<html><head></head><body></body></html>"); XmlElement body = document.DocumentElement.LastChild as XmlElement; fragment.AppendChild (document.CreateElement ("p")); fragment.AppendChild (document.CreateElement ("div")); // appending fragment to element XmlNode ret = body.AppendChild (fragment); Assert.IsNotNull (body.FirstChild, "#AppendFragmentToElement.Exist"); Assert.AreEqual (XmlNodeType.Element, body.FirstChild.NodeType, "#AppendFragmentToElement.ChildIsElement"); Assert.AreEqual ("p", body.FirstChild.Name, "#AppendFragmentToElement.FirstChild"); Assert.AreEqual ("div", body.LastChild.Name, "#AppendFragmentToElement.LastChild"); Assert.AreEqual ("p", ret.LocalName, "#AppendFragmentToElement.ReturnValue"); }
public void AppendChildToFragment () { document = new XmlDocument (); fragment = document.CreateDocumentFragment (); document.LoadXml ("<html><head></head><body></body></html>"); XmlElement el = document.CreateElement ("p"); el.InnerXml = "Test Paragraph"; // appending element to fragment fragment.AppendChild (el); AssertNotNull ("#AppendChildToFragment.Element", fragment.FirstChild); AssertNotNull ("#AppendChildToFragment.Element.Children", fragment.FirstChild.FirstChild); AssertEquals ("#AppendChildToFragment.Element.Child.Text", "Test Paragraph", fragment.FirstChild.FirstChild.Value); }
public void core0004A() { string computedValue = ""; string expectedValue = "domestic"; System.Xml.XmlAttribute domesticAttr = null; testResults results = new testResults("Core0004A"); try { results.description = "Attr objects may be associated with Element " + "nodes contained within a DocumentFragment."; System.Xml.XmlDocumentFragment docFragment = util.getDOMDocument().CreateDocumentFragment(); System.Xml.XmlElement newElement = (System.Xml.XmlElement)util.createNode(util.ELEMENT_NODE, "element1"); // // The new DocumentFragment is empty upon creation. Set an attribute for // a newly created element and add the element to the documentFragment. // newElement.SetAttribute("domestic", "Yes"); docFragment.AppendChild(newElement); // // Access the attributes of the only child of the documentFragment // domesticAttr = (System.Xml.XmlAttribute)docFragment.FirstChild.Attributes.Item(0); computedValue = domesticAttr.Name; } catch (System.Exception ex) { computedValue = "Exception " + ex.Message; } // // Write out results // results.expected = expectedValue; results.actual = computedValue; util.resetData(); Assert.AreEqual(results.expected, results.actual); // return results; }
public virtual XmlNode ImportNode(XmlNode node, bool deep) { if (node == null) { throw new NullReferenceException("Null node cannot be imported."); } switch (node.NodeType) { case XmlNodeType.Attribute: XmlAttribute srcAtt = node as XmlAttribute; XmlAttribute dstAtt = this.CreateAttribute(srcAtt.Prefix, srcAtt.LocalName, srcAtt.NamespaceURI); for (XmlNode n = srcAtt.FirstChild; n != null; n = n.NextSibling) { dstAtt.AppendChild(this.ImportNode(n, deep)); } return(dstAtt); case XmlNodeType.CDATA: return(this.CreateCDataSection(node.Value)); case XmlNodeType.Comment: return(this.CreateComment(node.Value)); case XmlNodeType.Document: throw new XmlException("Document cannot be imported."); case XmlNodeType.DocumentFragment: XmlDocumentFragment df = this.CreateDocumentFragment(); if (deep) { for (XmlNode n = node.FirstChild; n != null; n = n.NextSibling) { df.AppendChild(this.ImportNode(n, deep)); } } return(df); case XmlNodeType.DocumentType: throw new XmlException("DocumentType cannot be imported."); case XmlNodeType.Element: XmlElement src = (XmlElement)node; XmlElement dst = this.CreateElement(src.Prefix, src.LocalName, src.NamespaceURI); for (int i = 0; i < src.Attributes.Count; i++) { XmlAttribute attr = src.Attributes [i]; if (attr.Specified) // copies only specified attributes { dst.SetAttributeNode((XmlAttribute)this.ImportNode(attr, deep)); } } if (deep) { for (XmlNode n = src.FirstChild; n != null; n = n.NextSibling) { dst.AppendChild(this.ImportNode(n, deep)); } } return(dst); case XmlNodeType.EndElement: throw new XmlException("Illegal ImportNode call for NodeType.EndElement"); case XmlNodeType.EndEntity: throw new XmlException("Illegal ImportNode call for NodeType.EndEntity"); case XmlNodeType.EntityReference: return(this.CreateEntityReference(node.Name)); case XmlNodeType.None: throw new XmlException("Illegal ImportNode call for NodeType.None"); case XmlNodeType.ProcessingInstruction: XmlProcessingInstruction pi = node as XmlProcessingInstruction; return(this.CreateProcessingInstruction(pi.Target, pi.Data)); case XmlNodeType.SignificantWhitespace: return(this.CreateSignificantWhitespace(node.Value)); case XmlNodeType.Text: return(this.CreateTextNode(node.Value)); case XmlNodeType.Whitespace: return(this.CreateWhitespace(node.Value)); case XmlNodeType.XmlDeclaration: XmlDeclaration srcDecl = node as XmlDeclaration; return(this.CreateXmlDeclaration(srcDecl.Version, srcDecl.Encoding, srcDecl.Standalone)); default: throw new InvalidOperationException("Cannot import specified node type: " + node.NodeType); } }
protected void MoveChildNodes(XmlDocumentFragment fragment, XmlElement element) { while (element.ChildNodes.Count > 0) { fragment.AppendChild(element.ChildNodes[0]); } }