Esempio n. 1
0
        public void WriteStringTest()
        {
            using (var memStream = new MemoryStream())
            using (OpenXmlPartWriter target = new OpenXmlPartWriter(memStream))
            {
                string text = "abc";
                target.WriteStartDocument();
                target.WriteStartElement(new Run());

                target.WriteStartElement(new Text());
                target.WriteString(text);
                target.WriteEndElement();

                var textElement = new Text();
                textElement.Space = new EnumValue<SpaceProcessingModeValues>();
                textElement.Space.Value = SpaceProcessingModeValues.Preserve;
                target.WriteStartElement(new Text(), textElement.GetAttributes());
                target.WriteString(text);
                target.WriteString(text);
                target.WriteEndElement();

                var run = new Run(new Text("111"), new Text("222"));
                using (var reader = OpenXmlReader.Create(run))
                {
                    reader.Read();
                    reader.Read();
                    target.WriteStartElement(reader);
                    target.WriteString(reader.GetText());
                    reader.Read();
                    target.WriteEndElement();

                    reader.Read();
                    target.WriteStartElement(reader, textElement.GetAttributes());
                    target.WriteString(reader.GetText());
                    target.WriteString(text);
                    reader.Read();
                    target.WriteEndElement();

                    reader.Close();
                }
                target.Close();
            }
        }
Esempio n. 2
0
        public void WriterWithNsTest()
        {
            using (var memStream = new MemoryStream())
            {
                using (OpenXmlPartWriter target = new OpenXmlPartWriter(memStream))
                {
                    target.WriteStartDocument();
                    target.WriteStartElement(new Document());

                    target.WriteStartElement(new Body(), new List<OpenXmlAttribute>() { new OpenXmlAttribute("a:a", "http://aa", "ab") });

                    target.WriteStartElement(new Paragraph(), null, null);
                    target.WriteEndElement();

                    List<KeyValuePair<string, string>> ns = new List<KeyValuePair<string, string>>();
                    KeyValuePair<string, string> item = new KeyValuePair<string, string>("b", "http://aaa");
                    ns.Add(item);
                    target.WriteStartElement(new Table(), null, ns);
                    target.WriteEndElement();

                    target.WriteEndElement();
                    
                    target.WriteEndElement();
                    target.Close();
                }
                memStream.Flush();
                memStream.Seek(0, SeekOrigin.Begin);

                using (var reader = new OpenXmlPartReader(memStream))
                {
                    //<w:document>
                    reader.Read();
                    Assert.Equal(1, reader.NamespaceDeclarations.Count());

                    //<w:body>
                    reader.Read();
                    Assert.Equal(1, reader.NamespaceDeclarations.Count());

                    //<w:p>
                    reader.ReadFirstChild();
                    Assert.True(reader.ElementType.Name.EndsWith("Paragraph"));
                    Assert.Equal(0, reader.NamespaceDeclarations.Count());

                    //<w:p>
                    reader.ReadNextSibling();
                    Assert.True(reader.ElementType.Name.EndsWith("Table"));
                    Assert.Equal(1, reader.NamespaceDeclarations.Count());


                }
            }
        }
Esempio n. 3
0
        public void WriteStringExceptionTest6()
        {
            using (var memStream = new MemoryStream())
            using (OpenXmlPartWriter target = new OpenXmlPartWriter(memStream))
            {
                string text = "abc";
                target.WriteStartDocument();
                target.WriteStartElement(new Run());

                var textElement = new Text();
                textElement.Space = SpaceProcessingModeValues.Preserve;
                target.WriteStartElement(new RunProperties(), textElement.GetAttributes());
                target.WriteEndElement();
                target.Close();
                Assert.Throws<System.InvalidOperationException>(() =>
                {
                    target.WriteString(text); // exception
                });
            }
        }