本文整理匯總了C#中System.Xml.XmlAttribute.CloneNode方法的典型用法代碼示例。如果您正苦於以下問題:C# XmlAttribute.CloneNode方法的具體用法?C# XmlAttribute.CloneNode怎麽用?C# XmlAttribute.CloneNode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。
在下文中一共展示了XmlAttribute.CloneNode方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
//Create an XmlDocument.
XmlDocument doc = new XmlDocument();
doc.Load("2elems.xml");
//Create an attribute.
XmlAttribute attr;
attr = doc.CreateAttribute("bk","genre","urn:samples");
attr.Value = "novel";
//Add the attribute to the first book.
XmlElement currNode = (XmlElement) doc.DocumentElement.FirstChild;
currNode.SetAttributeNode(attr);
//An attribute cannot be added to two different elements.
//You must clone the attribute and add it to the second book.
XmlAttribute attr2;
attr2 = (XmlAttribute) attr.CloneNode(true);
currNode = (XmlElement) doc.DocumentElement.LastChild;
currNode.SetAttributeNode(attr2);
Console.WriteLine("Display the modified XML...\r\n");
XmlTextWriter writer = new XmlTextWriter(Console.Out);
writer.Formatting = Formatting.Indented;
doc.WriteContentTo(writer);
}
}