public void ChildNodes () { XmlTextReader xtr = new XmlTextReader ("<!DOCTYPE root [<!ENTITY ent 'ent-value'><!ENTITY el '<foo>hoge</foo><bar/>'>]><root/>", XmlNodeType.Document, null); XmlDocument doc = new XmlDocument (); doc.Load (xtr); XmlEntityReference ent = doc.CreateEntityReference ("ent"); // ChildNodes are not added yet. AssertNull (ent.FirstChild); doc.DocumentElement.AppendChild (ent); // ChildNodes are added here. AssertNotNull (ent.FirstChild); ent = doc.CreateEntityReference ("foo"); AssertNull (ent.FirstChild); // Entity value is empty when the matching DTD entity // node does not exist. doc.DocumentElement.AppendChild (ent); AssertNotNull (ent.FirstChild); AssertEquals (String.Empty, ent.FirstChild.Value); ent = doc.CreateEntityReference ("el"); AssertEquals ("", ent.InnerText); doc.DocumentElement.AppendChild (ent); AssertEquals ("<foo>hoge</foo><bar />", ent.InnerXml); AssertEquals ("hoge", ent.InnerText); AssertEquals (XmlNodeType.Element, ent.FirstChild.NodeType); }
private XmlAttribute LoadAttributeNode() { Debug.Assert(reader.NodeType == XmlNodeType.Attribute); XmlReader r = reader; if (r.IsDefault) { return(LoadDefaultAttribute()); } XmlAttribute attr = doc.CreateAttribute(r.Prefix, r.LocalName, r.NamespaceURI); IXmlSchemaInfo schemaInfo = r.SchemaInfo; if (schemaInfo != null) { attr.XmlName = doc.AddAttrXmlName(attr.Prefix, attr.LocalName, attr.NamespaceURI, schemaInfo); } while (r.ReadAttributeValue()) { XmlNode node; switch (r.NodeType) { case XmlNodeType.Text: node = doc.CreateTextNode(r.Value); break; case XmlNodeType.EntityReference: node = doc.CreateEntityReference(r.LocalName); if (r.CanResolveEntity) { r.ResolveEntity(); LoadAttributeValue(node, false); // Code internally relies on the fact that an EntRef nodes has at least one child (even an empty text node). Ensure that this holds true, // if the reader does not present any children for the ent-ref if (node.FirstChild == null) { node.AppendChildForLoad(doc.CreateTextNode(string.Empty), doc); } } break; default: throw UnexpectedNodeType(r.NodeType); } Debug.Assert(node != null); attr.AppendChildForLoad(node, doc); } return(attr); }
public override void WriteEntityRef(string name) { VerifyState(Method.WriteEntityRef); XmlNode node = document.CreateEntityReference(name); AddChild(node, write); }
public override void WriteEntityRef(string name) { VerifyState(Method.WriteEntityRef); XmlNode node = _document.CreateEntityReference(name); AddChild(node, _write); // REVIEW: the namespace scope is incorrect(?) unless write == null }
public void WriteTo () { XmlDocument doc = new XmlDocument(); doc.LoadXml("<root/>"); XmlEntityReference er = doc.CreateEntityReference("foo"); doc.DocumentElement.AppendChild(er); AssertEquals ("Name", "foo", er.Name); AssertEquals ("WriteTo", "<root>&foo;</root>", doc.DocumentElement.OuterXml); }
private XmlEntityReference LoadEntityReferenceNode() { Debug.Assert(reader.NodeType == XmlNodeType.EntityReference); XmlEntityReference eref = doc.CreateEntityReference(reader.Name); if (reader.CanResolveEntity) { reader.ResolveEntity(); LoadChildren(eref); // Code internally relies on the fact that an EntRef nodes has at least one child (even an empty text node). Ensure that this holds true, // if the reader does not present any children for the ent-ref if (eref.LastChild == null) { eref.AppendChild(doc.CreateTextNode("")); } } return(eref); }
/// <summary> /// Creates a System.Xml.XmlEntityReference node. /// </summary> /// <param name="name">The name of the entity reference</param> public override void WriteEntityRef(string name) { if (state == WriteState.Element) { state = WriteState.Content; } XmlNode n = current; if (state == WriteState.Attribute) { n = ca; } else if (state != WriteState.Content) { throw new InvalidOperationException("Invalid state '" + WriteState.ToString() + "' for entity reference"); } n.AppendChild(owner.CreateEntityReference(name)); }
private void WriteStyle(XmlDocument doc, XmlElement me, object o) { if (o.GetType() != typeof(SvgStyle)) { me.SetAttribute("style", doc.NamespaceURI, o.ToString()); return; } SvgStyle style = (SvgStyle)o; /* foreach(string s in style.Keys) { me.SetAttribute(s, doc.NamespaceURI, style.Get(s).ToString()); } */ me.SetAttribute("style", doc.NamespaceURI, style.ToString()); doc.CreateEntityReference("pingu"); }
public override void WriteEntityRef(string name) { WritePossiblyTopLevelNode(doc.CreateEntityReference(name), true); }
/// <summary> /// Used by CompressXML /// </summary> /// <param name="counts">Map of attribute to number of occurrences -- could be used to improve algorithm</param> /// <param name="entities">Map of attribute to entity name</param> /// <param name="doc"></param> /// <param name="el"></param> /// <param name="idx">Number that is incremented to provide new entity names</param> private static void RecCompXML(Hashtable counts, Hashtable entities, XmlDocument doc, XmlElement el, ref int idx) { ArrayList keys = new ArrayList(); foreach(XmlAttribute att in el.Attributes) { //don't try to do anything with xmlns nodes. if (att.Name.IndexOf("xmlns") == -1) keys.Add(att.Name); } foreach(string s in keys) { string val = el.Attributes[s].Value; if (counts[val] == null) { counts[val] = 1; } else { counts[val] = (int)counts[val] + 1; } if (val.Length > 30) { string entname; if(entities[val] == null) { idx += 1; entname = "E"+idx.ToString(); entities[val] = entname; } else { entname = (string)entities[val]; } XmlAttribute attr; //xlinks are a special case at the moment as the .NET XML API requires whatever bit of code //outputs a qualified name to *just magically happen to know* the URI that the prefix refers to. if (s.IndexOf("xlink") == 0) { attr = doc.CreateAttribute(s, "http://www.w3.org/1999/xlink"); } else { attr = doc.CreateAttribute(s); } attr.AppendChild(doc.CreateEntityReference(entname)); el.SetAttributeNode(attr); } } foreach(XmlNode ch in el.ChildNodes) { if (ch.GetType() == typeof(XmlElement)) RecCompXML(counts, entities, doc, (XmlElement)ch, ref idx); } }
protected internal override void Associate(XmlDocument/*!*/ document) { if (!IsAssociated) { XmlEntityReference = document.CreateEntityReference(_name); } }
void CopyNode (XmlDocument newDoc, XmlNode from, XmlNode toParent) { if (RemoveAll && from.NodeType != XmlNodeType.Element) return; XmlNode child = null; bool newLineNode = false; switch (from.NodeType) { case XmlNodeType.Element: newLineNode = true; if (RemoveNamespacesAndPrefixes) child = newDoc.CreateElement (from.LocalName); else { XmlElement e = from as XmlElement; child = newDoc.CreateElement (e.Prefix, e.LocalName, e.NamespaceURI); } break; case XmlNodeType.Attribute: { if (RemoveAttributes) return; XmlAttribute fromAttr = from as XmlAttribute; if (!fromAttr.Specified) return; XmlAttribute a; if (RemoveNamespacesAndPrefixes) a = newDoc.CreateAttribute (fromAttr.LocalName); else a = newDoc.CreateAttribute (fromAttr.Prefix, fromAttr.LocalName, fromAttr.NamespaceURI); toParent.Attributes.Append(a); CopyNodes (newDoc, from, a); return; } case XmlNodeType.CDATA: newLineNode = true; child = newDoc.CreateCDataSection ((from as XmlCDataSection).Data); break; case XmlNodeType.Comment: if (RemoveWhiteSpace) return; newLineNode = true; child = newDoc.CreateComment ((from as XmlComment).Data); break; case XmlNodeType.ProcessingInstruction: newLineNode = true; XmlProcessingInstruction pi = from as XmlProcessingInstruction; child = newDoc.CreateProcessingInstruction (pi.Target, pi.Data); break; case XmlNodeType.DocumentType: newLineNode = true; toParent.AppendChild (from.CloneNode (true)); return; case XmlNodeType.EntityReference: child = newDoc.CreateEntityReference ((from as XmlEntityReference).Name); break; case XmlNodeType.SignificantWhitespace: if (RemoveWhiteSpace) return; child = newDoc.CreateSignificantWhitespace (from.Value); break; case XmlNodeType.Text: if (RemoveText) return; newLineNode = true; child = newDoc.CreateTextNode (from.Value); break; case XmlNodeType.Whitespace: if (RemoveWhiteSpace) return; child = newDoc.CreateWhitespace (from.Value); break; case XmlNodeType.XmlDeclaration: newLineNode = true; XmlDeclaration d = from as XmlDeclaration; XmlDeclaration d1 = newDoc.CreateXmlDeclaration (d.Version, d.Encoding, d.Standalone); newDoc.InsertBefore(d1, newDoc.DocumentElement); return; } if (NewLines && newLineNode && toParent.NodeType != XmlNodeType.Attribute) { XmlSignificantWhitespace s = newDoc.CreateSignificantWhitespace("\r\n"); toParent.AppendChild (s); } toParent.AppendChild(child); CopyNodes (newDoc, from, child); }
/// <summary> /// Used by CompressXML /// </summary> /// <param name="counts">Map of attribute to number of occurrences -- could be used to improve algorithm</param> /// <param name="entities">Map of attribute to entity name</param> /// <param name="doc"></param> /// <param name="el"></param> /// <param name="idx">Number that is incremented to provide new entity names</param> private static void RecCompXML(Hashtable counts, Hashtable entities, XmlDocument doc, XmlElement el, ref int idx) { ArrayList keys = new ArrayList(); foreach(XmlAttribute att in el.Attributes) { keys.Add(att.Name); } foreach(string s in keys) { string val = el.Attributes[s].Value; if (counts[val] == null) { counts[val] = 1; } else { counts[val] = (int)counts[val] + 1; } if (val.Length > 30) { string entname; if(entities[val] == null) { idx += 1; entname = "E"+idx.ToString(); entities[val] = entname; } else { entname = (string)entities[val]; } XmlAttribute attr = doc.CreateAttribute(s); attr.AppendChild(doc.CreateEntityReference(entname)); el.SetAttributeNode(attr); } } foreach(XmlNode ch in el.ChildNodes) { if (ch.GetType() == typeof(XmlElement)) RecCompXML(counts, entities, doc, (XmlElement)ch, ref idx); } }