本文整理汇总了C#中System.Xml.Linq.XElement.ReplaceNodes方法的典型用法代码示例。如果您正苦于以下问题:C# XElement.ReplaceNodes方法的具体用法?C# XElement.ReplaceNodes怎么用?C# XElement.ReplaceNodes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Linq.XElement
的用法示例。
在下文中一共展示了XElement.ReplaceNodes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PerformMigration
/// ------------------------------------------------------------------------------------
/// <summary>
/// Perform one increment migration step.
///
/// In this case, the migration is not related to a model change,
/// but is a simple data change that removes the class elements in the xml.
/// The end resujlt xml will have the top-level 'rt' element and zero, or more,
/// property level elements.
/// </summary>
/// ------------------------------------------------------------------------------------
public void PerformMigration(IDomainObjectDTORepository domainObjectDtoRepository)
{
DataMigrationServices.CheckVersionNumber(domainObjectDtoRepository, 7000014);
// No. We need to convert all instances, even if they are no longer part of the model.
// DM19, for example, removes LgWritingSystem instances and the class form the model,
// but it tries to access the data as if it had been properly processed by DM15,
// *but* this code would leave out LgWritingSystem instnaces form being processed here.
//foreach (var dto in domainObjectDtoRepository.AllInstancesWithSubclasses("CmObject"))
foreach (var dto in domainObjectDtoRepository.AllInstances())
{
var rtElement = XElement.Parse(dto.Xml);
// Removes all current child nodes (class level),
// and replaces them with the old property nodes (if any).
#if !__MonoCS__
rtElement.ReplaceNodes(rtElement.Elements().Elements());
dto.Xml = rtElement.ToString();
#else // FWNX-165: work around mono bug https://bugzilla.novell.com/show_bug.cgi?id=592435
var copy = new XElement(rtElement);
copy.ReplaceNodes(rtElement.Elements().Elements());
dto.Xml = copy.ToString();
#endif
domainObjectDtoRepository.Update(dto);
}
DataMigrationServices.IncrementVersionNumber(domainObjectDtoRepository);
}
示例2: ReorderChildNodes
/// <summary>
/// Move VSM to last child of element.
/// </summary>
/// <param name="element">Element that is getting its VSM moved to the end.</param>
private void ReorderChildNodes(XElement element)
{
List<NodeCollection> nodeCollections = new List<NodeCollection>();
NodeCollection vsmNodeCollection = new NodeCollection();
var children = element.Nodes();
bool hasCollectionBeenAdded = false;
NodeCollection currentNodeCollection = null;
foreach (var child in children)
{
if (currentNodeCollection == null)
{
currentNodeCollection = new NodeCollection();
}
currentNodeCollection.Nodes.Add(child);
if (child.NodeType == XmlNodeType.Element)
{
if (this.VSMNode.IsMatch(((XElement)child).Name))
{
// Extract VSM for adding to end.
vsmNodeCollection = currentNodeCollection;
hasCollectionBeenAdded = true;
}
else if (!hasCollectionBeenAdded)
{
// Maintain all other nodes.
nodeCollections.Add(currentNodeCollection);
hasCollectionBeenAdded = true;
}
currentNodeCollection = null;
hasCollectionBeenAdded = false;
}
}
var newNodes = (this.Mode == VisualStateManagerRule.Last)
? nodeCollections.SelectMany(_ => _.Nodes).Concat(vsmNodeCollection.Nodes)
: vsmNodeCollection.Nodes.Concat(nodeCollections.SelectMany(_ => _.Nodes));
element.ReplaceNodes(newNodes);
}
示例3: GetOrderedElement
private XElement GetOrderedElement(XElement element)
{
var children = element.Nodes().ToList();
var childElements = children.OfType<XElement>().
OrderBy(e => (string)e.Attribute("env")).
OrderBy(e => (string)e.Attribute("key")).
OrderBy(e => e.Name.LocalName).
ToList();
var newChildren = childElements.Cast<XNode>().ToList();
var comments = children.OfType<XComment>().ToList();
newChildren = ProcessComments(comments, newChildren);
foreach (var subsidiaryElement in newChildren.OfType<XElement>().Where(e => e.HasElements))
{
subsidiaryElement.ReplaceWith(GetOrderedElement(subsidiaryElement));
}
element.ReplaceNodes(newChildren);
return element;
}
示例4: AddData
public void AddData(Bot bot, double refs, int trades)
{
StoreName(bot);
if (File.Exists(fileName))
{
XDocument doc = XDocument.Load(fileName);
XElement service = new XElement(bot.DisplayName);
service.ReplaceNodes("trades", trades.ToString());
doc.Save(fileName);
}
else if (File.Exists(fileName))
{
XDocument doc = XDocument.Load(fileName);
XElement service = new XElement(bot.DisplayName);
service.ReplaceNodes("refs", refs.ToString());
doc.Save(fileName);
}
else
{
CreateFile();
if (File.Exists(fileName))
{
XDocument doc = XDocument.Load(fileName);
XElement service = new XElement(bot.DisplayName);
service.Add(new XElement("trades", trades.ToString()));
doc.Save(fileName);
}
if (File.Exists(fileName))
{
XDocument doc = XDocument.Load(fileName);
XElement service = new XElement(bot.DisplayName);
service.Add(new XElement("refs", refs.ToString()));
doc.Save(fileName);
}
}
}
示例5: ReorderChildNodes
/// <summary>
/// Move VSM to last child of element.
/// </summary>
/// <param name="element">Element that is getting its VSM moved to the end.</param>
private void ReorderChildNodes(XElement element)
{
List<NodeCollection> nodeCollections = new List<NodeCollection>();
List<NodeCollection> propertyElementCollection = new List<NodeCollection>();
NodeCollection vsmNodeCollection = new NodeCollection();
var parentName = element.Name;
var children = element.Nodes().ToList();
bool hasCollectionBeenAdded = false;
NodeCollection currentNodeCollection = null;
//remove last new line node to prevent new lines on every format
if (this.Mode == VisualStateManagerRule.Last)
{
children.Remove(children.Last());
}
foreach (var child in children)
{
if (currentNodeCollection == null)
{
currentNodeCollection = new NodeCollection();
}
currentNodeCollection.Nodes.Add(child);
if (child.NodeType == XmlNodeType.Element)
{
var childName = ((XElement)child).Name;
if (this.VSMNode.IsMatch(childName))
{
// Extract VSM for adding to end.
vsmNodeCollection = currentNodeCollection;
hasCollectionBeenAdded = true;
}
else if(childName.LocalName.StartsWith($"{parentName.LocalName}."))
{
// Extract property-element syntax nodes.
propertyElementCollection.Add(currentNodeCollection);
hasCollectionBeenAdded = true;
}
else if (!hasCollectionBeenAdded)
{
// Maintain all other nodes.
nodeCollections.Add(currentNodeCollection);
hasCollectionBeenAdded = true;
}
currentNodeCollection = null;
hasCollectionBeenAdded = false;
}
}
// Add any trailing non-Element nodes (e.g., comments).
if (currentNodeCollection != null)
{
nodeCollections.Add(currentNodeCollection);
}
var newNodes = ((this.Mode == VisualStateManagerRule.Last)
? propertyElementCollection.SelectMany(_ => _.Nodes)
.Concat(nodeCollections.SelectMany(_ => _.Nodes))
.Concat(vsmNodeCollection.Nodes)
: propertyElementCollection.SelectMany(_ => _.Nodes)
.Concat(vsmNodeCollection.Nodes)
.Concat(nodeCollections.SelectMany(_ => _.Nodes))).ToList();
var firstNode = newNodes.First() as XText;
if ((this.Mode == VisualStateManagerRule.Last)
&& firstNode != null
&& string.IsNullOrWhiteSpace(firstNode.Value.Trim()))
{
newNodes.Remove(firstNode);
}
element.ReplaceNodes(newNodes);
}
示例6: XElementReplaceWithIEnumerable
public void XElementReplaceWithIEnumerable()
{
XElement xElem = new XElement("root");
IEnumerable<XNode> newValue = InputSpace.GetElement(1000, 2).DescendantNodes();
XElement xElemOriginal = new XElement(xElem);
using (UndoManager undo = new UndoManager(xElem))
{
undo.Group();
using (EventsHelper eHelper = new EventsHelper(xElem))
{
xElem.ReplaceNodes(newValue);
eHelper.Verify(XObjectChange.Add, newValue.ToArray());
}
undo.Undo();
Assert.True(XNode.DeepEquals(xElem, xElemOriginal), "Undo did not work!");
}
}
示例7: ExecuteXElementVariation
public void ExecuteXElementVariation(XNode toReplace)
{
XNode newValue = new XText("text");
XElement xElem = new XElement("root", InputSpace.GetAttributeElement(10, 1000).Elements().Attributes(), toReplace);
XElement xElemOriginal = new XElement(xElem);
using (UndoManager undo = new UndoManager(xElem))
{
undo.Group();
using (EventsHelper eHelper = new EventsHelper(xElem))
{
xElem.ReplaceNodes(newValue);
Assert.True(xElem.Nodes().Count() == 1, "Did not replace correctly");
Assert.True(Object.ReferenceEquals(xElem.FirstNode, newValue), "Did not replace correctly");
Assert.True(xElem.HasAttributes, "ReplaceNodes removed attributes");
xElem.Verify();
eHelper.Verify(new XObjectChange[] { XObjectChange.Remove, XObjectChange.Add }, new XObject[] { toReplace, newValue });
}
undo.Undo();
Assert.True(xElem.Nodes().SequenceEqual(xElemOriginal.Nodes(), XNode.EqualityComparer), "Undo did not work!");
Assert.True(xElem.Attributes().EqualsAllAttributes(xElemOriginal.Attributes(), Helpers.MyAttributeComparer), "Undo did not work!");
}
}
示例8: ProcessGrid
private void ProcessGrid(XElement element)
{
List<GridNodeContainer> lstNodeContainers = new List<GridNodeContainer>();
int commentIndex = int.MaxValue;
var children = element.Nodes();
// Run through child elements & read the attributes
foreach (var child in children)
{
switch (child.NodeType)
{
case XmlNodeType.Element:
// it's an element. Search for Grid.Row attribute / Grid.Column attribute
var childElement = (XElement)child;
var rowAttr = childElement.Attributes("Grid.Row");
var columnAttr = childElement.Attributes("Grid.Column");
int row;
int column;
if (rowAttr == null || !rowAttr.Any() || !int.TryParse(rowAttr.First().Value, out row))
{
row = childElement.Name.LocalName.Contains(".") ? -2 : -1;
}
if (columnAttr == null || !columnAttr.Any() || !int.TryParse(columnAttr.First().Value, out column))
{
column = -1;
}
while (commentIndex < lstNodeContainers.Count)
{
lstNodeContainers[commentIndex].Row = row;
commentIndex++;
}
commentIndex = int.MaxValue;
// no attribute? 0,0
lstNodeContainers.Add(new GridNodeContainer(child, row, column));
break;
default:
// it's not an element - add it, passing in the previous row/column value - this ensures
// comments, whitespace, ... are kept in the correct place
var prev = lstNodeContainers.LastOrDefault();
if (prev != null)
{
lstNodeContainers.Add(new GridNodeContainer(child, prev.Row, prev.Column));
}
else
{
lstNodeContainers.Add(new GridNodeContainer(child, int.MinValue, int.MinValue));
}
if (child.NodeType == XmlNodeType.Comment && commentIndex == int.MaxValue)
{
commentIndex = lstNodeContainers.Count - 1;
}
break;
}
}
// sort that list.
lstNodeContainers = lstNodeContainers.OrderBy(nc => nc.Row).ThenBy(nc => nc.Column).ToList();
// replace the element's nodes
element.ReplaceNodes(lstNodeContainers.Select(nc => nc.Node));
}
示例9: SaveAsXml
public void SaveAsXml(string path)
{
var xdoc = new XDocument();
xdoc.Add(new XElement("root"));
//xdoc.Root = new XElement("root");
foreach (DataRow row in this.ResultTable.Rows)
{
var xItem = new XElement("item");
foreach (DataColumn column in ResultTable.Columns)
{
var xElement = new XElement(column.ColumnName);
if (column.ColumnName == "details")
xElement.ReplaceNodes(new XCData(row[column].ToString()));
else
xElement.Value = row[column].ToString();
xItem.Add(xElement);
}
xdoc.Root.Add(xItem);
}
xdoc.Save(path);
}
示例10: ReorderChildNodes
/// <summary>
/// Reorder child nodes matching ChildNodeNames
/// </summary>
/// <param name="element">Element thats getting its children reordered</param>
private void ReorderChildNodes(XElement element)
{
List<NodeCollection> nodeCollections = new List<NodeCollection>();
var children = element.Nodes();
// This indicates if last element matched ChildNodeNames
bool inMatchingChildBlock = false;
// This value changes each time a non matching ChildNodeName is reached ensuring that only sortable elements are reordered
int childBlockIndex = 0;
NodeCollection currentNodeCollection = null;
// Run through children
foreach (var child in children)
{
if (currentNodeCollection == null)
{
currentNodeCollection = new NodeCollection();
nodeCollections.Add(currentNodeCollection);
}
if (child.NodeType == XmlNodeType.Element)
{
XElement childElement = (XElement)child;
var isMatchingChild = ChildNodeNames.Any(match => match.IsMatch(childElement.Name));
if (isMatchingChild == false || inMatchingChildBlock == false)
{
childBlockIndex++;
inMatchingChildBlock = isMatchingChild;
}
if (isMatchingChild)
{
currentNodeCollection.SortAttributeValues = SortByAttributes.Select(x => x.GetValue(childElement)).ToArray();
}
currentNodeCollection.BlockIndex = childBlockIndex;
}
currentNodeCollection.Nodes.Add(child);
if (child.NodeType == XmlNodeType.Element)
currentNodeCollection = null;
}
if (currentNodeCollection != null)
currentNodeCollection.BlockIndex = childBlockIndex + 1;
// sort node list
nodeCollections = nodeCollections.OrderBy(x => x).ToList();
// replace the element's nodes
element.ReplaceNodes(nodeCollections.SelectMany(nc => nc.Nodes));
}
示例11: ReorderChildNodes
/// <summary>
/// Reorder child nodes matching ChildNodeNames
/// </summary>
/// <param name="element">Element thats getting its children reordered</param>
private void ReorderChildNodes(XElement element)
{
List<NodeCollection> nodeCollections = new List<NodeCollection>();
var children = element.Nodes();
// This indicates if last element matched ChildNodeNames.
bool inMatchingChildBlock = false;
// This value changes each time a non matching ChildNodeName is reached ensuring
// that only sortable elements are reordered.
int childBlockIndex = 0;
NodeCollection currentNodeCollection = null;
// Run through children.
foreach (var child in children)
{
if (currentNodeCollection == null)
{
currentNodeCollection = new NodeCollection();
nodeCollections.Add(currentNodeCollection);
}
if (child.NodeType == XmlNodeType.Element)
{
XElement childElement = (XElement)child;
var isMatchingChild = this.ChildNodeNames.Any(_ => _.IsMatch(childElement.Name))
&& !this.ignoredNodeNames.Any(_ => _.IsMatch(childElement.Name));
if (!isMatchingChild || !inMatchingChildBlock)
{
childBlockIndex++;
inMatchingChildBlock = isMatchingChild;
}
if (isMatchingChild)
{
currentNodeCollection.SortAttributeValues =
this.SortByAttributes.Select(_ => _.GetValue(childElement)).ToArray();
}
currentNodeCollection.BlockIndex = childBlockIndex;
}
currentNodeCollection.Nodes.Add(child);
if (child.NodeType == XmlNodeType.Element)
{
currentNodeCollection = null;
}
}
if (currentNodeCollection != null)
{
currentNodeCollection.BlockIndex = (childBlockIndex + 1);
}
// Sort node list.
nodeCollections = nodeCollections.OrderBy(_ => _).ToList();
// Replace the element's nodes.
element.ReplaceNodes(nodeCollections.SelectMany(_ => _.Nodes));
}
示例12: SortElement
private void SortElement(XElement xe, bool sortElements, bool sortAttributes)
{
if (!xe.Elements().Any())
{
return;
}
if (sortElements)
{
xe.ReplaceNodes(xe.Elements().OrderBy(x => x.Name.LocalName));
}
if (sortAttributes)
{
xe.ReplaceAttributes(xe.Attributes().OrderBy(x => x.Name.LocalName));
}
foreach (XElement xc in xe.Elements())
{
SortElement(xc, sortElements, sortAttributes);
}
}
示例13: ProcessSetters
/// <summary>
/// Order child setters by property/targetname
///
/// Notes on index vars used:
///
///
/// </summary>
/// <param name="element"></param>
private void ProcessSetters(XElement element)
{
// A string that hopefully always are sorted at the en
List<SetterNodeCollection> nodeCollections = new List<SetterNodeCollection>();
var children = element.Nodes();
// This is increased each time Define sortable parameters
int settersBlockIndex = 1;
bool inSettersBlock = false;
SetterNodeCollection currentNodeCollection = null;
// Run through children
foreach (var child in children)
{
if (currentNodeCollection == null)
{
currentNodeCollection = new SetterNodeCollection();
nodeCollections.Add(currentNodeCollection);
}
if (child.NodeType == XmlNodeType.Element)
{
XElement childElement = (XElement)child;
var isSetter = childElement.Name.LocalName == "Setter" && childElement.Name.NamespaceName == "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
if (isSetter != inSettersBlock)
{
settersBlockIndex++;
inSettersBlock = isSetter;
}
if (isSetter)
{
currentNodeCollection.Property = (string)childElement.Attribute("Property");
currentNodeCollection.TargetName = (string)childElement.Attribute("TargetName");
}
currentNodeCollection.BlockIndex = settersBlockIndex;
}
currentNodeCollection.Nodes.Add(child);
if (child.NodeType == XmlNodeType.Element)
currentNodeCollection = null;
}
if (currentNodeCollection != null)
currentNodeCollection.BlockIndex = settersBlockIndex + 1;
// sort that list.
switch (Options.ReorderSetters)
{
case ReorderSettersBy.None:
break;
case ReorderSettersBy.Property:
nodeCollections = nodeCollections.OrderBy(x => x.BlockIndex).ThenBy(x=>x.Property).ToList();
break;
case ReorderSettersBy.TargetName:
nodeCollections = nodeCollections.OrderBy(x => x.BlockIndex).ThenBy(x => x.TargetName).ToList();
break;
case ReorderSettersBy.TargetNameThenProperty:
nodeCollections = nodeCollections.OrderBy(x => x.BlockIndex).ThenBy(x => x.TargetName).ThenBy(x => x.Property).ToList();
break;
default:
throw new ArgumentOutOfRangeException();
}
// replace the element's nodes
element.ReplaceNodes(nodeCollections.SelectMany(nc => nc.Nodes));
}
示例14: ContainerReplaceNodes
/// <summary>
/// Validate ReplaceNodes on container.
/// </summary>
/// <param name="contextValue"></param>
/// <returns></returns>
//[Variation(Desc = "ContainerReplaceNodes")]
public void ContainerReplaceNodes()
{
XElement element =
new XElement("foo",
new XAttribute("att", "bar"),
"abc",
new XElement("nested", new XText("abcd")));
// Replace with a node, attribute, string, some other value, and an IEnumerable.
// ReplaceNodes does not remove attributes.
XComment comment = new XComment("this is a comment");
XComment comment2 = new XComment("this is a comment 2");
XComment comment3 = new XComment("this is a comment 3");
XAttribute attribute = new XAttribute("att2", "att-value");
string str = "this is a string";
TimeSpan other1 = new TimeSpan(1, 2, 3);
element.ReplaceNodes(
comment,
attribute,
str,
other1,
new XComment[] { comment2, comment3 });
Validate.EnumeratorDeepEquals(
element.Nodes(),
new XNode[]
{
comment,
new XText(str + XmlConvert.ToString(other1)),
comment2,
comment3
});
Validate.Count(element.Attributes(), 2);
Validate.AttributeNameAndValue(element.Attribute("att"), "att", "bar");
Validate.AttributeNameAndValue(element.Attribute("att2"), "att2", "att-value");
}
示例15: CreatingXElementsFromNewDev10Types
//[Variation(Desc = "Tuple - New Dev10 Types", Param = 1)]
//[Variation(Desc = "DynamicObject - New Dev10 Types", Param = 2)]
//[Variation(Desc = "Guid - old type", Param = 3)]
//[Variation(Desc = "Dictionary - old type", Param = 4)]
public void CreatingXElementsFromNewDev10Types()
{
object t = null;
Type type = typeof(object);
int param = (int)this.Variation.Param;
switch (param)
{
case 1: t = Tuple.Create(1, "Melitta", 7.5); type = typeof(Tuple); break;
case 3: t = new Guid(); type = typeof(Guid); break;
case 4: t = new Dictionary<int, string>(); ((Dictionary<int, string>)t).Add(7, "a"); type = typeof(Dictionary<int, string>); break;
}
XElement e = new XElement("e1",
new XElement("e2"), "text1",
new XElement("e3"), t);
e.Add(t);
e.FirstNode.ReplaceWith(t);
XNode n = e.FirstNode.NextNode;
n.AddBeforeSelf(t);
n.AddAnnotation(t);
n.ReplaceWith(t);
e.FirstNode.AddAfterSelf(t);
e.AddFirst(t);
e.Annotation(type);
e.Annotations(type);
e.RemoveAnnotations(type);
e.ReplaceAll(t);
e.ReplaceAttributes(t);
e.ReplaceNodes(t);
e.SetAttributeValue("a", t);
e.SetElementValue("e2", t);
e.SetValue(t);
XAttribute a = new XAttribute("a", t);
XStreamingElement se = new XStreamingElement("se", t);
se.Add(t);
try
{
new XDocument(t);
}
catch (ArgumentException)
{
try
{
new XDocument(t);
}
catch (ArgumentException)
{
return;
}
}
TestLog.Compare(false, "Failed");
}