static void trimWhitespace(XmlDocumentFragment fragment)
        {
            while (fragment.FirstChild != null && fragment.FirstChild.NodeType == XmlNodeType.Whitespace)
            {
                fragment.RemoveChild(fragment.FirstChild);
            }

            while (fragment.LastChild != null && fragment.LastChild.NodeType == XmlNodeType.Whitespace)
            {
                fragment.RemoveChild(fragment.LastChild);
            }
        }
示例#2
0
        public void Set <T>(string namespce, string key, T value)
        {
            lock (xml_document) {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                StringWriter  writer     = new StringWriter();
                serializer.Serialize(writer, value);
                XmlDocumentFragment fragment = xml_document.CreateDocumentFragment();
                fragment.InnerXml = writer.ToString();
                writer.Close();

                if (fragment.FirstChild is XmlDeclaration)
                {
                    fragment.RemoveChild(fragment.FirstChild); // This is only a problem with Microsoft's System.Xml
                }

                XmlNode namespace_node = GetNamespaceNode(namespce == null
                    ? new string [] { null_namespace }
                    : namespce.Split('.'), true);

                bool found = false;
                foreach (XmlNode node in namespace_node.ChildNodes)
                {
                    if (node.Attributes[tag_identifier_attribute_name].Value == key && node.Name == value_tag_name)
                    {
                        node.InnerXml = fragment.InnerXml;
                        found         = true;
                        break;
                    }
                }
                if (!found)
                {
                    XmlNode      new_node  = xml_document.CreateElement(value_tag_name);
                    XmlAttribute attribute = xml_document.CreateAttribute(tag_identifier_attribute_name);
                    attribute.Value = key;
                    new_node.Attributes.Append(attribute);
                    new_node.AppendChild(fragment);
                    namespace_node.AppendChild(new_node);
                }
                QueueWrite();
            }
        }