本文整理汇总了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);
}
}
示例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!");
}
}
示例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());
}
示例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;
}