本文整理汇总了C#中System.Xml.XmlElement.ReplaceChild方法的典型用法代码示例。如果您正苦于以下问题:C# XmlElement.ReplaceChild方法的具体用法?C# XmlElement.ReplaceChild怎么用?C# XmlElement.ReplaceChild使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.XmlElement
的用法示例。
在下文中一共展示了XmlElement.ReplaceChild方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteInstance
internal static void WriteInstance(XmlDocument xmlDoc, XmlElement pluginNode)
{
if (instance == null)
{
//This can occur if a logbook could not be loaded, then ST is closed
instance = new Settings();
}
XmlElement user = xmlDoc.CreateElement("User");
XmlAttribute username = xmlDoc.CreateAttribute("username");
username.Value = instance.User.Username;
user.Attributes.Append(username);
XmlAttribute password = xmlDoc.CreateAttribute("password");
password.Value = instance.User.Password;
user.Attributes.Append(password);
XmlNode existing = pluginNode.SelectSingleNode(user.Name);
if (existing == null)
pluginNode.AppendChild(user);
else
pluginNode.ReplaceChild(user, existing);
XmlElement login = xmlDoc.CreateElement("Login");
XmlAttribute loginId = xmlDoc.CreateAttribute("loginId");
loginId.Value = instance.User.LoginId;
login.Attributes.Append(loginId);
XmlAttribute loginSecret = xmlDoc.CreateAttribute("loginSecret");
loginSecret.Value = instance.User.LoginSecret;
login.Attributes.Append(loginSecret);
existing = pluginNode.SelectSingleNode(login.Name);
if (existing == null)
pluginNode.AppendChild(login);
else
pluginNode.ReplaceChild(login, existing);
XmlElement ExportNameInComment = xmlDoc.CreateElement("ExportNameInComment");
XmlAttribute export = xmlDoc.CreateAttribute("export");
export.Value = instance.boExportNameInComment.ToString();
ExportNameInComment.Attributes.Append(export);
existing = pluginNode.SelectSingleNode(ExportNameInComment.Name);
if (existing == null)
pluginNode.AppendChild(ExportNameInComment);
else
pluginNode.ReplaceChild(ExportNameInComment, existing);
XmlElement RoutePrivacyElement = xmlDoc.CreateElement("RoutePrivacy");
RoutePrivacyElement.SetAttribute("RoutePrivacyIndex", instance.RoutePrivacyIndex.ToString());
existing = pluginNode.SelectSingleNode(RoutePrivacyElement.Name);
if (existing == null)
pluginNode.AppendChild(RoutePrivacyElement);
else
pluginNode.ReplaceChild(RoutePrivacyElement, existing);
// Save activity type mappings
XmlElement mappings = xmlDoc.CreateElement("Mappings"); //Remains called only Mappings for compatibility reasons
foreach (ActivityTypeMapping atm in instance.ActivityTypeMappings)
{
if (atm.Funbeat != 51) // 51 is default, only save mappings that differ
{
XmlElement mapping = xmlDoc.CreateElement("Mapping");
XmlAttribute funbeat = xmlDoc.CreateAttribute("funbeat");
XmlAttribute st2 = xmlDoc.CreateAttribute("st2");
funbeat.Value = atm.Funbeat.ToString();
st2.Value = atm.SportTracks;
mapping.Attributes.Append(funbeat);
mapping.Attributes.Append(st2);
mappings.AppendChild(mapping);
}
}
existing = pluginNode.SelectSingleNode(mappings.Name);
if (existing == null)
pluginNode.AppendChild(mappings);
else
pluginNode.ReplaceChild(mappings, existing);
// Save equipment mappings
XmlElement eqMappings = xmlDoc.CreateElement("EquipmentMappings");
foreach (EquipmentTypeMapping etm in instance.EquipmentTypeMappings)
{
if (etm.Funbeat != "") // Only save mappings that are used
{
XmlElement mapping = xmlDoc.CreateElement("Mapping");
XmlAttribute funbeat = xmlDoc.CreateAttribute("funbeat");
XmlAttribute st2 = xmlDoc.CreateAttribute("st2");
funbeat.Value = etm.Funbeat;
st2.Value = etm.SportTracks;
mapping.Attributes.Append(funbeat);
mapping.Attributes.Append(st2);
eqMappings.AppendChild(mapping);
}
}
existing = pluginNode.SelectSingleNode(eqMappings.Name);
if (existing == null)
pluginNode.AppendChild(eqMappings);
else
pluginNode.ReplaceChild(eqMappings, existing);
}
示例2: saveTestReplaceChildAffectOnEnumeration
// TODO: Take the word save off front of this method when XmlNode.ReplaceChild() is implemented.
public void saveTestReplaceChildAffectOnEnumeration ()
{
document.LoadXml ("<foo><child1/><child2/></foo>");
element = document.DocumentElement;
node = document.CreateElement("child3");
enumerator = element.GetEnumerator();
Assert.AreEqual (enumerator.MoveNext(), true, "MoveNext should have succeeded.");
element.ReplaceChild(node, element.LastChild);
enumerator.MoveNext();
Assert.AreEqual (((XmlElement)enumerator.Current).LocalName, "child3", "Expected child3 element.");
Assert.AreEqual (enumerator.MoveNext(), false, "MoveNext should have failed.");
}
示例3: mergeNodes
void mergeNodes(XmlElement oldroot,XmlElement newroot,string nd,string idattr)
{
string[] atr = idattr.Split('|');
foreach (XmlElement e in newroot)
if (e.Name == nd)
{
string vl = "";
for (int i=0;i<atr.Length;i++)
{
if (i != 0) vl += "|";
vl += e.Attributes[atr[i]].Value;
}
XmlElement e2 = findNode(oldroot, nd, idattr, vl);
if (e2 == null)
oldroot.AppendChild(doc.ImportNode(e,true));
else
{
bool same = true;
if (e2.Attributes.Count != e.Attributes.Count)
same = false;
else
for (int i = 0; i < e2.Attributes.Count;i++ )
{
string nm = e2.Attributes[i].Name;
if (e.Attributes[nm] == null || e.Attributes[nm].Value != e2.Attributes[nm].Value)
same = false;
}
if (!same)
{
if (new MergeChanges(e2,e).ShowDialog() == DialogResult.OK)
oldroot.ReplaceChild(doc.ImportNode(e,true), e2);
}
}
}
}
示例4: GetNormalizedChildNodes
IEnumerable<XmlNode> GetNormalizedChildNodes(XmlElement element)
{
XmlNode node = element.FirstChild;
while (node != null) {
XmlText text = node as XmlText;
XmlCDataSection cData = node as XmlCDataSection;
if (node.NodeType == XmlNodeType.SignificantWhitespace) {
text = element.OwnerDocument.CreateTextNode(node.Value);
element.ReplaceChild(text, node);
node = text;
}
if (text != null || cData != null) {
node = node.NextSibling;
while (node != null
&& (node.NodeType == XmlNodeType.Text
|| node.NodeType == XmlNodeType.CDATA
|| node.NodeType == XmlNodeType.SignificantWhitespace)) {
if (text != null) text.Value += node.Value;
else cData.Value += node.Value;
XmlNode nodeToDelete = node;
node = node.NextSibling;
element.RemoveChild(nodeToDelete);
}
if (text != null) yield return text;
else yield return cData;
} else {
yield return node;
node = node.NextSibling;
}
}
}
示例5: ProcessTypeLibrary
/// ------------------------------------------------------------------------------------
/// <summary>
/// Creates the entries for the type library and all classes defined in this type
/// library. We can get the necessary information for the file element (file name and
/// size) directly from the file. We get the information for the type library from
/// the type library itself.
/// </summary>
/// <param name="parent">The parent node.</param>
/// <param name="fileName">Name (and path) of the file.</param>
/// ------------------------------------------------------------------------------------
public void ProcessTypeLibrary(XmlElement parent, string fileName)
{
m_BaseDirectory = Path.GetDirectoryName(fileName);
m_FileName = fileName.ToLower();
try
{
XmlNode oldChild;
ITypeLib typeLib = LoadTypeLib(m_FileName);
IntPtr pLibAttr;
typeLib.GetLibAttr(out pLibAttr);
var libAttr = (TYPELIBATTR)
System.Runtime.InteropServices.Marshal.PtrToStructure(pLibAttr, typeof(TYPELIBATTR));
typeLib.ReleaseTLibAttr(pLibAttr);
string flags = string.Empty;
if ((libAttr.wLibFlags & LIBFLAGS.LIBFLAG_FHASDISKIMAGE) == LIBFLAGS.LIBFLAG_FHASDISKIMAGE)
flags = "HASDISKIMAGE";
// <file name="FwKernel.dll" asmv2:size="1507328">
XmlElement file = GetOrCreateFileNode(parent, fileName);
// <typelib tlbid="{2f0fccc0-c160-11d3-8da2-005004defec4}" version="1.0" helpdir=""
// resourceid="0" flags="HASDISKIMAGE" />
if (m_TlbGuids.ContainsKey(libAttr.guid))
{
Console.WriteLine("Warning: Type library with GUID {0} is defined in {1} and {2}",
libAttr.guid, m_TlbGuids[libAttr.guid], Path.GetFileName(fileName));
}
else
{
m_TlbGuids.Add(libAttr.guid, Path.GetFileName(fileName));
XmlElement elem = m_Doc.CreateElement("typelib", UrnAsmv1);
elem.SetAttribute("tlbid", string.Format("{{{0}}}", libAttr.guid));
elem.SetAttribute("version", string.Format("{0}.{1}", libAttr.wMajorVerNum,
libAttr.wMinorVerNum));
elem.SetAttribute("helpdir", string.Empty);
elem.SetAttribute("resourceid", "0");
elem.SetAttribute("flags", flags);
oldChild = file.SelectSingleNode(string.Format("typelib[tlbid='{{{0}}}']",
libAttr.guid));
if (oldChild != null)
file.ReplaceChild(elem, oldChild);
else
file.AppendChild(elem);
}
Debug.WriteLine(string.Format(@"typelib tlbid=""{0}"" version=""{1}.{2}"" helpdir="""" resourceid=""0"" flags=""{3}""",
libAttr.guid, libAttr.wMajorVerNum, libAttr.wMinorVerNum, flags));
int count = typeLib.GetTypeInfoCount();
for (int i = 0; i < count; i++)
{
ITypeInfo typeInfo;
typeLib.GetTypeInfo(i, out typeInfo);
ProcessTypeInfo(parent, libAttr.guid, typeInfo);
}
oldChild = parent.SelectSingleNode(string.Format("file[name='{0}']",
Path.GetFileName(fileName)));
if (oldChild != null)
parent.ReplaceChild(file, oldChild);
else
parent.AppendChild(file);
}
catch (System.Runtime.InteropServices.COMException)
{
// just ignore if this isn't a type library
if (m_fDisplayWarnings)
Console.WriteLine("Warning: Can't load type library {0}", fileName);
}
}
示例6: ProcessTypeLibrary
/// ------------------------------------------------------------------------------------
/// <summary>
/// Creates the entries for the type library and all classes defined in this type
/// library. We can get the necessary information for the file element (file name and
/// size) directly from the file. We get the information for the type library from
/// the type library itself.
/// </summary>
/// <param name="parent">The parent node.</param>
/// <param name="fileName">Name (and path) of the file.</param>
/// ------------------------------------------------------------------------------------
public void ProcessTypeLibrary(XmlElement parent, string fileName)
{
_baseDirectory = Path.GetDirectoryName(fileName);
_fileName = fileName.ToLower();
try
{
_log.LogMessage(MessageImportance.Low, "\tProcessing type library {0}", fileName);
XmlNode oldChild;
ITypeLib typeLib;
RegHelper.LoadTypeLib(_fileName, out typeLib);
IntPtr pLibAttr;
typeLib.GetLibAttr(out pLibAttr);
var libAttr = (TYPELIBATTR)
Marshal.PtrToStructure(pLibAttr, typeof(TYPELIBATTR));
typeLib.ReleaseTLibAttr(pLibAttr);
string flags = string.Empty;
if ((libAttr.wLibFlags & LIBFLAGS.LIBFLAG_FHASDISKIMAGE) == LIBFLAGS.LIBFLAG_FHASDISKIMAGE)
flags = "HASDISKIMAGE";
// <file name="FwKernel.dll" asmv2:size="1507328">
var file = GetOrCreateFileNode(parent, fileName);
// <typelib tlbid="{2f0fccc0-c160-11d3-8da2-005004defec4}" version="1.0" helpdir=""
// resourceid="0" flags="HASDISKIMAGE" />
if (_tlbGuids.ContainsKey(libAttr.guid))
{
_log.LogWarning("Type library with GUID {0} is defined in {1} and {2}",
libAttr.guid, _tlbGuids[libAttr.guid], Path.GetFileName(fileName));
}
else
{
_tlbGuids.Add(libAttr.guid, Path.GetFileName(fileName));
XmlElement elem = _doc.CreateElement("typelib", UrnAsmv1);
elem.SetAttribute("tlbid", libAttr.guid.ToString("B"));
elem.SetAttribute("version", string.Format("{0}.{1}", libAttr.wMajorVerNum,
libAttr.wMinorVerNum));
elem.SetAttribute("helpdir", string.Empty);
elem.SetAttribute("resourceid", "0");
elem.SetAttribute("flags", flags);
oldChild = file.SelectSingleNode(string.Format("asmv1:typelib[asmv1:tlbid='{0}']",
libAttr.guid.ToString("B")), _nsManager);
if (oldChild != null)
file.ReplaceChild(elem, oldChild);
else
file.AppendChild(elem);
}
Debug.WriteLine(@"typelib tlbid=""{0}"" version=""{1}.{2}"" helpdir="""" resourceid=""0"" flags=""{3}""",
libAttr.guid, libAttr.wMajorVerNum, libAttr.wMinorVerNum, flags);
int count = typeLib.GetTypeInfoCount();
_log.LogMessage(MessageImportance.Low, "\t\tTypelib has {0} types", count);
for (int i = 0; i < count; i++)
{
ITypeInfo typeInfo;
typeLib.GetTypeInfo(i, out typeInfo);
ProcessTypeInfo(parent, libAttr.guid, typeInfo);
}
oldChild = parent.SelectSingleNode(string.Format("asmv1:file[asmv1:name='{0}']",
Path.GetFileName(fileName)), _nsManager);
if (oldChild != null)
parent.ReplaceChild(file, oldChild);
else
parent.AppendChild(file);
}
catch (Exception)
{
// just ignore if this isn't a type library
_log.LogMessage(MessageImportance.Normal, "Can't load type library {0}", fileName);
}
}
示例7: insertXmlNode
//直接和xml打交道的处理和部分对于显示的刷新。
//把dstXe加到srcXe里
public static bool insertXmlNode(MainWindow pW, XmlControl xmlCtrl, XmlElement dstXe, XmlElement srcXe, int index = 0)
{
XmlElement tmpXe1 = dstXe.OwnerDocument.CreateElement("tmp1");
XmlElement tmpXe2 = dstXe.OwnerDocument.CreateElement("tmp2");
if(index < (srcXe.ChildNodes.Count + 1) / 2)
{
srcXe.PrependChild(tmpXe1);
XmlNode iXe = tmpXe1;
for (int i = 0; i < index; i++)
{
XmlNode nextXe = iXe.NextSibling;
srcXe.ReplaceChild(tmpXe2, nextXe);
srcXe.ReplaceChild(nextXe, tmpXe1);
srcXe.ReplaceChild(tmpXe1, tmpXe2);
iXe = tmpXe1;
}
srcXe.ReplaceChild(dstXe, tmpXe1);
}
else
{
srcXe.AppendChild(tmpXe1);
XmlNode iXe = tmpXe1;
for (int i = srcXe.ChildNodes.Count - 1; i > index; i--)
{
XmlNode prevXe = iXe.PreviousSibling;
srcXe.ReplaceChild(tmpXe2, prevXe);
srcXe.ReplaceChild(prevXe, tmpXe1);
srcXe.ReplaceChild(tmpXe1, tmpXe2);
iXe = tmpXe1;
}
srcXe.ReplaceChild(dstXe, tmpXe1);
}
if (xmlCtrl != null)
{
insertItemByXe(pW, xmlCtrl, dstXe, srcXe, ref index);
}
return false;
}
示例8: PreprocessElement
private void PreprocessElement(XmlDocument document, XmlElement element, ref List<PreprocessorAreaData> datas, PreprocessorAreaData parentArea = null)
{
List<XmlElement> nodes = element.ChildNodes.OfType<XmlElement>().ToList();
foreach (XmlElement subElement in nodes)
{
if (subElement == null)
{
continue;
}
switch (subElement.Name.ToLower())
{
case "mssqlauditorpreprocessor":
{
string className = subElement.Attributes["preprocessor"].Value;
XmlAttribute idAttr = subElement.Attributes["id"];
XmlAttribute nameAttr = subElement.Attributes["name"];
XmlAttribute columnAttr = subElement.Attributes["column"];
XmlAttribute rowAttr = subElement.Attributes["row"];
XmlAttribute colSpanAttr = subElement.Attributes["colspan"];
XmlAttribute rowSpanAttr = subElement.Attributes["rowspan"];
XmlAttribute vertTestAlign = subElement.Attributes["text-vertical-align"];
XmlAttribute textAlign = subElement.Attributes["text-align"];
string id = "";
string preprocName = "unnamed";
if (nameAttr != null)
{
preprocName = nameAttr.Value;
}
if (idAttr != null)
{
id = idAttr.Value;
}
int col = ParseIntAttribute(columnAttr, 1);
int row = ParseIntAttribute(rowAttr, 1);
int colSpan = ParseIntAttribute(colSpanAttr, 1);
int rowSpan = ParseIntAttribute(rowSpanAttr, 1);
VerticalTextAlign? preprocVertTestAlign = null;
if (vertTestAlign != null)
{
VerticalTextAlign tempVertTestAlign;
if (Enum.TryParse(vertTestAlign.Value, out tempVertTestAlign))
{
preprocVertTestAlign = tempVertTestAlign;
}
}
TextAlign preprocTestAlign = TextAlign.Left;
if (textAlign != null)
{
if (!Enum.TryParse(textAlign.Value, out preprocTestAlign))
{
preprocTestAlign = TextAlign.Left;
}
}
IPreprocessor preprocessor =
(from proc in this._availablePreprocessors where proc.GetType().Name == className select proc)
.FirstOrDefault();
if (preprocessor != null)
{
string configuration = subElement.InnerXml;
IContentFactory factory = preprocessor.CreateContentFactory(id, configuration);
PreprocessorData data = new PreprocessorData
{
ContentFactory = factory,
Name = preprocName,
Column = col,
Row = row,
ColSpan = colSpan,
RowSpan = rowSpan,
VerticalTextAlign = preprocVertTestAlign,
TextAlign = preprocTestAlign
};
XmlElement newSubElement = document.CreateElement("div");
newSubElement.SetAttribute("style", "margin:0; padding:0;");
newSubElement.InnerXml = string.Empty;
element.ReplaceChild(newSubElement, subElement);
if (parentArea != null)
{
parentArea.Preprocessors.Add(data);
}
else
{
//.........这里部分代码省略.........
示例9: compareFldrs
/// <summary>
/// Сравнивает два документа. Результат в pFldLocal
/// </summary>
/// <param name="pFldLocal"></param>
/// <param name="pFldRemote"></param>
public static void compareFldrs(XmlElement pFldLocal, XmlElement pFldRemote) {
/*Обновление модулей и удаление старых*/
XmlNodeList vMdls = pFldLocal.SelectNodes("assembly");
foreach(XmlElement vNode in vMdls) {
XmlElement vRNode = (XmlElement)pFldRemote.SelectSingleNode("assembly[@name='" + vNode.GetAttribute("name") + "']");
if((vRNode != null) && (Utl.CompareVer(vRNode.GetAttribute("version"), vNode.GetAttribute("version")) > 0)) {
XmlNode vNewEl = pFldLocal.OwnerDocument.ImportNode(vRNode, true);
pFldLocal.ReplaceChild(vNewEl, vNode);
} else {
if(vRNode == null)
vNode.SetAttribute("action", "kill");
else
vNode.SetAttribute("action", "skip");
}
}
/*Добавляем новые модули*/
vMdls = pFldRemote.SelectNodes("assembly");
foreach(XmlElement vNode in vMdls) {
XmlElement vLNode = (XmlElement)pFldLocal.SelectSingleNode("assembly[@name='" + vNode.GetAttribute("name") + "']");
if(vLNode == null) {
XmlNode vNewEl = pFldLocal.OwnerDocument.ImportNode(vNode, true);
pFldLocal.AppendChild(vNewEl);
}
}
/*Обновляем существующие и удаляем старые папки*/
vMdls = pFldLocal.SelectNodes("folder");
foreach(XmlElement vNode in vMdls) {
XmlElement vRNode = (XmlElement)pFldRemote.SelectSingleNode("folder[@name='" + vNode.GetAttribute("name") + "']");
if(vRNode != null) {
compareFldrs(vNode, vRNode);
} else {
if(!vNode.GetAttribute("name").Equals("trash"))
vNode.SetAttribute("action", "kill");
}
}
/*Добавляем новые папки*/
vMdls = pFldRemote.SelectNodes("folder");
foreach(XmlElement vNode in vMdls) {
XmlNode vLNode = pFldLocal.SelectSingleNode("folder[@name='" + vNode.GetAttribute("name") + "']");
if(vLNode == null) {
XmlNode vNewEl = pFldLocal.OwnerDocument.ImportNode(vNode, true);
pFldLocal.AppendChild(vNewEl);
}
}
}