Esempio n. 1
0
        public static XElement AddWithIndentation(this XElement element, XElement newContent)
        {
            int      indentationLevel = 0;
            XElement tmp = element;

            while (tmp != null)
            {
                tmp = tmp.Parent;
                indentationLevel++;
            }
            if (!element.Nodes().Any())
            {
                element.Add(new XText(Environment.NewLine + GetIndentation(indentationLevel - 1)));
            }
            XText whitespace = element.Nodes().Last() as XText;

            if (whitespace != null && string.IsNullOrWhiteSpace(whitespace.Value))
            {
                whitespace.AddBeforeSelf(new XText(Environment.NewLine + GetIndentation(indentationLevel)));
                whitespace.AddBeforeSelf(newContent = FormatXml(newContent, indentationLevel));
            }
            else
            {
                element.Add(new XText(Environment.NewLine + GetIndentation(indentationLevel)));
                element.Add(newContent = FormatXml(newContent, indentationLevel));
            }
            return(newContent);
        }
Esempio n. 2
0
        public static void ElementsBeforeSelfWithXNameBeforeAndAfter()
        {
            XText    aText = new XText("a"), bText = new XText("b");
            XElement a = new XElement("A", aText), b = new XElement("B", bText);

            aText.AddBeforeSelf(b);
            IEnumerable <XElement> nodes = aText.ElementsBeforeSelf("B");

            Assert.Equal(1, nodes.Count());
            b.Remove();
            Assert.Equal(0, nodes.Count());
        }
Esempio n. 3
0
        public static void AddBeforeSelfIndented(this XNode self, XElement addedElement)
        {
            string selfIndent;

            if (TryGetElementIndent(self, out selfIndent))
            {
                IndentDescendantNodes(addedElement, selfIndent);
                var newLineAndIndent = new XText("\n" + selfIndent);
                self.AddBeforeSelf(newLineAndIndent);
                newLineAndIndent.AddBeforeSelf(addedElement);
            }
            else
            {
                // Ignore indent
                self.AddBeforeSelf(addedElement);
            }
        }
Esempio n. 4
0
 public static void ElementsBeforeSelfWithXNameBeforeAndAfter()
 {
     XText aText = new XText("a"), bText = new XText("b");
     XElement a = new XElement("A", aText), b = new XElement("B", bText);
     aText.AddBeforeSelf(b);
     IEnumerable<XElement> nodes = aText.ElementsBeforeSelf("B");
     Assert.Equal(1, nodes.Count());
     b.Remove();
     Assert.Equal(0, nodes.Count());
 }