Esempio n. 1
0
        public void NodeReaderReadWithSimpleXml()
        {
            string        xml        = "<root atri='val'><child /></root>";
            XmlNodeReader nodeReader = NodeReaderTestHelper.CreateNodeReader(xml);

            Assert.True(nodeReader.Read()); //Root Element Read
            Assert.Equal("root", nodeReader.Name);
            Assert.Equal(ReadState.Interactive, nodeReader.ReadState);
            Assert.Equal(XmlNodeType.Element, nodeReader.NodeType);

            Assert.True(nodeReader.Read()); //Child Element Read
            Assert.Equal("child", nodeReader.Name);
            Assert.Equal(ReadState.Interactive, nodeReader.ReadState);
            Assert.Equal(XmlNodeType.Element, nodeReader.NodeType);
            Assert.True(nodeReader.IsEmptyElement);

            Assert.True(nodeReader.Read()); //End Element Read
            Assert.Equal("root", nodeReader.Name);
            Assert.Equal(ReadState.Interactive, nodeReader.ReadState);
            Assert.Equal(XmlNodeType.EndElement, nodeReader.NodeType);

            Assert.False(nodeReader.Read()); //EOF Read
            Assert.Equal(ReadState.EndOfFile, nodeReader.ReadState);
            Assert.True(nodeReader.EOF);
            Assert.Equal(XmlNodeType.None, nodeReader.NodeType);

            Assert.False(nodeReader.Read()); //EOF Read
        }
        public void NodeReaderMoveToNextAttributeWithSimpleXml()
        {
            XmlNodeReader nodeReader = NodeReaderTestHelper.CreateNodeReader("<root></root>");

            Assert.True(nodeReader.Read());
            Assert.False(nodeReader.MoveToNextAttribute());
        }
Esempio n. 3
0
        public void NodeReaderReadAttributeValueWithSimpleXml()
        {
            string        xml        = "<root><child /></root>";
            XmlNodeReader nodeReader = NodeReaderTestHelper.CreateNodeReader(xml);

            Assert.True(nodeReader.Read());
            Assert.False(nodeReader.ReadAttributeValue());
        }
        public void NodeReaderMoveToAttributeWithSimpleXml()
        {
            XmlNodeReader nodeReader = NodeReaderTestHelper.CreateNodeReader("<root></root>");

            Assert.True(nodeReader.Read());
            Assert.False(nodeReader.MoveToAttribute(string.Empty));
            Assert.False(nodeReader.MoveToAttribute(string.Empty, null));
            Assert.Throws <ArgumentOutOfRangeException>(() => { nodeReader.MoveToAttribute(0); });
        }
Esempio n. 5
0
        public void NodeReaderSkipWithSimpleXml()
        {
            XmlNodeReader nodeReader = NodeReaderTestHelper.CreateNodeReader("<root atri='val'><child /></root>");

            Assert.True(nodeReader.Read());
            nodeReader.Skip();
            Assert.True(nodeReader.EOF);
            Assert.Equal(ReadState.EndOfFile, nodeReader.ReadState);
            Assert.Equal(XmlNodeType.None, nodeReader.NodeType);
        }
Esempio n. 6
0
        public void NodeReaderGetAttributeWithValidXml()
        {
            string        xml        = "<root attr='val'><child /></root>";
            XmlNodeReader nodeReader = NodeReaderTestHelper.CreateNodeReader(xml);

            Assert.True(nodeReader.Read());
            Assert.Equal("val", nodeReader.GetAttribute("attr"));
            Assert.Equal("val", nodeReader.GetAttribute("attr", string.Empty));
            Assert.Equal("val", nodeReader.GetAttribute("attr", null));
            Assert.Equal("val", nodeReader.GetAttribute(0));
        }
        public void NodeReaderMoveToNextAttributeWithAttributeXml()
        {
            string        xml        = "<root attr='cal' attr2='val'></root>";
            XmlNodeReader nodeReader = NodeReaderTestHelper.CreateNodeReader(xml);

            Assert.True(nodeReader.Read());
            Assert.True(nodeReader.MoveToNextAttribute());

            nodeReader.ReadContentAsBase64(new byte[33], 10, 10);
            Assert.True(nodeReader.MoveToNextAttribute());
        }
Esempio n. 8
0
        public void NodeReaderLookupNamespaceWithSimpleXml()
        {
            XmlNodeReader nodeReader = NodeReaderTestHelper.CreateNodeReader("<root></root>");

            nodeReader.Read();
            var namespaceResolver = nodeReader as IXmlNamespaceResolver;

            Assert.Null(namespaceResolver.LookupNamespace("prefix"));
            Assert.Collection(namespaceResolver.GetNamespacesInScope(XmlNamespaceScope.All)
                              , kv => Assert.Equal("xml", kv.Key));
            Assert.Empty(namespaceResolver.GetNamespacesInScope(XmlNamespaceScope.Local));
        }
        public void NodeReaderMoveToElementWithSimpleXml()
        {
            string        xml        = "<root attr='cal'><child attr='val'><inner attr='val'></inner></child></root>";
            XmlNodeReader nodeReader = NodeReaderTestHelper.CreateNodeReader(xml);

            Assert.True(nodeReader.Read());
            nodeReader.MoveToFirstAttribute();
            Assert.True(nodeReader.MoveToElement());
            Assert.True(nodeReader.MoveToAttribute("attr"));
            nodeReader.ReadContentAsBase64(new byte[33], 10, 10);
            Assert.True(nodeReader.MoveToElement());
        }
Esempio n. 10
0
        public void NodeReaderReadElementContentAsBinHexWithSimpleXml()
        {
            byte[]        byteData   = Encoding.ASCII.GetBytes("hello world");
            string        xml        = $"<root attr='val'>{BitConverter.ToString(byteData).Replace("-", "")}</root>";
            XmlNodeReader nodeReader = NodeReaderTestHelper.CreateNodeReader(xml);

            Assert.True(nodeReader.Read());
            Assert.True(nodeReader.CanReadBinaryContent);
            var resultArr = new byte[byteData.Length];

            Assert.Equal(byteData.Length, nodeReader.ReadElementContentAsBinHex(resultArr, 0, byteData.Length));
            Assert.Equal(byteData, resultArr);
            Assert.Equal("hello world", Encoding.ASCII.GetString(resultArr));
            Assert.Equal(0, nodeReader.ReadElementContentAsBinHex(new byte[33], 10, 10));
        }
Esempio n. 11
0
        public void NodeReaderReadElementContentAsBase64WithSimpleXml()
        {
            byte[]        byteData   = "hello world" u8.ToArray();
            string        xml        = $"<root attr='val'>{Convert.ToBase64String(byteData)}</root>"; //hello world encoded
            XmlNodeReader nodeReader = NodeReaderTestHelper.CreateNodeReader(xml);

            Assert.True(nodeReader.Read());
            Assert.True(nodeReader.CanReadBinaryContent);
            var resultArr = new byte[byteData.Length];

            Assert.Equal(byteData.Length, nodeReader.ReadElementContentAsBase64(resultArr, 0, byteData.Length));
            Assert.Equal(byteData, resultArr);
            Assert.Equal("hello world", Encoding.ASCII.GetString(resultArr));
            Assert.Equal(0, nodeReader.ReadElementContentAsBase64(new byte[33], 10, 10));
        }
Esempio n. 12
0
        public void NodeReaderReadAttributeValueWithAttributeXml()
        {
            string        xml        = "<root attr='val'><child /></root>";
            XmlNodeReader nodeReader = NodeReaderTestHelper.CreateNodeReader(xml);

            Assert.True(nodeReader.Read());
            Assert.Equal("root", nodeReader.Name);
            Assert.True(nodeReader.MoveToAttribute("attr"));
            Assert.True(nodeReader.ReadAttributeValue());
            Assert.Equal(ReadState.Interactive, nodeReader.ReadState);
            Assert.Equal(XmlNodeType.Text, nodeReader.NodeType);
            Assert.Equal(2, nodeReader.Depth);
            Assert.True(nodeReader.HasValue);
            Assert.Equal("val", nodeReader.Value);
            Assert.Equal(1, nodeReader.AttributeCount);
        }
        public void NodeReaderMoveToAttributeWithAttributeXml()
        {
            string        xml        = "<root catr='tal' xmlns:attr='cal' fatr='gal' xmlns:attr2='val'></root>";
            XmlNodeReader nodeReader = NodeReaderTestHelper.CreateNodeReader(xml);

            Assert.True(nodeReader.Read());
            Assert.True(nodeReader.MoveToAttribute("catr"));
            Assert.True(nodeReader.MoveToAttribute("attr", "http://www.w3.org/2000/xmlns/"));
            nodeReader.MoveToAttribute(0);

            nodeReader.ReadContentAsBase64(new byte[33], 10, 10);
            Assert.True(nodeReader.MoveToAttribute("fatr"));
            nodeReader.ReadContentAsBase64(new byte[33], 10, 10);
            Assert.True(nodeReader.MoveToAttribute("attr2", "http://www.w3.org/2000/xmlns/"));
            nodeReader.ReadContentAsBase64(new byte[33], 10, 10);
            nodeReader.MoveToAttribute(1);
        }
Esempio n. 14
0
        public void NodeReaderReadContentAsBase64WithSimpleXml()
        {
            byte[]        byteData   = Encoding.ASCII.GetBytes("hello world");
            string        xml        = $"<root attr='{Convert.ToBase64String(byteData)}'><child /></root>"; //hello world encoded
            XmlNodeReader nodeReader = NodeReaderTestHelper.CreateNodeReader(xml);

            Assert.True(nodeReader.Read());
            Assert.Equal("root", nodeReader.Name);
            Assert.True(nodeReader.MoveToAttribute("attr"));
            Assert.True(nodeReader.CanReadBinaryContent);
            var resultArr = new byte[byteData.Length];

            Assert.Equal(byteData.Length, nodeReader.ReadContentAsBase64(resultArr, 0, byteData.Length));
            Assert.Equal(byteData, resultArr);
            Assert.Equal("hello world", Encoding.ASCII.GetString(resultArr));
            Assert.Equal(0, nodeReader.ReadContentAsBase64(new byte[33], 10, 10));
        }
Esempio n. 15
0
        public void NodeReaderReadStringWithEntityXml()
        {
            string        fst        = "<!ENTITY fst 'Sample String'>]>";
            string        dtd        = "<!DOCTYPE root[<!ELEMENT root (#PCDATA)>" + fst;
            string        xml        = dtd + "<root>&fst;</root>";
            XmlNodeReader nodeReader = NodeReaderTestHelper.CreateNodeReader(xml);

            Assert.True(nodeReader.Read()); //DTD Read
            Assert.True(nodeReader.Read()); //Root Element Read
            Assert.Equal("root", nodeReader.Name);
            Assert.True(nodeReader.Read()); //EntityReference Read
            Assert.Equal(ReadState.Interactive, nodeReader.ReadState);
            Assert.Equal(XmlNodeType.EntityReference, nodeReader.NodeType);
            nodeReader.ResolveEntity();
            Assert.True(nodeReader.CanResolveEntity);
            Assert.Equal("Sample String", nodeReader.ReadString());
        }
Esempio n. 16
0
        public void NodeReaderReadContentAsBinHexWithSimpleXml()
        {
            byte[]        byteData   = "hello world" u8.ToArray();
            string        xml        = $"<root attr='{BitConverter.ToString(byteData).Replace("-", "")}'><child /></root>";
            XmlNodeReader nodeReader = NodeReaderTestHelper.CreateNodeReader(xml);

            Assert.True(nodeReader.Read());
            Assert.Equal("root", nodeReader.Name);
            Assert.True(nodeReader.MoveToAttribute("attr"));
            Assert.True(nodeReader.CanReadBinaryContent);
            var resultArr = new byte[byteData.Length];

            Assert.Equal(byteData.Length, nodeReader.ReadContentAsBinHex(resultArr, 0, byteData.Length));
            Assert.Equal(byteData, resultArr);
            Assert.Equal("hello world", Encoding.ASCII.GetString(resultArr));
            Assert.Equal(0, nodeReader.ReadContentAsBinHex(new byte[33], 10, 10));
        }
Esempio n. 17
0
        public void NodeReaderLookupNamespaceWithValidXml()
        {
            string xml = "<book xmlns:bk='urn:samples'> " +
                         "<title>Pride And Prejudice</title>" +
                         "<bk:genre>novel</bk:genre>" +
                         "</book>";
            XmlNodeReader nodeReader = NodeReaderTestHelper.CreateNodeReader(xml);

            Assert.Equal(string.Empty, nodeReader.LocalName);
            Assert.Equal(string.Empty, nodeReader.Name);
            Assert.Equal(string.Empty, nodeReader.NamespaceURI);
            Assert.Equal(string.Empty, nodeReader.Prefix);
            var namespaceResolver = nodeReader as IXmlNamespaceResolver;

            Assert.Equal(string.Empty, namespaceResolver.LookupNamespace(string.Empty));

            Assert.True(nodeReader.Read());
            Assert.True(nodeReader.Read());
            Assert.True(nodeReader.Read());
            Assert.Null(nodeReader.LookupNamespace(string.Empty));
            namespaceResolver = nodeReader as IXmlNamespaceResolver;
            Assert.Equal(string.Empty, namespaceResolver.LookupNamespace(string.Empty));

            Assert.True(nodeReader.Read());
            Assert.True(nodeReader.Read());
            Assert.Equal("genre", nodeReader.LocalName);
            Assert.Equal("bk:genre", nodeReader.Name);
            Assert.Equal("urn:samples", nodeReader.LookupNamespace(nodeReader.Prefix));
            Assert.Equal("bk", nodeReader.Prefix);

            namespaceResolver = nodeReader as IXmlNamespaceResolver;
            Assert.Equal("bk", namespaceResolver.LookupPrefix("urn:samples"));
            Assert.Equal("urn:samples", namespaceResolver.LookupNamespace("bk"));
            IDictionary <string, string> namespaces = namespaceResolver.GetNamespacesInScope(XmlNamespaceScope.All);

            Assert.True(namespaces.ContainsKey("bk") && namespaces["bk"].Equals("urn:samples"));
            Assert.True(namespaces.ContainsKey("xml"));
        }