1 using System;
2 using System.Dom;
3 using System.IO;
4 using System.Text;
5
6 public UniquePtr<Document> CreateDoc()
7 {
8 UniquePtr<Document> doc(new Document());
9 UniquePtr<Element> phs(new Element(u"philosophers"));
10 UniquePtr<Element> ph1(new Element(u"philosopher"));
11 ph1->SetAttribute(u"name", u"Plato");
12 phs->AppendChild(UniquePtr<Node>(ph1.Release()));
13 UniquePtr<Element> ph2(new Element(u"philosopher"));
14 ph2->SetAttribute(u"name", u"Aristotle");
15 phs->AppendChild(UniquePtr<Node>(ph2.Release()));
16 UniquePtr<Element> ph3(new Element(u"philosopher"));
17 ph3->SetAttribute(u"name", u"Socrates");
18 phs->AppendChild(UniquePtr<Node>(ph3.Release()));
19 doc->AppendChild(UniquePtr<Node>(phs.Release()));
20 return doc;
21 }
22
23 public void WriteDocToFile(Document* doc)
24 {
25 StreamWriter writer = File.CreateText("philosophers.xml");
26 CodeFormatter formatter(writer);
27 formatter.SetIndentSize(1);
28 doc->Write(formatter);
29 }
30
31 public UniquePtr<Document> ReadDocFromFile()
32 {
33 UniquePtr<Document> doc = ReadDocument("philosophers.xml");
34 return doc;
35 }
36
37 public void WriteDocToConsole(Document* doc)
38 {
39 CodeFormatter formatter(Console.Out());
40 formatter.SetIndentSize(4);
41 doc->Write(formatter);
42 }
43
44 int main()
45 {
46 try
47 {
48 UniquePtr<Document> doc = CreateDoc();
49 WriteDocToFile(doc.Get());
50 UniquePtr<Document> docRead = ReadDocFromFile();
51 WriteDocToConsole(docRead.Get());
52 }
53 catch (const Exception& ex)
54 {
55 Console.Error() << ex.Message() << endl();
56 return 1;
57 }
58 return 0;
59 }