private void Form1_Load(object sender, System.EventArgs e) { const string xmlns = "http://www.atmarkit.co.jp/xmlns/sample/person"; XmlDocument document = new XmlDocument(); XmlDeclaration xmlDecl = document.CreateXmlDeclaration("1.0", "UTF-8", null); document.AppendChild(xmlDecl); XmlElement rootElement = document.CreateElement("person", "record", xmlns); document.AppendChild(rootElement); XmlAttribute idAttr = document.CreateAttribute("id"); idAttr.Value = "0011"; rootElement.Attributes.Append(idAttr); XmlElement nameElement = document.CreateElement("person", "name", xmlns); rootElement.AppendChild(nameElement); XmlText nameText = document.CreateTextNode("山田一郎"); nameElement.AppendChild(nameText); XmlElement ageElement = document.CreateElement("person", "age", xmlns); rootElement.AppendChild(ageElement); ageElement.InnerText = "17"; XmlElement addressElement = document.CreateElement("person", "address", xmlns); rootElement.AppendChild(addressElement); addressElement.InnerXml = "jp" + "東京都杉並区1-2-3"; XmlComment comment1 = document.CreateComment("サンプルプログラムの出力"); rootElement.InsertBefore(comment1, nameElement); XmlComment comment2 = document.CreateComment("個人情報"); rootElement.InsertAfter(comment2, nameElement); document.Save(@"c:\sample.xml"); }