Example #1
0
    /// <summary>
    /// Constructs an XmlStorage.
    /// This is used to create sub-sections
    /// </summary>
    /// <param name="root">Root XmlStorage instance</param>
    /// <param name="name">Name of subsection</param>
    /// <param name="reader">Xml reader to read elements from</param>
    private XmlStorage(XmlStorage root, string name, Xml.XmlTextReader reader)
    {
        this.isRoot = false;
        this.root = root;
        this.name = name;

        ReadElements(reader);
    }
        private static string GetElementText(Xml.XmlElement element)
        {
            foreach (Xml.XmlNode n in element.ChildNodes)
            {
                if (n is Xml.XmlText)
                {
                    Xml.XmlText textNode = (Xml.XmlText)n;
                    return textNode.Value;
                }
            }

            return string.Empty;
        }
        private static Chunk ReadStep(Xml.XmlElement element)
        {
            Chunk newChunk = new Chunk(element.Name);

            foreach (Xml.XmlNode n in element.ChildNodes)
            {
                if (n is Xml.XmlElement)
                {
                    Xml.XmlElement subElement = (Xml.XmlElement)n;

                    if (subElement.Name == "Value")
                    {
                        if (subElement.HasAttribute("Name"))
                        {
                            string valueName = subElement.GetAttribute("Name");
                            newChunk.Values[valueName] = GetElementText(subElement);
                        }
                    }
                }
            }

            // Recurse.
            foreach (Xml.XmlNode n in element.ChildNodes)
            {
                if (n is Xml.XmlElement )
                {
                    Xml.XmlElement subElement = (Xml.XmlElement)n;

                    if (subElement.Name != "Value")
                    {
                        newChunk.Add(ReadStep(subElement));
                    }
                }
            }

            return newChunk;
        }
Example #4
0
    /// <summary>
    /// Writes elements to an xml writer
    /// </summary>
    /// <param name="writer"></param>
    private void WriteElements(Xml.XmlTextWriter writer)
    {
        // iterate across all keys in this section and write them to the xml file
        IDictionaryEnumerator enumerator = this.keyTable.GetEnumerator();

        while (enumerator.MoveNext())
        {
            writer.WriteStartElement(keyName);
            writer.WriteAttributeString("name", (string)enumerator.Key);
            writer.WriteString((string)enumerator.Value);

        //			System.Diagnostics.Debug.WriteLine(
        //"Wrote: " + this.name + " " + (string)enumerator.Key + " = " + (string)enumerator.Value);

            writer.WriteEndElement();
        }

        // then do it for all sub sections
        foreach (XmlStorage xml in this.subsectionTable.Values)
        {
            writer.WriteStartElement(subsectionName);
            writer.WriteAttributeString("name", xml.name);

            xml.WriteElements(writer);

            writer.WriteEndElement();
        }
    }
Example #5
0
    /// <summary>
    /// Reads the xml elements from the reader and stores them in the hashtable
    /// </summary>
    /// <param name="reader"></param>
    private void ReadElements(Xml.XmlTextReader reader)
    {
        try
        {
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case Xml.XmlNodeType.Element:

                        // we've hit a value, so read it in
                        if (reader.Name == keyName)
                        {
                            string name = reader.GetAttribute("name");
                            if (name == string.Empty || name == "")
                                throw new ApplicationException();

                            if (!reader.IsEmptyElement && reader.Read())
                            {
        //								System.Diagnostics.Debug.WriteLine("Read: " + this.name + " " + name + " = " + reader.Value);
                                this.keyTable.Add(name, reader.Value);
                            }
                            else
                                this.keyTable.Add(name, string.Empty);
                        }
                            // hit a subsection, read in the name of it
                        else if (reader.Name == subsectionName)
                        {
                            if (!reader.IsEmptyElement)
                            {
                                string name = reader.GetAttribute("name");
                                if (name == string.Empty || name == "")
                                    throw new ApplicationException();

                                this.subsectionTable.Add(name, new XmlStorage(this.root, name, reader));
                            }
                        }

                        break;

                    case Xml.XmlNodeType.EndElement:

                        // end of subsection, party is over
                        if (reader.Name == subsectionName && !this.isRoot)
                            return;
                        break;
                }
            }
        }
        catch (Exception e)
        {
            throw new ApplicationException("Malformed xml storage file", e);
        }
    }
        public void AddConnectionStringElement_throws_if_config_invalid()
        {
            var configContents = new[]
            {
                @"<configuration1 />",
                @"<configuration xmlns=""fakexmlns""/>",
                @"<ns:configuration xmlns:ns=""fakexmlns""/>"
            };

            foreach (var config in configContents)
            {
                var configXml = new XmlDocument();
                configXml.LoadXml(config);

                Assert.Equal(
                    Assert.Throws<XmlException>(
                        () => ConnectionManager.AddConnectionStringElement(configXml, "MyDb", "db=mydb", "fancyDb")).Message,
                    Resources.ConnectionManager_CorruptConfig);
            }
        }
        private static void WriteStep(Xml.XmlTextWriter writer, Chunk chunk)
        {
            // First write out the chunk itself.
            writer.WriteStartElement(chunk.Name);

            // Write out any values.
            foreach (string key in chunk.Values.Keys )
            {
                writer.WriteStartElement("Value");
                writer.WriteAttributeString("Name", key);
                writer.WriteString( chunk.Values[key] );
                writer.WriteEndElement();
            }

            // Recurse children.
            foreach (Chunk c in chunk)
            {
                WriteStep(writer, c);
            }

            writer.WriteEndElement();
        }