本文整理汇总了C#中System.Xml.XmlDocument.ReplaceChild方法的典型用法代码示例。如果您正苦于以下问题:C# XmlDocument.ReplaceChild方法的具体用法?C# XmlDocument.ReplaceChild怎么用?C# XmlDocument.ReplaceChild使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.XmlDocument
的用法示例。
在下文中一共展示了XmlDocument.ReplaceChild方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DocumentElementNodeWithNewNode
public static void DocumentElementNodeWithNewNode()
{
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml("<root/>");
var root = xmlDocument.DocumentElement;
var newRoot = xmlDocument.CreateElement("newroot");
xmlDocument.ReplaceChild(newRoot, root);
Assert.Same(newRoot, xmlDocument.DocumentElement);
}
示例2: SaveToXml
//.........这里部分代码省略.........
if (targetXmlNode == null)
{
return;
}
if (XMLNode == null)
{
string xml = "";
if (sXMLEncoding == "UTF-16")
{
xml = "<?xml version='1.0' encoding='UTF-16'?>";
}
else if (sXMLEncoding == "UTF-8")
{
xml = "<?xml version='1.0' encoding='UTF-8'?>";
}
else if (sXMLEncoding.Length > 0)
{
xml = "<?xml version='1.0' encoding='" + sXMLEncoding + "'?>";
}
else
{
xml = "<?xml version='1.0'?>";
}
xml = xml + "<root/>";
xmlDoc.LoadXml(xml);
if ((type_.type == ElementType.Math) && displayStyle)
{
XmlAttribute attribute = xmlDoc.CreateAttribute("", "display", "");
attribute.Value = "block";
targetXmlNode.Attributes.Append(attribute);
}
xmlDoc.ReplaceChild(targetXmlNode, xmlDoc.DocumentElement);
}
else if ((!ownSel && !false) && !false)
{
{
XMLNode.AppendChild(targetXmlNode);
}
if ((type_.type == ElementType.Math) && displayStyle)
{
XmlAttribute attribute = xmlDoc.CreateAttribute("", "display", "");
attribute.Value = "block";
targetXmlNode.Attributes.Append(attribute);
XMLNode.AppendChild(targetXmlNode);
}
}
if (((((Selection_Collection == null) || (this != Selection_Collection.parent)))) && ((!HasChildren())))
{
return;
}
if ((Selection_Collection != null) && (this == Selection_Collection.parent))
{
list = Selection_Collection.nodesList;
}
else
{
list = GetChildrenNodes();
}
list.Reset();
Node next = list.Next();
int level = 0;
bool sameSel = false;
bool isPrev = false;
示例3: updateElementName
private void updateElementName(XmlDocument doc, String oldName, String newName)
{
XmlElement oldElement = (XmlElement)doc.GetElementsByTagName(oldName)[0];
XmlElement newElement = copyElementToName(oldElement, newName);
doc.ReplaceChild(newElement,oldElement);
}
示例4: Apply
/// <inheritdoc />
public override void Apply(XmlDocument document, string key)
{
// Set the evaluation context
context["key"] = key;
XPathExpression xpath = pathExpression.Clone();
xpath.SetContext(context);
// Evaluate the path
string path = document.CreateNavigator().Evaluate(xpath).ToString();
if(basePath != null)
path = Path.Combine(basePath, path);
string targetDirectory = Path.GetDirectoryName(path);
if(!Directory.Exists(targetDirectory))
Directory.CreateDirectory(targetDirectory);
if(writeXhtmlNamespace)
{
document.DocumentElement.SetAttribute("xmlns", "http://www.w3.org/1999/xhtml");
document.LoadXml(document.OuterXml);
}
if (settings.OutputMethod == XmlOutputMethod.Html)
{
XmlDocumentType documentType = document.CreateDocumentType("html", null, null, null);
if (document.DocumentType != null)
{
document.ReplaceChild(documentType, document.DocumentType);
}
else
{
document.InsertBefore(documentType, document.FirstChild);
}
}
// Save the document.
// selectExpression determines which nodes get saved. If there is no selectExpression we simply
// save the root node as before. If there is a selectExpression, we evaluate the XPath expression
// and save the resulting node set. The select expression also enables the "literal-text" processing
// instruction, which outputs its content as unescaped text.
if(selectExpression == null)
{
try
{
using(XmlWriter writer = XmlWriter.Create(path, settings))
{
document.Save(writer);
}
}
catch(IOException e)
{
base.WriteMessage(key, MessageLevel.Error, "An access error occurred while attempting to " +
"save to the file '{0}'. The error message is: {1}", path, e.GetExceptionMessage());
}
catch(XmlException e)
{
base.WriteMessage(key, MessageLevel.Error, "Invalid XML was written to the output " +
"file '{0}'. The error message is: '{1}'", path, e.GetExceptionMessage());
}
}
else
{
// IMPLEMENTATION NOTE: The separate StreamWriter is used to maintain XML indenting. Without it
// the XmlWriter won't honor our indent settings after plain text nodes have been written.
using(StreamWriter output = File.CreateText(path))
{
using(XmlWriter writer = XmlWriter.Create(output, settings))
{
XPathExpression select_xpath = selectExpression.Clone();
select_xpath.SetContext(context);
XPathNodeIterator ni = document.CreateNavigator().Select(selectExpression);
while(ni.MoveNext())
{
if(ni.Current.NodeType == XPathNodeType.ProcessingInstruction &&
ni.Current.Name.Equals("literal-text"))
{
writer.Flush();
output.Write(ni.Current.Value);
}
else
ni.Current.WriteSubtree(writer);
}
}
}
}
// Raise an event to indicate that a file was created
this.OnComponentEvent(new FileCreatedEventArgs(path, true));
}
示例5: UpdateElementName
public void UpdateElementName(XmlDocument doc, string oldName, string newName)
{
var itemOf = (XmlElement)doc.GetElementsByTagName(oldName)[0];
var name = CopyElementToName(itemOf, newName);
doc.ReplaceChild(name, itemOf);
}
示例6: SerializeGovTalkMessage
public XmlDocument SerializeGovTalkMessage()
{
_loggingService.LogInfo(this, "Serializing GovTalkMessage.");
using(MemoryStream memStream = new MemoryStream())
{
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
IndentChars = "\t",
Encoding = Encoding.UTF8
};
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add(string.Empty, "http://www.govtalk.gov.uk/CM/envelope");
XmlSerializer serializer = new XmlSerializer(typeof(GovTalkMessage));
serializer.Serialize(XmlWriter.Create(memStream,settings), _govTalkMessageBuilder.GovTalkMessage, ns);
memStream.Seek(0, SeekOrigin.Begin);
// Use XmlWriter to make use of settings
// Load to XmlDoc for ease of use by client
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.Load(memStream);
XmlDeclaration xmlDeclaration;
xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.ReplaceChild(xmlDeclaration, doc.FirstChild);
return doc;
}
}
示例7: GetEntity
public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
{
string local_path = absoluteUri.LocalPath;
bool root_xsl = local_path.Equals (local_path, StringComparison.CurrentCultureIgnoreCase);
XmlDocument doc = new XmlDocument ();
doc.Load (local_path);
if (_files != null)
_files.Add (local_path);
XmlNodeList list = doc.DocumentElement.GetElementsByTagName ("output", NS_XSL);
for (int i = 0; i < list.Count; i++)
doc.DocumentElement.RemoveChild (list[i]);
if (!_html4) {
if (root_xsl) {
SetupXSLOutput (doc, "xml", "utf-8", "-//W3C//DTD XHTML 1.1//EN", "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd", _indent);
}
} else {
XmlElement new_root = ConvertXHTMLtoHTML (doc, doc.DocumentElement);
doc.ReplaceChild (new_root, doc.DocumentElement);
if (root_xsl) {
SetupXSLOutput (doc, "html", "utf-8", "-//W3C//DTD HTML 4.01//EN", "http://www.w3.org/TR/html4/strict.dtd", _indent);
}
}
using (MemoryStream ms = new MemoryStream ())
using (XmlTextWriter writer = new XmlTextWriter (ms, System.Text.Encoding.UTF8)) {
doc.WriteTo (writer);
writer.Close ();
ms.Close ();
return new MemoryStream (ms.ToArray ());
}
}
示例8: ReplaceNodeWithChildrenWithEmptyNode
public static void ReplaceNodeWithChildrenWithEmptyNode()
{
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml("<root><child1/><child2/></root>");
var newNode = xmlDocument.CreateElement("newElement");
Assert.True(xmlDocument.DocumentElement.HasChildNodes);
xmlDocument.ReplaceChild(newNode, xmlDocument.DocumentElement);
Assert.False(xmlDocument.DocumentElement.HasChildNodes);
}