当前位置: 首页>>代码示例>>C#>>正文


C# XDocument.ReplaceNodes方法代码示例

本文整理汇总了C#中System.Xml.Linq.XDocument.ReplaceNodes方法的典型用法代码示例。如果您正苦于以下问题:C# XDocument.ReplaceNodes方法的具体用法?C# XDocument.ReplaceNodes怎么用?C# XDocument.ReplaceNodes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Xml.Linq.XDocument的用法示例。


在下文中一共展示了XDocument.ReplaceNodes方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: NodeParent

                /// <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);
                    }
                }
开发者ID:johnhhm,项目名称:corefx,代码行数:54,代码来源:SDMNode.cs

示例2: ExecuteXDocumentVariation

 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!");
     }
 }
开发者ID:ChuangYang,项目名称:corefx,代码行数:19,代码来源:EventsReplace.cs

示例3: SubstitutePlaceHolders

        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());
        }
开发者ID:ayezutov,项目名称:NDistribUnit,代码行数:42,代码来源:DistributedConfigurationOperator.cs

示例4: GetMediaRoot

 public static XElement GetMediaRoot(XDocument xdoc)
 {
     if (xdoc.Root == null || xdoc.Root.Name.ToString() != ElementMedia)
     {
         xdoc.ReplaceNodes(new XElement(ElementMedia));
     }
     return xdoc.Root;
 }
开发者ID:richinoz,项目名称:Orchard1.6,代码行数:8,代码来源:PrettyGalleryService.cs


注:本文中的System.Xml.Linq.XDocument.ReplaceNodes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。