CreateSignificantWhitespace() public method

public CreateSignificantWhitespace ( string text ) : XmlSignificantWhitespace
text string
return XmlSignificantWhitespace
Example #1
0
        private XmlNode LoadCurrentNode()
        {
            switch (reader.NodeType)
            {
            case XmlNodeType.Element:
                return(LoadElementNode());

            case XmlNodeType.Attribute:
                return(LoadAttributeNode());

            case XmlNodeType.Text:
                return(doc.CreateTextNode(reader.Value));

            case XmlNodeType.SignificantWhitespace:
                return(doc.CreateSignificantWhitespace(reader.Value));

            case XmlNodeType.Whitespace:
                // Cannot create a function from this code, b/c this code does not return a whitespace node if preserceWS is false
                if (preserveWhitespace)
                {
                    return(doc.CreateWhitespace(reader.Value));
                }

                // if preserveWhitespace is false skip all subsequent WS nodes and position on the first non-WS node
                do
                {
                    if (!reader.Read())
                    {
                        return(null);
                    }
                } while (reader.NodeType == XmlNodeType.Whitespace);
                return(LoadCurrentNode());      // Skip WS node if preserveWhitespace is false

            case XmlNodeType.CDATA:
                return(doc.CreateCDataSection(reader.Value));

            case XmlNodeType.EntityReference:
                return(LoadEntityReferenceNode());

            case XmlNodeType.XmlDeclaration:
                return(LoadDeclarationNode());

            case XmlNodeType.ProcessingInstruction:
                return(doc.CreateProcessingInstruction(reader.Name, reader.Value));

            case XmlNodeType.Comment:
                return(doc.CreateComment(reader.Value));

            case XmlNodeType.DocumentType:
                return(LoadDocumentTypeNode());

            case XmlNodeType.EndElement:
            case XmlNodeType.EndEntity:
                return(null);

            default:
                throw new InvalidOperationException(string.Format(Res.GetString(Res.Xdom_Load_NodeType), reader.NodeType.ToString()));
            }
        }
		public void GetReady ()
		{
			document = new XmlDocument ();
			document.LoadXml ("<root><foo></foo></root>");
			XmlElement element = document.CreateElement ("foo");
			whitespace = document.CreateSignificantWhitespace ("\r\n");
			element.AppendChild (whitespace);

			doc2 = new XmlDocument ();
		}
        public static void ImportSignificantWhitespace()
        {
            var whitespace = "        \t";
            var tempDoc = new XmlDocument();
            var nodeToImport = tempDoc.CreateSignificantWhitespace(whitespace);

            var xmlDocument = new XmlDocument();
            var node = xmlDocument.ImportNode(nodeToImport, true);

            Assert.Equal(xmlDocument, node.OwnerDocument);
            Assert.Equal(XmlNodeType.SignificantWhitespace, node.NodeType);
            Assert.Equal(whitespace, node.Value);
        }
Example #4
0
        public static XmlNode CreateNode(XmlDocument doc, XmlNodeType nodeType)
        {
            Assert.NotNull(doc);

            switch (nodeType)
            {
                case XmlNodeType.CDATA:
                    return doc.CreateCDataSection(@"&lt; &amp; <tag> < ! > & </tag> 	 ");
                case XmlNodeType.Comment:
                    return doc.CreateComment(@"comment");
                case XmlNodeType.Element:
                    return doc.CreateElement("E");
                case XmlNodeType.Text:
                    return doc.CreateTextNode("text");
                case XmlNodeType.Whitespace:
                    return doc.CreateWhitespace(@"	  ");
                case XmlNodeType.SignificantWhitespace:
                    return doc.CreateSignificantWhitespace("	");
                default:
                    throw new ArgumentException("Wrong XmlNodeType: '" + nodeType + "'");
            }
        }
Example #5
0
        private XmlNode LoadNode(bool skipOverWhitespace)
        {
            XmlReader      r      = _reader;
            XmlNode        parent = null;
            XmlElement     element;
            IXmlSchemaInfo schemaInfo;

            do
            {
                XmlNode node = null;
                switch (r.NodeType)
                {
                case XmlNodeType.Element:
                    bool fEmptyElement = r.IsEmptyElement;
                    element         = _doc.CreateElement(r.Prefix, r.LocalName, r.NamespaceURI);
                    element.IsEmpty = fEmptyElement;

                    if (r.MoveToFirstAttribute())
                    {
                        XmlAttributeCollection attributes = element.Attributes;
                        do
                        {
                            XmlAttribute attr = LoadAttributeNode();
                            attributes.Append(attr);     // special case for load
                        }while (r.MoveToNextAttribute());
                        r.MoveToElement();
                    }

                    // recursively load all children.
                    if (!fEmptyElement)
                    {
                        if (parent != null)
                        {
                            parent.AppendChildForLoad(element, _doc);
                        }
                        parent = element;
                        continue;
                    }
                    else
                    {
                        schemaInfo = r.SchemaInfo;
                        if (schemaInfo != null)
                        {
                            element.XmlName = _doc.AddXmlName(element.Prefix, element.LocalName, element.NamespaceURI, schemaInfo);
                        }
                        node = element;
                        break;
                    }

                case XmlNodeType.EndElement:
                    if (parent == null)
                    {
                        return(null);
                    }
                    Debug.Assert(parent.NodeType == XmlNodeType.Element);
                    schemaInfo = r.SchemaInfo;
                    if (schemaInfo != null)
                    {
                        element = parent as XmlElement;
                        if (element != null)
                        {
                            element.XmlName = _doc.AddXmlName(element.Prefix, element.LocalName, element.NamespaceURI, schemaInfo);
                        }
                    }
                    if (parent.ParentNode == null)
                    {
                        return(parent);
                    }
                    parent = parent.ParentNode;
                    continue;

                case XmlNodeType.EntityReference:
                    node = LoadEntityReferenceNode(false);
                    break;

                case XmlNodeType.EndEntity:
                    Debug.Assert(parent == null);
                    return(null);

                case XmlNodeType.Attribute:
                    node = LoadAttributeNode();
                    break;

                case XmlNodeType.Text:
                    node = _doc.CreateTextNode(r.Value);
                    break;

                case XmlNodeType.SignificantWhitespace:
                    node = _doc.CreateSignificantWhitespace(r.Value);
                    break;

                case XmlNodeType.Whitespace:
                    if (_preserveWhitespace)
                    {
                        node = _doc.CreateWhitespace(r.Value);
                        break;
                    }
                    else if (parent == null && !skipOverWhitespace)
                    {
                        // if called from LoadEntityReferenceNode, just return null
                        return(null);
                    }
                    else
                    {
                        continue;
                    }

                case XmlNodeType.CDATA:
                    node = _doc.CreateCDataSection(r.Value);
                    break;


                case XmlNodeType.XmlDeclaration:
                    node = LoadDeclarationNode();
                    break;

                case XmlNodeType.ProcessingInstruction:
                    node = _doc.CreateProcessingInstruction(r.Name, r.Value);
                    break;

                case XmlNodeType.Comment:
                    node = _doc.CreateComment(r.Value);
                    break;

                case XmlNodeType.DocumentType:
                    node = LoadDocumentTypeNode();
                    break;

                default:
                    throw UnexpectedNodeType(r.NodeType);
                }

                Debug.Assert(node != null);
                if (parent != null)
                {
                    parent.AppendChildForLoad(node, _doc);
                }
                else
                {
                    return(node);
                }
            }while (r.Read());

            // when the reader ended before full subtree is read, return whatever we have created so far
            if (parent != null)
            {
                while (parent.ParentNode != null)
                {
                    parent = parent.ParentNode;
                }
            }
            return(parent);
        }
Example #6
0
		public void SignificantWhitespaceIgnored2 ()
		{
			// To make sure, create pure significant whitespace element
			// using XmlNodeReader (that does not have xml:space attribute
			// column).
			DataSet ds = new DataSet ();
			XmlDocument doc = new XmlDocument ();
			doc.AppendChild (doc.CreateElement ("root"));
			doc.DocumentElement.AppendChild (doc.CreateSignificantWhitespace
			("      \n\n"));
			XmlReader xr = new XmlNodeReader (doc);
			ds.InferXmlSchema (xr, null);
			AssertDataSet ("pure_whitespace", ds, "root", 0, 0);
		}
    private void DeserializeValue(JsonReader reader, XmlDocument document, XmlNamespaceManager manager, string propertyName, XmlNode currentNode)
    {
      switch (propertyName)
      {
        case TextName:
          currentNode.AppendChild(document.CreateTextNode(reader.Value.ToString()));
          break;
        case CDataName:
          currentNode.AppendChild(document.CreateCDataSection(reader.Value.ToString()));
          break;
        case WhitespaceName:
          currentNode.AppendChild(document.CreateWhitespace(reader.Value.ToString()));
          break;
        case SignificantWhitespaceName:
          currentNode.AppendChild(document.CreateSignificantWhitespace(reader.Value.ToString()));
          break;
        default:
          // processing instructions and the xml declaration start with ?
          if (!string.IsNullOrEmpty(propertyName) && propertyName[0] == '?')
          {
            if (propertyName == DeclarationName)
            {
              string version = null;
              string encoding = null;
              string standalone = null;
              while (reader.Read() && reader.TokenType != JsonToken.EndObject)
              {
                switch (reader.Value.ToString())
                {
                  case "@version":
                    reader.Read();
                    version = reader.Value.ToString();
                    break;
                  case "@encoding":
                    reader.Read();
                    encoding = reader.Value.ToString();
                    break;
                  case "@standalone":
                    reader.Read();
                    standalone = reader.Value.ToString();
                    break;
                  default:
                    throw new JsonSerializationException("Unexpected property name encountered while deserializing XmlDeclaration: " + reader.Value);
                }
              }

              XmlDeclaration declaration = document.CreateXmlDeclaration(version, encoding, standalone);
              currentNode.AppendChild(declaration);
            }
            else
            {
              XmlProcessingInstruction instruction = document.CreateProcessingInstruction(propertyName.Substring(1), reader.Value.ToString());
              currentNode.AppendChild(instruction);
            }
          }
          else
          {
            // deserialize xml element
            bool finishedAttributes = false;
            bool finishedElement = false;
            string elementPrefix = GetPrefix(propertyName);
            Dictionary<string, string> attributeNameValues = new Dictionary<string, string>();

            // a string token means the element only has a single text child
            if (reader.TokenType != JsonToken.String
              && reader.TokenType != JsonToken.Null
              && reader.TokenType != JsonToken.Boolean
              && reader.TokenType != JsonToken.Integer
              && reader.TokenType != JsonToken.Float
              && reader.TokenType != JsonToken.Date
              && reader.TokenType != JsonToken.StartConstructor)
            {
              // read properties until first non-attribute is encountered
              while (!finishedAttributes && !finishedElement && reader.Read())
              {
                switch (reader.TokenType)
                {
                  case JsonToken.PropertyName:
                    string attributeName = reader.Value.ToString();

                    if (attributeName[0] == '@')
                    {
                      attributeName = attributeName.Substring(1);
                      reader.Read();
                      string attributeValue = reader.Value.ToString();
                      attributeNameValues.Add(attributeName, attributeValue);

                      string namespacePrefix;

                      if (IsNamespaceAttribute(attributeName, out namespacePrefix))
                      {
                        manager.AddNamespace(namespacePrefix, attributeValue);
                      }
                    }
                    else
                    {
                      finishedAttributes = true;
                    }
                    break;
                  case JsonToken.EndObject:
                    finishedElement = true;
                    break;
                  default:
                    throw new JsonSerializationException("Unexpected JsonToken: " + reader.TokenType);
                }
              }
            }

            // have to wait until attributes have been parsed before creating element
            // attributes may contain namespace info used by the element
            XmlElement element = (!string.IsNullOrEmpty(elementPrefix))
                    ? document.CreateElement(propertyName, manager.LookupNamespace(elementPrefix))
                    : document.CreateElement(propertyName);

            currentNode.AppendChild(element);

            // add attributes to newly created element
            foreach (KeyValuePair<string, string> nameValue in attributeNameValues)
            {
              string attributePrefix = GetPrefix(nameValue.Key);

              XmlAttribute attribute = (!string.IsNullOrEmpty(attributePrefix))
                      ? document.CreateAttribute(nameValue.Key, manager.LookupNamespace(attributePrefix))
                      : document.CreateAttribute(nameValue.Key);

              attribute.Value = nameValue.Value;

              element.SetAttributeNode(attribute);
            }

            if (reader.TokenType == JsonToken.String)
            {
              element.AppendChild(document.CreateTextNode(reader.Value.ToString()));
            }
            else if (reader.TokenType == JsonToken.Integer)
            {
              element.AppendChild(document.CreateTextNode(XmlConvert.ToString((long)reader.Value)));
            }
            else if (reader.TokenType == JsonToken.Float)
            {
              element.AppendChild(document.CreateTextNode(XmlConvert.ToString((double)reader.Value)));
            }
            else if (reader.TokenType == JsonToken.Boolean)
            {
              element.AppendChild(document.CreateTextNode(XmlConvert.ToString((bool)reader.Value)));
            }
            else if (reader.TokenType == JsonToken.Date)
            {
              DateTime d = (DateTime)reader.Value;
              element.AppendChild(document.CreateTextNode(XmlConvert.ToString(d, DateTimeUtils.ToSerializationMode(d.Kind))));
            }
            else if (reader.TokenType == JsonToken.Null)
            {
              // empty element. do nothing
            }
            else
            {
              // finished element will have no children to deserialize
              if (!finishedElement)
              {
                manager.PushScope();

                DeserializeNode(reader, document, manager, element);

                manager.PopScope();
              }
            }
          }
          break;
      }
    }
Example #8
0
        private static int Detokenize(Tuplet<ArrayDiffKind, Token>[] tokens, int index, XmlElement current, XmlDocument doc)
        {
            for(; index < tokens.Length; ++index) {
                Tuplet<ArrayDiffKind, Token> token = tokens[index];
                switch(token.Item1) {
                case ArrayDiffKind.Same:
                case ArrayDiffKind.Added:
                    switch(token.Item2.Type) {
                    case XmlNodeType.CDATA:
                        if(current == null) {
                            throw new ArgumentNullException("current");
                        }
                        current.AppendChild(doc.CreateCDataSection(token.Item2.Value));
                        break;
                    case XmlNodeType.Comment:
                        if(current == null) {
                            throw new ArgumentNullException("current");
                        }
                        current.AppendChild(doc.CreateComment(token.Item2.Value));
                        break;
                    case XmlNodeType.SignificantWhitespace:
                        if(current == null) {
                            throw new ArgumentNullException("current");
                        }
                        current.AppendChild(doc.CreateSignificantWhitespace(token.Item2.Value));
                        break;
                    case XmlNodeType.Text:
                        if(current == null) {
                            throw new ArgumentNullException("current");
                        }
                        current.AppendChild(doc.CreateTextNode(token.Item2.Value));
                        break;
                    case XmlNodeType.Whitespace:
                        if(current == null) {
                            throw new ArgumentNullException("current");
                        }
                        current.AppendChild(doc.CreateWhitespace(token.Item2.Value));
                        break;
                    case XmlNodeType.Element:
                        XmlElement next = doc.CreateElement(token.Item2.Value);
                        if(current == null) {
                            doc.AppendChild(next);
                        } else {
                            current.AppendChild(next);
                        }
                        index = Detokenize(tokens, index + 1, next, doc);
                        break;
                    case XmlNodeType.Attribute:
                        if(current == null) {
                            throw new ArgumentNullException("current");
                        }
                        string[] parts = token.Item2.Value.Split(new char[] { '=' }, 2);
                        current.SetAttribute(parts[0], parts[1]);
                        break;
                    case XmlNodeType.EndElement:

                        // nothing to do
                        break;
                    case XmlNodeType.None:
                        if(current == null) {
                            throw new ArgumentNullException("current");
                        }

                        // ensure we're closing the intended element
                        if(token.Item2.Value != current.Name) {
                            throw new InvalidOperationException(string.Format("mismatched element ending; found </{0}>, expected </{1}>", token.Item2.Value, current.Name));
                        }

                        // we're done with this sequence
                        return index;
                    default:
                        throw new InvalidOperationException("unhandled node type: " + token.Item2.Type);
                    }
                    break;
                case ArrayDiffKind.Removed:

                    // ignore removed nodes
                    break;
                default:
                    throw new InvalidOperationException("invalid diff kind: " + token.Item1);
                }
            }
            if(current != null) {
                throw new InvalidOperationException("unexpected end of tokens");
            }
            return index;
        }
Example #9
0
 public void AsText_on_element_concats_whitespace_text_significant_whitespace_and_CDATA()
 {
     var document = new XmlDocument();
     document.AppendChild(document.CreateElement("test"));
     var x = document.CreateElement("x");
     document.DocumentElement.AppendChild(x);
     x.AppendChild(document.CreateTextNode("foo"));
     x.AppendChild(document.CreateWhitespace(" "));
     x.AppendChild(document.CreateCDataSection("bar"));
     x.AppendChild(document.CreateSignificantWhitespace(" "));
     var doc = new XDoc(document);
     Assert.AreEqual("foo bar ", doc["x"].AsText);
 }
Example #10
0
 public void AsText_on_significant_whitespace_should_return_value()
 {
     var document = new XmlDocument();
     document.AppendChild(document.CreateElement("test"));
     var x = document.CreateElement("x");
     document.DocumentElement.AppendChild(x);
     x.AppendChild(document.CreateSignificantWhitespace(" "));
     var doc = new XDoc(document);
     Assert.AreEqual(" ", doc["x"][0].AsText);
 }
Example #11
0
		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);
		}
Example #12
0
        public bool UpdateDirTree()
        {
            if (!GetAllDirNames())
            {
                return false;
            }
            XmlDocument doc = new XmlDocument();
            doc.PreserveWhitespace = true;
            doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", null));

            doc.AppendChild(doc.CreateSignificantWhitespace("\r\n"));
            XmlElement ele = doc.CreateElement("Root");
            doc.AppendChild(ele);
            XmlElement ele2 = doc.CreateElement("1");
            XmlElement ele3 = doc.CreateElement("2");
            ele.AppendChild(ele2);
            ele.AppendChild(ele3);

            if (AddChildNode(ref doc, ref ele2))
            {
                m_dirProvider.Document = doc;
                return true;
            }
            else
            {
                return false;
            }
        }