VB.NET, C#, PowerShellを使用しているエンジニアのためのサイト。
基本的な使用方法から開発で役立つTipsまで幅広く取り扱っています。
属性を追加するには、XmlAttributeクラスを使用します。XmlAttributeクラスのValueプロパティで値を設定した後、属性を追加する要素のArrtibutes.Appendメソッドを使用して属性を追加します。
Dim xmlDoc As New System.Xml.XmlDocument 'XML宣言を設定する Dim xmlDecl As System.Xml.XmlDeclaration = _ xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", Nothing) '作成したXML宣言をDOMドキュメントに追加します xmlDoc.AppendChild(xmlDecl) 'ルート要素の作成 Dim rootElement As System.Xml.XmlElement = xmlDoc.CreateElement("personal_data") '属性を追加する Dim Attr As System.Xml.XmlAttribute = xmlDoc.CreateAttribute("zip") Attr.Value = "123-4567" rootElement.Attributes.Append(Attr) 'ルート要素をDOMドキュメントに追加する xmlDoc.AppendChild(rootElement) Try '作成したDOMドキュメントをファイルに保存 xmlDoc.Save("C:\Work\Test.xml") Catch ex As System.Xml.XmlException 'XMLによる例外をキャッチ Console.WriteLine(ex.Message) End Try