using System; using System.IO; using System.Xml;public class Sample { public static void Main() { //Create the XmlDocument. XmlDocument doc = new XmlDocument(); doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" + "<title>Pride And Prejudice</title>" + "</book>"); //Create a new node and add it to the document. //The text node is the content of the price element. XmlElement elem = doc.CreateElement("price"); XmlText text = doc.CreateTextNode("19.95"); doc.DocumentElement.AppendChild(elem); doc.DocumentElement.LastChild.AppendChild(text); Console.WriteLine("Display the modified XML..."); doc.Save(Console.Out); } } 这样它就添加了price这个成员
添加成员的方法来的??举个例子看看
using System.IO;
using System.Xml;public class Sample
{
public static void Main()
{
//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" +
"</book>"); //Create a new node and add it to the document.
//The text node is the content of the price element.
XmlElement elem = doc.CreateElement("price");
XmlText text = doc.CreateTextNode("19.95");
doc.DocumentElement.AppendChild(elem);
doc.DocumentElement.LastChild.AppendChild(text); Console.WriteLine("Display the modified XML...");
doc.Save(Console.Out); }
}
这样它就添加了price这个成员
因为一个独立的XmlElement 没有任何意义
保证一个类仅有一个实例。 二,实现
Singleton定义如下: class Singleton
{
public:
static Singleton* Instance() ;
protected:
Singleton() {}
private:
static Singleton *_instance ;
Singleton(const Singleton&) ;
Singleton& operator=(const Singleton&) ;
} ;Singleton* Singleton::_instance = NULL ;Singleton* Singleton::Instance()
{
(_instance == NULL) ? _instance = new Singleton() : 0 ; //lazy initialization
return _instance ;
}
(1)因为返回的是指针,为防止用户调用delete函数,可把static Singleton *_instance;改为在Instance()中定义static Singleton _instance。这样显然更安全,同时也具有lazy initialization的特性(即第一次访问时才创建)。 (2)假设需要从Singleton派生子类,而子类也需要有同样的性质,既只能创建一个实例。我觉得,这很难办。根本原因在于Instance()函数不是虚函数,不具有多态的性质。一种常用方法是把Instance()函数移到子类中,这时就只能用static Singleton *_instance,而不能用static Singleton _instance了,除非把_instance也要移到子类,无论怎么做都不优雅。另一种方法是用模板。具体用什么方法,只能根据实际情况权衡。
我认为你的理解是对的,我也这么认为的,我还想明白它的具体实现,以及这种方法的相关模式描述。
这不是 Singleton 模式。
Elment、Attribute、Comment它们只能在 XmlDocument 类中创建,组装成一个 Xml Document。我觉得这里面可能涉及到 Composite 模式,但在 Composite 模式中,各个 Part 都是可以单独存在的。