本文整理汇总了C#中System.Xml.Linq.XDocument.RemoveNodes方法的典型用法代码示例。如果您正苦于以下问题:C# XDocument.RemoveNodes方法的具体用法?C# XDocument.RemoveNodes怎么用?C# XDocument.RemoveNodes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Linq.XDocument
的用法示例。
在下文中一共展示了XDocument.RemoveNodes方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VerifyRemoveNodes
private void VerifyRemoveNodes(XDocument doc)
{
IEnumerable<XNode> nodesBefore = doc.Nodes().ToList();
XDeclaration decl = doc.Declaration;
doc.RemoveNodes();
TestLog.Compare(doc.Nodes().IsEmpty(), "e.Nodes().IsEmpty()");
TestLog.Compare(nodesBefore.Where(n => n.Parent != null).IsEmpty(), "nodesBefore.Where(n=>n.Parent!=null).IsEmpty()");
TestLog.Compare(nodesBefore.Where(n => n.Document != null).IsEmpty(), "nodesBefore.Where(n=>n.Parent!=null).IsEmpty()");
TestLog.Compare(doc.Declaration == decl, "doc.Declaration == decl");
}
示例2: 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);
}
}
示例3: ValidAddIntoXDocument
public void ValidAddIntoXDocument(TestedFunction testedFunction, CalculateExpectedValues calculateExpectedValues)
{
runWithEvents = (bool)Params[0];
var isConnected = (bool)Variation.Params[0];
var combCount = (int)Variation.Params[1];
object[] nodes = { new XDocumentType("root", "", "", ""), new XDocumentType("roo2t", "", "", ""), new XProcessingInstruction("PI", "data"), new XComment("Comment"), new XElement("elem1"), new XElement("elem2", new XElement("C", "nodede")), new XText(""), new XText(" "), new XText("\t"), new XCData(""), new XCData("<A/>"), " ", "\t", "", null };
object[] origNodes = { new XProcessingInstruction("OO", "oo"), new XComment("coco"), " ", new XDocumentType("root", null, null, null), new XElement("anUnexpectedlyLongNameForTheRootElement") };
if (isConnected)
{
new XElement("foo", nodes.Where(n => n is XNode && !(n is XDocumentType)));
foreach (XNode nn in nodes.Where(n => n is XDocumentType))
{
new XDocument(nn);
}
}
foreach (var origs in origNodes.NonRecursiveVariations(2))
{
if (origs.Select(o => new ExpectedValue(false, o)).IsXDocValid())
{
continue;
}
foreach (var o in nodes.NonRecursiveVariations(combCount))
{
var doc = new XDocument(origs);
XNode n = doc.FirstNode;
List<ExpectedValue> expNodes = Helpers.ProcessNodes(calculateExpectedValues(doc, 0, o)).ToList();
bool shouldFail = expNodes.IsXDocValid();
try
{
if (runWithEvents)
{
eHelper = new EventsHelper(doc);
}
testedFunction(n, o);
TestLog.Compare(!shouldFail, "should fail - exception expected here");
TestLog.Compare(expNodes.EqualAll(doc.Nodes(), XNode.EqualityComparer), "nodes does not pass");
}
catch (InvalidOperationException ex)
{
TestLog.Compare(shouldFail, "Unexpected exception : " + ex.Message);
}
catch (ArgumentException ex)
{
TestLog.Compare(shouldFail, "Unexpected exception : " + ex.Message);
}
finally
{
doc.RemoveNodes();
}
}
}
}
示例4: ExecuteXDocumentVariation
public void ExecuteXDocumentVariation()
{
XNode[] content = Variation.Params as XNode[];
XDocument xDoc = new XDocument(content);
XDocument xDocOriginal = new XDocument(xDoc);
using (UndoManager undo = new UndoManager(xDoc))
{
undo.Group();
using (EventsHelper docHelper = new EventsHelper(xDoc))
{
xDoc.RemoveNodes();
docHelper.Verify(XObjectChange.Remove, content);
}
undo.Undo();
TestLog.Compare(XNode.DeepEquals(xDoc, xDocOriginal), "Undo did not work!");
}
}
示例5: DocumentRoot
/// <summary>
/// Validate behavior of the XDocument Root property.
/// </summary>
/// <returns>true if pass, false if fail</returns>
//[Variation(Desc = "DocumentRoot")]
public void DocumentRoot()
{
XDocument doc = new XDocument();
Validate.IsNull(doc.Root);
XElement e = new XElement("element");
doc.Add(e);
XElement e2 = doc.Root;
Validate.IsReferenceEqual(e2, e);
doc.RemoveNodes();
doc.Add(new XComment("comment"));
Validate.IsNull(doc.Root);
}
示例6: DocumentXmlDeclaration
/// <summary>
/// Validate behavior of the XDocument XmlDeclaration property.
/// </summary>
/// <returns>true if pass, false if fail</returns>
//[Variation(Desc = "DocumentXmlDeclaration")]
public void DocumentXmlDeclaration()
{
XDocument doc = new XDocument();
Validate.IsNull(doc.Declaration);
XDeclaration dec = new XDeclaration("1.0", "utf-16", "yes");
XDocument doc2 = new XDocument(dec);
XDeclaration dec2 = doc2.Declaration;
Validate.IsReferenceEqual(dec2, dec);
doc2.RemoveNodes();
Validate.IsNotNull(doc2.Declaration);
}
示例7: NodeDocument
/// <summary>
/// Tests the Document property on Node.
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
//[Variation(Desc = "NodeDocument")]
public void NodeDocument()
{
XDocument document = new XDocument();
XNode[] topLevelNodes = new XNode[]
{
new XComment("comment"),
new XElement("element"),
new XProcessingInstruction("target", "data"),
};
XNode[] nestedNodes = new XNode[]
{
new XText("abcd"),
new XElement("nested"),
new XProcessingInstruction("target2", "data2")
};
// Test top-level cases.
foreach (XNode node in topLevelNodes)
{
Validate.IsNull(node.Document);
document.Add(node);
Validate.IsReferenceEqual(document, node.Document);
document.RemoveNodes();
}
// Test nested cases.
XElement root = new XElement("root");
document.Add(root);
foreach (XNode node in nestedNodes)
{
Validate.IsNull(node.Document);
root.Add(node);
Validate.IsReferenceEqual(document, root.Document);
root.RemoveNodes();
}
}
示例8: DocumentXmlDeclaration
public void DocumentXmlDeclaration()
{
XDocument doc = new XDocument();
Assert.Null(doc.Declaration);
XDeclaration dec = new XDeclaration("1.0", "utf-16", "yes");
XDocument doc2 = new XDocument(dec);
Assert.Same(dec, doc2.Declaration);
doc2.RemoveNodes();
Assert.NotNull(doc2.Declaration);
}
示例9: DocumentRoot
public void DocumentRoot()
{
XDocument doc = new XDocument();
Assert.Null(doc.Root);
XElement e = new XElement("element");
doc.Add(e);
Assert.Same(e, doc.Root);
doc.RemoveNodes();
doc.Add(new XComment("comment"));
Assert.Null(doc.Root);
}
示例10: TestReplacement
private void TestReplacement(XDocument e, object[] nodes, int numOfNodes)
{
for (int i = 0; i < e.Nodes().Count(); i++)
{
object[] allowedNodes = e.Nodes().ElementAt(i) is XElement ? nodes.Where(o => !(o is XElement)).ToArray() : nodes;
foreach (var replacement in nodes.NonRecursiveVariations(numOfNodes))
{
bool shouldFail = false;
var doc = new XDocument(e);
XNode toReplace = doc.Nodes().ElementAt(i);
XNode prev = toReplace.PreviousNode;
XNode next = toReplace.NextNode;
bool isReplacementOriginal = replacement.Where(o => o is XNode).Where(o => (o as XNode).Document != null).IsEmpty();
IEnumerable<ExpectedValue> expValues = ReplaceWithExpValues(doc, toReplace, replacement).ProcessNodes().ToList();
// detect invalid states
shouldFail = expValues.IsXDocValid();
try
{
if (_runWithEvents)
{
_eHelper = new EventsHelper(doc);
}
toReplace.ReplaceWith(replacement);
TestLog.Compare(!shouldFail, "Should fail ... ");
TestLog.Compare(toReplace.Parent == null, "toReplace.Parent == null");
TestLog.Compare(toReplace.Document == null, "toReplace.Document == null");
TestLog.Compare(expValues.EqualAll(doc.Nodes(), XNode.EqualityComparer), "expected values");
if (isReplacementOriginal)
{
TestLog.Compare(replacement.Where(o => o is XNode).Where(o => (o as XNode).Document != doc.Document).IsEmpty(), "replacement.Where(o=>o is XNode).Where(o=>(o as XNode).Document!=doc.Document).IsEmpty()");
}
}
catch (InvalidOperationException)
{
TestLog.Compare(shouldFail, "Exception not expected here");
}
catch (ArgumentException)
{
TestLog.Compare(shouldFail, "Exception not expected here");
}
finally
{
doc.RemoveNodes();
}
}
}
}
示例11: RunValidTests
/// <summary>
/// Runs test for valid cases
/// </summary>
/// <param name="nodeType">XElement/XAttribute</param>
/// <param name="name">name to be tested</param>
public void RunValidTests(string nodeType, string name)
{
XDocument xDocument = new XDocument();
XElement element = null;
try
{
switch (nodeType)
{
case "XElement":
element = new XElement(name, name);
xDocument.Add(element);
IEnumerable<XNode> nodeList = xDocument.Nodes();
TestLog.Compare(nodeList.Count(), 1, "Failed to create element { " + name + " }");
xDocument.RemoveNodes();
break;
case "XAttribute":
element = new XElement(name, name);
XAttribute attribute = new XAttribute(name, name);
element.Add(attribute);
xDocument.Add(element);
XAttribute x = element.Attribute(name);
TestLog.Compare(x.Name.LocalName, name, "Failed to get the added attribute");
xDocument.RemoveNodes();
break;
case "XName":
XName xName = XName.Get(name, name);
TestLog.Compare(xName.LocalName.Equals(name), "Invalid LocalName");
TestLog.Compare(xName.NamespaceName.Equals(name), "Invalid Namespace Name");
TestLog.Compare(xName.Namespace.NamespaceName.Equals(name), "Invalid Namespace Name");
break;
default:
break;
}
}
catch (Exception e)
{
TestLog.WriteLine(nodeType + "failed to create with a valid name { " + name + " }");
throw new TestFailedException(e.ToString());
}
}
示例12: Document1
//[Variation(Priority = 0, Desc = "XDocument : dynamic")]
public void Document1()
{
object[] content = new object[] {
new object [] {new string [] {" ", null, " "}, " "},
new object [] {new string [] {" "," \t"}, new XText(" \t")},
new object [] {new XText [] {new XText(" "), new XText("\t")}, new XText(" \t")},
new XDocumentType ("root", "", "", ""),
new XProcessingInstruction ("PI1",""),
new XText("\n"),
new XText("\t"),
new XText(" "),
new XProcessingInstruction ("PI1",""),
new XElement ("myroot"),
new XProcessingInstruction ("PI2","click"),
new object [] {new XElement ("X", new XAttribute ("id","a1"), new XText ("hula")), new XElement ("X", new XText ("hula"), new XAttribute ("id","a1"))},
new XComment (""),
new XComment ("comment"),
};
foreach (object[] objs in content.NonRecursiveVariations(4))
{
XDocument doc1 = null;
XDocument doc2 = null;
try
{
object[] o1 = ExpandAndProtectTextNodes(objs, 0).ToArray();
object[] o2 = ExpandAndProtectTextNodes(objs, 1).ToArray();
if (o1.Select(x => new ExpectedValue(false, x)).IsXDocValid() || o2.Select(x => new ExpectedValue(false, x)).IsXDocValid()) continue;
doc1 = new XDocument(o1);
doc2 = new XDocument(o2);
VerifyComparison(true, doc1, doc2);
}
catch (InvalidOperationException)
{
// some combination produced from the array are invalid
continue;
}
finally
{
if (doc1 != null) doc1.RemoveNodes();
if (doc2 != null) doc2.RemoveNodes();
}
}
}