本文整理汇总了C#中System.Xml.XmlNode.PrependChild方法的典型用法代码示例。如果您正苦于以下问题:C# XmlNode.PrependChild方法的具体用法?C# XmlNode.PrependChild怎么用?C# XmlNode.PrependChild使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.XmlNode
的用法示例。
在下文中一共展示了XmlNode.PrependChild方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddElementToXml
static void AddElementToXml(XmlNode node, XmlElement newElement, string targetNodeForParent)
{
if (node.Name == targetNodeForParent)
node.PrependChild(newElement);
else
{
for( int i = 0; i < node.ChildNodes.Count; i++)
AddElementToXml(node.ChildNodes[i], newElement, targetNodeForParent);
}
}
示例2: SortNodes
/// <summary>
/// Sorts the children of the parentNode that match the xpath selector
/// </summary>
/// <param name="parentNode"></param>
/// <param name="childXPathSelector">An xpath expression used to select child nodes of the XmlElement</param>
/// <param name="childSelector">An expression that returns true if the XElement passed in is a valid child node to be sorted</param>
/// <param name="orderByValue">The value to order the results by</param>
internal static void SortNodes(
XmlNode parentNode,
string childXPathSelector,
Func<XElement, bool> childSelector,
Func<XElement, object> orderByValue)
{
var xElement = parentNode.ToXElement();
var children = xElement.Elements().Where(x => childSelector(x)).ToArray(); //(DONT conver to method group, the build server doesn't like it)
var data = children
.OrderByDescending(orderByValue) //order by the sort order desc
.Select(x => children.IndexOf(x)) //store the current item's index (DONT conver to method group, the build server doesn't like it)
.ToList();
//get the minimum index that a content node exists in the parent
var minElementIndex = xElement.Elements()
.TakeWhile(x => childSelector(x) == false)
.Count();
//if the minimum index is zero, then it is the very first node inside the parent,
// if it is not, we need to store the child property node that exists just before the
// first content node found so we can insert elements after it when we're sorting.
var insertAfter = minElementIndex == 0 ? null : parentNode.ChildNodes[minElementIndex - 1];
var selectedChildren = parentNode.SelectNodes(childXPathSelector);
if (selectedChildren == null)
{
throw new InvalidOperationException(string.Format("The childXPathSelector value did not return any results {0}", childXPathSelector));
}
var childElements = selectedChildren.Cast<XmlElement>().ToArray();
//iterate over the ndoes starting with the node with the highest sort order.
//then we insert this node at the begginning of the parent so that by the end
//of the iteration the node with the least sort order will be at the top.
foreach (var node in data.Select(index => childElements[index]))
{
if (insertAfter == null)
{
parentNode.PrependChild(node);
}
else
{
parentNode.InsertAfter(node, insertAfter);
}
}
}
示例3: findOrPrependElement
private static void findOrPrependElement(string name, string androidName, string ns, string value, XmlNode parent, XmlDocument doc)
{
XmlElement e = FindElementForNameWithNamespace(name, androidName, ns, value, parent);
if (e == null)
{
e = doc.CreateElement(name);
e.SetAttribute(androidName, ns, value);
parent.PrependChild(e);
}
}
示例4: SetXmlNodeChild
// ------------------------------------------------------------------------------------
// Name: SetXmlNodeChild
// Goal: Set the attribute [strChildAttr] of the child named [strChildName] of
// xml node [ndxThis] with value [strChildValue]
// If this child does not exist, then create it as the FIRST child
// History:
// 12-05-2010 ERK Created
// ------------------------------------------------------------------------------------
public XmlNode SetXmlNodeChild(XmlDocument pdxThisFile, ref XmlNode ndxThis, string strChildName, string strAttrSel, string strChildAttr, string strChildValue)
{
XmlNode ndxChild = null; // Child being looked for
XmlAttribute atxChild = null; // The attribute we are looking for
string[] arAttr = null; // Array of attribute name + value
string strSelect = null; // The string to find the appropriate child
int intI = 0; // Counter
try {
// Verify the input
if ((ndxThis == null) || (string.IsNullOrEmpty(strChildName)) || (string.IsNullOrEmpty(strChildAttr))) {
return null;
}
// Start making the selection string
strSelect = "./" + strChildName;
if (!string.IsNullOrEmpty(strAttrSel)) {
// Extract the list of necessary attributes
arAttr = strAttrSel.Split(';');
// Append left bracket
strSelect += "[";
// Visit all attributes
for (intI = 0; intI <= arAttr.GetUpperBound(0); intI += 2) {
// Should we add the logical "AND"?
if (intI > 0) {
strSelect += " and ";
}
// Add this attribute to the list
strSelect += "@" + arAttr[intI] + "='" + arAttr[intI + 1] + "'";
}
// Append right bracket
strSelect += "]";
}
// Try to get the appropriate child
ndxChild = ndxThis.SelectSingleNode(strSelect);
// Is there a child?
if (ndxChild == null) {
// Create a new child node
ndxChild = pdxThisFile.CreateNode(XmlNodeType.Element, strChildName, null);
// Should first other attributes be created?
if (!string.IsNullOrEmpty(strAttrSel)) {
// Create all necessary attributes
for (intI = 0; intI <= arAttr.GetUpperBound(0); intI += 2) {
// Add this attribute
atxChild = pdxThisFile.CreateAttribute(arAttr[intI]);
atxChild.Value = arAttr[intI + 1];
ndxChild.Attributes.Append(atxChild);
}
}
// Create a new attribute
atxChild = pdxThisFile.CreateAttribute(strChildAttr);
// Add the attribute to the node
ndxChild.Attributes.Append(atxChild);
// Make this new node the FIRST child of [ndxThis]
ndxThis.PrependChild(ndxChild);
}
// Find the attribute of the child
atxChild = ndxChild.Attributes[strChildAttr];
if ((atxChild) == null) {
// The attribute does not exist, so create it
atxChild = pdxThisFile.CreateAttribute(strChildAttr);
// Add the attribute to the node
ndxChild.Attributes.Append(atxChild);
}
// Set the attribute of the child
atxChild.Value = strChildValue;
// Return the child
return ndxChild;
} catch (Exception ex) {
// Show error
errHandle.DoError("modEditor/SetXmlNodeChild", ex);
// Return failure
return null;
}
}
示例5: eTreeInsertLevel
// ------------------------------------------------------------------------------------
// Name: eTreeInsertLevel
// Goal: Insert a node:
// eTree, eLeaf - between me and my parent
// forest - between forest and remainder
// History:
// 03-01-2013 ERK Created
// ------------------------------------------------------------------------------------
public bool eTreeInsertLevel(ref XmlNode ndxThis, ref XmlNode ndxNew)
{
XmlNode ndxChild = null; // Working node
XmlNodeList ndxList = null; // List of children
int intI = 0; // Counter
try {
// Validate something is selected
if (ndxThis != null) {
switch (ndxThis.Name) {
case "eLeaf":
case "eTree":
// (1) Create a new <eTree> element
ndxNew = CreateNewEtree(ref pdxCurrentFile);
// (2) Replace the parent's child with the new one
ndxThis.ParentNode.ReplaceChild(ndxNew, ndxThis);
// (3) Set its (only) child
ndxNew.PrependChild(ndxThis);
// (5) Get the appropriate values for @from and @to
ndxNew.Attributes["from"].Value = ndxThis.Attributes["from"].Value;
ndxNew.Attributes["to"].Value = ndxThis.Attributes["to"].Value;
// Return success
return true;
case "forest":
// Insert a level between the <forest> node and the remainder
// (1) Create a new <eTree> element
ndxNew = CreateNewEtree(ref pdxCurrentFile);
// (2) Prepare all the children of the forest parent
ndxList = ndxThis.SelectNodes("./eTree");
// (3) Make sure the new node is the child of the forest
ndxThis.PrependChild(ndxNew);
for (intI = 0; intI < ndxList.Count; intI++) {
ndxChild = ndxList[intI];
// Check this is not the new one
if (ndxChild != ndxNew) {
// Replace the parent of this item
ndxNew.AppendChild(ndxChild);
}
}
// Return success
return true;
}
}
// Return failure
return false;
} catch (Exception ex) {
// Show error
errHandle.DoError("modEditor/eTreeInsertLevel", ex);
// Return failure
return false;
}
}
示例6: eTreeAdd
// ------------------------------------------------------------------------------------
// Name: eTreeAdd
// Goal: Add a node:
// eTree - replace me as child by all my children
// forest, eLeaf - not possible
// strType can be:
// right - Add a sibling to my right
// left - Add a sibling to my left
// child - Create/add a child <eTree> node
// eLeaf - Create an <eLeaf> node under me (if there is none)
// History:
// 03-01-2013 ERK Created
// ------------------------------------------------------------------------------------
public bool eTreeAdd(ref XmlNode ndxThis, ref XmlNode ndxNew, string strType)
{
XmlNode ndxWork = null; // Working node
try {
// Validate something is selected
if (ndxThis != null) {
switch (ndxThis.Name) {
case "eLeaf":
case "forest":
// Impossible
return false;
case "eTree":
// Check what is our task
switch (strType) {
case "right":
case "Right": // Add a sibling <eTree> to my right
// (1) Create a new <eTree> element
ndxNew = CreateNewEtree(ref pdxCurrentFile);
// (2) Get my parent
ndxWork = ndxThis.ParentNode;
if (ndxWork != null) {
// Insert the new node as child after me
ndxWork.InsertAfter(ndxNew, ndxThis);
//' Adapt the <eTree>/@Id values from [ndxThis] onwards
//AdaptEtreeId(ndxThis.Attributes("Id").Value)
}
// Adapt the sentence, but don't re-calculate "org"
eTreeSentence(ref ndxNew, ref ndxNew, bDoOrg: false);
// Return success
return true;
case "left":
case "Left": // Add a sibling <eTree> to my left
// (1) Create a new <eTree> element
ndxNew = CreateNewEtree(ref pdxCurrentFile);
// (2) Get my parent
ndxWork = ndxThis.ParentNode;
if (ndxWork != null) {
// Insert the new node as child before me
ndxWork.InsertBefore(ndxNew, ndxThis);
//' Go to the first <eTree> child of [ndxWork]
//ndxWork = ndxWork.SelectSingleNode("./child::eTree[1]")
//If (ndxWork IsNot Nothing) Then
// ' Adapt the <eTree>/@Id values from [ndxWork] onwards
// AdaptEtreeId(ndxWork.Attributes("Id").Value)
//End If
}
// Adapt the sentence, but don't re-calculate "org"
eTreeSentence(ref ndxNew, ref ndxNew, bDoOrg: false);
// Return success
return true;
case "child":
case "Child":
case "childlast": // Add a child <eTree> under me
// (1) Create a new <eTree> element
ndxNew = CreateNewEtree(ref pdxCurrentFile);
// (2) Add the <eTree> child under me
ndxThis.AppendChild(ndxNew);
//' Adapt the <eTree>/@Id values from [ndxThis] onwards
//AdaptEtreeId(ndxThis.Attributes("Id").Value)
// Adapt the sentence, but don't re-calculate "org"
eTreeSentence(ref ndxNew, ref ndxNew, bDoOrg: false);
// Return success
return true;
case "firstchild":
case "childfirst": // Add a child <eTree> under me as the first one
// (1) Create a new <eTree> element
ndxNew = CreateNewEtree(ref pdxCurrentFile);
// (2) Add the <eTree> child under me
ndxThis.PrependChild(ndxNew);
//' Adapt the <eTree>/@Id values from [ndxThis] onwards
//AdaptEtreeId(ndxThis.Attributes("Id").Value)
// Adapt the sentence, but don't re-calculate "org"
eTreeSentence(ref ndxNew, ref ndxNew, bDoOrg: false);
// Return success
return true;
case "eLeaf":
case "eleaf":
case "leaf":
case "endnode":
// (1) Check: do I have any <eLeaf> or <eTree> children?
if ((ndxThis.SelectSingleNode("./child::eLeaf") == null) && (ndxThis.SelectSingleNode("./child::eTree") == null)) {
// (1) Create a new <eLeaf> element
ndxNew = CreateNewEleaf(ref pdxCurrentFile);
// (2) Add it under me
ndxThis.AppendChild(ndxNew);
// Re-do the sentence, including "org"
//.........这里部分代码省略.........
示例7: GenerateHeadTags
private static void GenerateHeadTags(ITemplateParsingState templateParsingState, XmlNode headNode)
{
SortedDictionary<double, LinkedList<XmlNode>> headerNodes = templateParsingState.HeaderNodes;
foreach (double loc in headerNodes.Keys)
if (loc < 0)
foreach (XmlNode xmlNode in headerNodes[loc])
headNode.PrependChild(xmlNode);
else
foreach (XmlNode xmlNode in headerNodes[loc])
headNode.InsertAfter(xmlNode, headNode.LastChild);
// handle oc:title, if present
// TODO: Use XPATH
XmlNodeList ocTitleNodes = headNode.OwnerDocument.GetElementsByTagName("title", templateParsingState.TemplateHandlerLocator.TemplatingConstants.TemplateNamespace);
if (ocTitleNodes.Count > 1)
for (int ctr = 1; ctr < ocTitleNodes.Count; ctr++)
{
XmlNode ocTitleNode = ocTitleNodes[ctr];
ocTitleNode.ParentNode.RemoveChild(ocTitleNode);
}
if (ocTitleNodes.Count > 0)
{
XmlNode ocTitleNode = ocTitleNodes[0];
bool titleNodePresent = false;
foreach (XmlNode titleNode in headNode.OwnerDocument.GetElementsByTagName("title", headNode.NamespaceURI))
if (titleNode.ParentNode == ocTitleNode.ParentNode)
titleNodePresent = true;
if (!titleNodePresent)
{
XmlNode titleNode = headNode.OwnerDocument.CreateElement("title", headNode.NamespaceURI);
foreach (XmlNode subNode in ocTitleNode.ChildNodes)
titleNode.AppendChild(subNode);
foreach (XmlAttribute attribute in ocTitleNode.Attributes)
titleNode.Attributes.Append(attribute);
ocTitleNode.ParentNode.InsertAfter(titleNode, ocTitleNode);
}
ocTitleNode.ParentNode.RemoveChild(ocTitleNode);
}
}
示例8: SetField
private static void SetField(
XmlNode parentNode, ref XmlNode previousSibling,
string tagName, string tagValue, string nspace)
{
// obtain a pointer to the XmlDocument object. This is used
// in a few places in this method.
XmlDocument doc = parentNode.OwnerDocument;
// create an XmlText object to hold the field's value.
XmlText text = doc.CreateTextNode(tagValue);
// look for the field.
XmlNode node = GetNode(doc, tagName, nspace);
// if the field does not exist,...
if (node == null)
{
// create an element for it and inside this element,
// insert the XmlText object we created earlier.
node = doc.CreateElement(tagName, nspace);
node.AppendChild(text);
// if there is a previous sibling, insert the new node
// after it.
if (previousSibling != null)
{
parentNode.InsertAfter(node, previousSibling);
}
// else, the new node becomes the first child.
else
{
parentNode.PrependChild(node);
}
}
// else, if the field already exists, replace its value.
else
{
// if the field does have a value, replace it with
// the XmlText object we created earlier.
if (node.HasChildNodes)
{
node.ReplaceChild(text, node.ChildNodes[0]);
}
// else, if it's empty, append the XmlText object
// we created earlier.
else
{
node.AppendChild(text);
}
}
// the next node to be added will be after this node. So, we
// set previousSibling to this node.
previousSibling = node;
}
示例9: PrependChild
public static XmlNode PrependChild(XmlNode parent, XmlNode newChild)
{
Assert.NotNull(parent);
Assert.NotNull(newChild);
return parent.PrependChild(newChild);
}
示例10: InsertNode
/// <summary>
/// Creates, updates and inserts a node.
/// </summary>
private void InsertNode(XmlNode anchor, IUpdateOperation updateOperation)
{
// create node according to type
XmlNode newXmlNode;
switch (GetNodeType())
{
case XmlNodeType.Attribute:
newXmlNode = XmlDocument.CreateAttribute(Name, Namespace ?? string.Empty);
break;
case XmlNodeType.Element:
newXmlNode = XmlDocument.CreateElement(Name, Namespace ?? string.Empty);
break;
case XmlNodeType.CData:
newXmlNode = XmlDocument.CreateCDataSection(string.Empty);
break;
case XmlNodeType.Comment:
newXmlNode = XmlDocument.CreateComment(string.Empty);
break;
case XmlNodeType.SignificantWhitespace:
newXmlNode = XmlDocument.CreateSignificantWhitespace(string.Empty);
break;
case XmlNodeType.Text:
newXmlNode = XmlDocument.CreateTextNode(string.Empty);
break;
case XmlNodeType.Whitespace:
newXmlNode = XmlDocument.CreateWhitespace(string.Empty);
break;
default:
// should not happen
throw new ArgumentOutOfRangeException();
}
// update node with input
updateOperation.Update(newXmlNode);
// insert node relative to anchor
if (newXmlNode is XmlAttribute)
{
var newXmlAttribute = (XmlAttribute) newXmlNode;
switch (GetNodePosition())
{
case XmlNodePosition.Append:
case XmlNodePosition.After:
if (anchor is XmlAttribute)
{
var xmlAttribute = (XmlAttribute) anchor;
if (xmlAttribute.OwnerElement != null)
xmlAttribute.OwnerElement.Attributes.InsertAfter(newXmlAttribute, xmlAttribute);
}
else if (anchor.Attributes != null)
{
anchor.Attributes.Append(newXmlAttribute);
}
break;
case XmlNodePosition.Prepend:
case XmlNodePosition.Before:
if (anchor is XmlAttribute)
{
var xmlAttribute = (XmlAttribute) anchor;
if (xmlAttribute.OwnerElement != null)
xmlAttribute.OwnerElement.Attributes.InsertBefore(newXmlAttribute, xmlAttribute);
}
else if (anchor.Attributes != null)
{
anchor.Attributes.Prepend(newXmlAttribute);
}
break;
default:
// should not happen
throw new ArgumentOutOfRangeException();
}
}
else
{
switch (GetNodePosition())
{
case XmlNodePosition.Append:
anchor.AppendChild(newXmlNode);
break;
case XmlNodePosition.Prepend:
anchor.PrependChild(newXmlNode);
break;
case XmlNodePosition.After:
if (anchor.ParentNode != null)
anchor.ParentNode.InsertAfter(newXmlNode, anchor);
break;
case XmlNodePosition.Before:
if (anchor.ParentNode != null)
anchor.ParentNode.InsertBefore(newXmlNode, anchor);
break;
default:
// should not happen
throw new ArgumentOutOfRangeException();
}
}
}
示例11: PrependElementIfMissing
public static XmlElement PrependElementIfMissing(string tagName, string name, Dictionary<string, string> otherAttributes, XmlNode parent) {
XmlElement e = null;
if (!string.IsNullOrEmpty(name)) {
e = FindElementWithTagAndName(tagName, name, parent);
}
if (e == null)
{
e = _document.CreateElement(tagName);
if (!string.IsNullOrEmpty(name)) {
e.SetAttribute("name", _namespace, name);
}
parent.PrependChild(e);
}
if (otherAttributes != null) {
foreach(string key in otherAttributes.Keys) {
e.SetAttribute(key, _namespace, otherAttributes[key]);
}
}
return e;
}
示例12: AddXmlChildElement
public static void AddXmlChildElement(XmlNode parentNode, string strChildNodeName, string strChildOuterXml, bool bForce, bool bAppend)
{
try
{
if ((((strChildNodeName != "") && (strChildOuterXml != "")) && (parentNode != null)) && (bForce || (parentNode.SelectSingleNode(strChildNodeName) == null)))
{
XmlNode newChild = parentNode.SelectSingleNode(strChildNodeName);
if (newChild == null)
{
newChild = parentNode.OwnerDocument.CreateElement(strChildNodeName);
parentNode.AppendChild(newChild);
}
newChild.InnerXml = strChildOuterXml;
XmlNode node2 = newChild.SelectSingleNode(strChildNodeName);
parentNode.RemoveChild(newChild);
if (node2 != null)
{
if (bAppend)
{
parentNode.AppendChild(node2);
}
else
{
parentNode.PrependChild(node2);
}
}
}
}
catch
{
}
}
示例13: SetNodeValue
public static bool SetNodeValue(XmlDocument doc, XmlNode node, string name, string text, bool createNew, bool append)
{
bool flag = true;
try
{
if ((doc == null) || (node == null))
{
return flag;
}
XmlNode newChild = node.SelectSingleNode(name);
if (((newChild == null) && createNew) && (text != null))
{
newChild = doc.CreateElement(name);
if (append)
{
node.AppendChild(newChild);
}
else
{
node.PrependChild(newChild);
}
}
if (newChild == null)
{
return flag;
}
if (text != null)
{
newChild.InnerText = text;
return flag;
}
newChild.ParentNode.RemoveChild(newChild);
}
catch
{
flag = false;
}
return flag;
}
示例14: ExpandCodeSnippet
private void ExpandCodeSnippet(XmlDocument doc, XmlNode node)
{
XmlNode innerNode = doc.CreateNode("element", "pre", "");
innerNode.InnerText = node.InnerText;
node.InnerText = "";
node.PrependChild(innerNode);
}