/// <summary>
        /// Inserts the lines.
        /// </summary>
        /// <param name="project">The project.</param>
        /// <param name="lineCount">The line count.</param>
        private void InsertLines(
            Project project,
            int lineCount)
        {
            // Pull out some useful variables.
            ProjectBlockCollection blocks = project.Blocks;

            // Modify the first line, which is always there.
            using (blocks[0].AcquireBlockLock(RequestLock.Write))
            {
                blocks[0].SetText("Line 1");
            }

            // Add in the additional lines after the first one.
            for (int i = 1;
                 i < lineCount;
                 i++)
            {
                var block = new Block(blocks);
                using (block.AcquireBlockLock(RequestLock.Write))
                {
                    block.SetText("Line " + (i + 1));
                }
                blocks.Add(block);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Reads the content file, either from the project reader or the Structure
        /// file depending on the persistence settings.
        /// </summary>
        /// <param name="projectReader">The project reader.</param>
        public void Read(XmlReader projectReader)
        {
            // Figure out which reader we'll be using.
            bool      createdReader;
            XmlReader reader = GetXmlReader(
                projectReader, Settings.ContentFilename, out createdReader);

            // Loop through the resulting file until we get to the end of the
            // XML element we care about.
            ProjectBlockCollection blocks = Project.Blocks;
            bool reachedContents          = reader.NamespaceURI == XmlConstants.ProjectNamespace &&
                                            reader.LocalName == "content";
            string    text       = null;
            BlockType blockType  = null;
            bool      firstBlock = true;

            while (reader.Read())
            {
                // Ignore anything outside of our namespace.
                if (reader.NamespaceURI != XmlConstants.ProjectNamespace)
                {
                    continue;
                }

                // Check to see if we're done reading.
                if (reader.NodeType == XmlNodeType.EndElement)
                {
                    switch (reader.LocalName)
                    {
                    case "content":
                        return;

                    case "block":
                        // Create the block and insert it into the list.
                        var block = new Block(blocks, blockType, text);

                        if (firstBlock)
                        {
                            // Because we have a rule that the collection may
                            // not have less than one block, the first block is
                            // a replacement instead of adding to the list.
                            blocks[0]  = block;
                            firstBlock = false;
                        }
                        else
                        {
                            blocks.Add(block);
                        }

                        break;
                    }
                }

                // For the rest of this loop, we only deal with begin elements.
                if (reader.NodeType != XmlNodeType.Element)
                {
                    continue;
                }

                // If we haven't reached the Structure, we just cycle through the XML.
                if (!reachedContents)
                {
                    // Flip the flag if we are starting to read the Structure.
                    if (reader.NamespaceURI == XmlConstants.ProjectNamespace &&
                        reader.LocalName == "content")
                    {
                        reachedContents = true;
                    }

                    // Continue on since we're done with this if clause.
                    continue;
                }

                // We process the remaining elements based on their local name.
                switch (reader.LocalName)
                {
                case "type":
                    string blockTypeName = reader.ReadString();
                    blockType = Project.BlockTypes[blockTypeName];
                    break;

                case "text":
                    text = reader.ReadString();
                    break;
                }
            }

            // If we created the reader, close it.
            if (createdReader)
            {
                reader.Close();
                reader.Dispose();
            }
        }