private XmlDiffViewNode ImportNode(XmlNode node) { XmlDiffViewNode newNode = null; switch (node.NodeType) { case XmlNodeType.Element: XmlElement el = (XmlElement)node; XmlDiffViewElement newElement = new XmlDiffViewElement(el.LocalName, el.Prefix, el.NamespaceURI, _bIgnorePrefixes); // attributes IEnumerator attributes = node.Attributes.GetEnumerator(); XmlDiffViewAttribute lastNewAttr = null; while (attributes.MoveNext()) { XmlAttribute at = (XmlAttribute)attributes.Current; XmlDiffViewAttribute newAttr = new XmlDiffViewAttribute(at.LocalName, at.Prefix, at.NamespaceURI, at.Value); newElement.InsertAttributeAfter(newAttr, lastNewAttr); lastNewAttr = newAttr; } // children IEnumerator childNodes = node.ChildNodes.GetEnumerator(); XmlDiffViewNode lastNewChildNode = null; while (childNodes.MoveNext()) { XmlDiffViewNode newChildNode = ImportNode((XmlNode)childNodes.Current); newElement.InsertChildAfter(newChildNode, lastNewChildNode, false); lastNewChildNode = newChildNode; } newNode = newElement; break; case XmlNodeType.Text: case XmlNodeType.CDATA: case XmlNodeType.Comment: newNode = new XmlDiffViewCharData(node.Value, node.NodeType); break; case XmlNodeType.ProcessingInstruction: newNode = new XmlDiffViewPI(node.Name, node.Value); break; case XmlNodeType.EntityReference: newNode = new XmlDiffViewER(node.Name); break; default: Debug.Assert(false, "Invalid node type."); break; } Debug.Assert(newNode != null); return(newNode); }
private void OnAddNode(XmlElement diffgramElement, string nodeTypeAttr, XmlDiffViewParentNode sourceParent, ref XmlDiffViewNode currentPosition) { XmlNodeType nodeType = (XmlNodeType)int.Parse(nodeTypeAttr); string name = diffgramElement.GetAttribute("name"); string prefix = diffgramElement.GetAttribute("prefix"); string ns = diffgramElement.GetAttribute("ns"); string opidAttr = diffgramElement.GetAttribute("opid"); int opid = (opidAttr == string.Empty) ? 0 : int.Parse(opidAttr); if (nodeType == XmlNodeType.Attribute) { Debug.Assert(name != string.Empty); XmlDiffViewAttribute newAttr = new XmlDiffViewAttribute(name, prefix, ns, diffgramElement.InnerText); newAttr._op = XmlDiffViewOperation.Add; newAttr._opid = opid; ((XmlDiffViewElement)sourceParent).InsertAttributeAfter(newAttr, null); } else { XmlDiffViewNode newNode = null; switch (nodeType) { case XmlNodeType.Element: Debug.Assert(name != string.Empty); newNode = new XmlDiffViewElement(name, prefix, ns, _bIgnorePrefixes); ApplyDiffgram(diffgramElement, (XmlDiffViewParentNode)newNode); break; case XmlNodeType.Text: case XmlNodeType.CDATA: case XmlNodeType.Comment: Debug.Assert(diffgramElement.InnerText != string.Empty); newNode = new XmlDiffViewCharData(diffgramElement.InnerText, nodeType); break; case XmlNodeType.ProcessingInstruction: Debug.Assert(diffgramElement.InnerText != string.Empty); Debug.Assert(name != string.Empty); newNode = new XmlDiffViewPI(name, diffgramElement.InnerText); break; case XmlNodeType.EntityReference: Debug.Assert(name != string.Empty); newNode = new XmlDiffViewER(name); break; case XmlNodeType.XmlDeclaration: Debug.Assert(diffgramElement.InnerText != string.Empty); newNode = new XmlDiffViewXmlDeclaration(diffgramElement.InnerText); break; case XmlNodeType.DocumentType: newNode = new XmlDiffViewDocumentType(diffgramElement.GetAttribute("name"), diffgramElement.GetAttribute("publicId"), diffgramElement.GetAttribute("systemId"), diffgramElement.InnerText); break; default: Debug.Assert(false, "Invalid node type."); break; } Debug.Assert(newNode != null); newNode._op = XmlDiffViewOperation.Add; newNode._opid = opid; sourceParent.InsertChildAfter(newNode, currentPosition, false); currentPosition = newNode; } }
private void LoadSourceChildNodes(XmlDiffViewParentNode parent, XmlReader reader, bool bEmptyElement) { LoadState savedLoadState = _loadState; _loadState.Reset(); // load attributes while (reader.MoveToNextAttribute()) { XmlDiffViewAttribute attr; if (reader.Prefix == "xmlns" || (reader.Prefix == string.Empty && reader.LocalName == "xmlns")) { attr = new XmlDiffViewAttribute(reader.LocalName, reader.Prefix, reader.NamespaceURI, reader.Value); if (_bIgnoreNamespaces) { attr._op = XmlDiffViewOperation.Ignore; } } else { string attrValue = _bIgnoreWhitespace ? NormalizeText(reader.Value) : reader.Value; attr = new XmlDiffViewAttribute(reader.LocalName, reader.Prefix, reader.NamespaceURI, attrValue); } ((XmlDiffViewElement)parent).InsertAttributeAfter(attr, _loadState._curLastAttribute); _loadState._curLastAttribute = attr; } // empty element -> return, do not load chilren if (bEmptyElement) { goto End; } // load children while (reader.Read()) { // ignore whitespaces between nodes if (reader.NodeType == XmlNodeType.Whitespace) { continue; } XmlDiffViewNode child = null; switch (reader.NodeType) { case XmlNodeType.Element: bool bEmptyEl = reader.IsEmptyElement; XmlDiffViewElement elem = new XmlDiffViewElement(reader.LocalName, reader.Prefix, reader.NamespaceURI, _bIgnorePrefixes); LoadSourceChildNodes(elem, reader, bEmptyEl); child = elem; break; case XmlNodeType.Attribute: Debug.Assert(false, "We should never get to this point, attributes should be read at the beginning of thid method."); break; case XmlNodeType.Text: child = new XmlDiffViewCharData((_bIgnoreWhitespace) ? NormalizeText(reader.Value) : reader.Value, XmlNodeType.Text); break; case XmlNodeType.CDATA: child = new XmlDiffViewCharData(reader.Value, XmlNodeType.CDATA); break; case XmlNodeType.EntityReference: child = new XmlDiffViewER(reader.Name); break; case XmlNodeType.Comment: child = new XmlDiffViewCharData(reader.Value, XmlNodeType.Comment); if (_bIgnoreComments) { child._op = XmlDiffViewOperation.Ignore; } break; case XmlNodeType.ProcessingInstruction: child = new XmlDiffViewPI(reader.Name, reader.Value); if (_bIgnorePI) { child._op = XmlDiffViewOperation.Ignore; } break; case XmlNodeType.SignificantWhitespace: if (reader.XmlSpace == XmlSpace.Preserve) { child = new XmlDiffViewCharData(reader.Value, XmlNodeType.SignificantWhitespace); if (_bIgnoreWhitespace) { child._op = XmlDiffViewOperation.Ignore; } } break; case XmlNodeType.XmlDeclaration: child = new XmlDiffViewXmlDeclaration(NormalizeText(reader.Value)); if (_bIgnoreXmlDecl) { child._op = XmlDiffViewOperation.Ignore; } break; case XmlNodeType.EndElement: goto End; case XmlNodeType.DocumentType: child = new XmlDiffViewDocumentType(reader.Name, reader.GetAttribute("PUBLIC"), reader.GetAttribute("SYSTEM"), reader.Value); if (_bIgnoreDtd) { child._op = XmlDiffViewOperation.Ignore; } break; default: Debug.Assert(false, "Invalid node type"); break; } parent.InsertChildAfter(child, _loadState._curLastChild, true); _loadState._curLastChild = child; } End: _loadState = savedLoadState; }