예제 #1
0
파일: SDMNode.cs 프로젝트: johnhhm/corefx
                /// <summary>
                /// Tests the Parent property on Node.
                /// </summary>
                /// <param name="context"></param>
                /// <returns></returns>
                //[Variation(Desc = "NodeParent")]
                public void NodeParent()
                {
                    // Only elements are returned as parents from the Parent property.
                    // Documents are not returned.
                    XDocument document = new XDocument();

                    XNode[] nodes = new XNode[]
                        {
                            new XComment("comment"),
                            new XElement("element"),
                            new XProcessingInstruction("target", "data"),
                            new XDocumentType("name", "publicid", "systemid", "internalsubset")
                        };

                    foreach (XNode node in nodes)
                    {
                        Validate.IsNull(node.Parent);
                        document.Add(node);
                        Validate.IsReferenceEqual(document, node.Document);
                        // Parent element is null.
                        Validate.IsNull(node.Parent);
                        document.RemoveNodes();
                    }

                    // Now test the cases where an element is the parent.
                    nodes = new XNode[]
                        {
                            new XComment("abcd"),
                            new XElement("nested"),
                            new XProcessingInstruction("target2", "data2"),
                            new XText("text")
                        };

                    XElement root = new XElement("root");
                    document.ReplaceNodes(root);

                    foreach (XNode node in nodes)
                    {
                        Validate.IsNull(node.Parent);

                        root.AddFirst(node);

                        Validate.IsReferenceEqual(node.Parent, root);

                        root.RemoveNodes();
                        Validate.IsNull(node.Parent);
                    }
                }
예제 #2
0
 public void ExecuteXDocumentVariation(XNode toReplace)
 {
     XNode newValue = new XText(" ");
     XDocument xDoc = new XDocument(toReplace);
     XDocument xDocOriginal = new XDocument(xDoc);
     using (UndoManager undo = new UndoManager(xDoc))
     {
         undo.Group();
         using (EventsHelper docHelper = new EventsHelper(xDoc))
         {
             xDoc.ReplaceNodes(newValue);
             Assert.True(xDoc.Nodes().Count() == 1, "Not all content were removed");
             Assert.True(Object.ReferenceEquals(xDoc.FirstNode, newValue), "Did not replace correctly");
             docHelper.Verify(new XObjectChange[] { XObjectChange.Remove, XObjectChange.Add }, new XObject[] { toReplace, newValue });
         }
         undo.Undo();
         Assert.True(XNode.DeepEquals(xDoc, xDocOriginal), "Undo did not work!");
     }
 }
        internal void SubstitutePlaceHolders(XDocument doc, DistributedConfigurationSubstitutions configurationSubstitutions)
        {
            var descendantComments = doc.DescendantNodes().OfType<XComment>();

            var comments = (from comment in descendantComments
                            let match = TypeIdentifier.Match(comment.Value)
                            where match.Success
                            select new { XComment = comment, Match = match }).ToList();

            foreach (var comment in comments)
            {
                if (!comment.Match.Groups["selector"].Value.Equals("Replace"))
                    continue;

                var parameters = new JavaScriptSerializer().DeserializeObject(comment.Match.Groups["object"].Value) as Dictionary<string, object>;
                var xPath = parameters["XPath"] as string;
                var value = parameters["Value"] as string;

                var node = (comment.XComment.XPathEvaluate(xPath) as IEnumerable<object>).FirstOrDefault();
                if (node is XElement)
                    ((XElement)node).SetValue(value);
                else if (node is XAttribute)
                    ((XAttribute)node).SetValue(value);
                comment.XComment.Remove();
            }

            var xmlAsString = doc.ToString(); // not efficient but clear and ok for usually small configs
            xmlAsString = Variable.Replace(xmlAsString, match =>
                                              {
                                                  var variableName = match.Groups["varName"].Value;
                                                  var variable = configurationSubstitutions.Variables
                                                      .FirstOrDefault(v => v.Name.Equals(variableName));

                                                  if (variable == null)
                                                     return match.Value;

                                                  return variable.Value;
                                              });
            xmlAsString = Escaping.Replace(xmlAsString, string.Empty);
            var doc2 = XDocument.Parse(xmlAsString);
            doc.ReplaceNodes(doc2.Nodes());
        }
예제 #4
0
 public static XElement GetMediaRoot(XDocument xdoc)
 {
     if (xdoc.Root == null || xdoc.Root.Name.ToString() != ElementMedia)
     {
         xdoc.ReplaceNodes(new XElement(ElementMedia));
     }
     return xdoc.Root;
 }