Esempio n. 1
0
        public void AddChild_works()
        {
            var xmlDocument = new XmlDocument();

            // setup
            xmlDocument.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!-- comment --><a><b>b-text</b></a>");

            // test
            XmlHelper.AddChildNode(xmlDocument, "a", "<c>content of c</c>");

            Assert.That(xmlDocument.OuterXml,
                        Is.EqualTo(
                            "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!-- comment --><a><b>b-text</b><c>content of c</c></a>"));

            // setup
            xmlDocument.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!-- comment --><a><b>b-text</b></a>");

            // test
            XmlHelper.AddChildNode(xmlDocument, "/a", "<c>content of c</c>");

            Assert.That(xmlDocument.OuterXml,
                        Is.EqualTo(
                            "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!-- comment --><a><b>b-text</b><c>content of c</c></a>"));

            // setup
            xmlDocument.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!-- comment --><a><b>b-text</b></a>");

            // test
            XmlHelper.AddChildNode(xmlDocument, "a/b", "<c>content of c</c>");

            Assert.That(xmlDocument.OuterXml,
                        Is.EqualTo(
                            "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!-- comment --><a><b>b-text<c>content of c</c></b></a>"));

            // setup
            xmlDocument.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!-- comment --><a><b>b-text</b></a>");

            // test
            var exception = Assert.Throws <XpathFoundNothingException>(delegate {
                var unused = XmlHelper.AddChildNode(xmlDocument, "b", "<c>content of c</c>");
            });

            Assert.That(exception.Message, Is.EqualTo(string.Format(XpathFoundNothingException.MsgMask, "b")));
        }