[태그:] <span>CreateAttribute</span>

 XmlDocuemnt 클래스에는 필요한 형식의 노드를 생성할 수 있는 Create 메서드를 제공하고 있습니다.

 노드의 선언을 만들 때는 CreateXmlDeclaration 메서드를 사용합니다.

public XmlDeclaration CreateXmlDeclaration(string version, 
                                           string encoding, 
                                           string stdandalone);

  버전은 항상 “1.0”이어야 하고 인코딩을 Encoding 클래스에서 지원하는 문자열로 설정합니다. 만약 인코딩을 null로 지정하면 기본 인코딩을 사용합니다.그리고 standalone의 값은 “yes” 혹은 “no”을 사용할 수 있고 null이나 string.Empty을 사용하면 선언에 기록하지 않습니다.

 XmlDeclartaion 개체는 XmlDocument 개체의 DocumentElement 속성 앞에 추가합니다.

XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", null, null);
XmlElement root = doc.DocumentElement;
doc.InsertBefore(decl, doc.DocumentElement);

  요소를 만들 때는 CreateElement 메서드를 사용합니다.

public XmlElement CreateElement ( string name);
public XmlElement CreateElement ( string name, string ns);
public XmlElement CreateElement ( string prefix, string name, string ns);
XmlElement root = doc.CreateElement("books");
doc.AppendChild(root);
XmlElement elem = doc.CreateElement("book");
elem.InnerText = "XML.NET";
doc.DocumentElement.AppendChild(elem);

  특성을 추가할 때는 이미 만들어진 요소에 추가합니다. 특성을 추가할 요소 개체에 SetAttribute 메서드를 이용하여 특성을 추가할 수 있습니다. 또 다른 방법으로는 CreateAttribute 메서드를 이용해 XmlAtrribute 노드를 만들고 나서 추가할 요소 개체의 특성 컬렉션에 추가할 수도 있습니다.

public XmlAttribute CreateAttribute (string name);
public XmlAttribute CreateAttribute (string name, string ns);
public XmlAttribute CreateAttribute (string prefix, string name, string ns);
elem.SetAttribute("price", "12000");
XmlAttribute attr = doc.CreateAttribute("count");
attr.Value = "50";
elem.Attributes.Append(attr);

 주석을 만들 때는 CreateComment 메서드를 사용합니다.

public XmlComment CreateComment(string data);
XmlComment comment;
comment = doc.CreateComment("XML 노드 삽입");
doc.AppendChild(comment);

 이 외에도 다른 노드 형식의 개체를 위한 Create 메서드를 제공하고 있습니다.

 이렇게 생성한 노드 개체는 XmlDocument 개체의 노드를 삽입하는 메소드를 이용하여 원하는 위치에 노드를 추가할 수 있습니다.

public XmlNode InsertBefore ( XmlNode new_node, XmlNode old_node);
public XmlNode InsertAfter ( XmlNode new_node, XmlNode old_node);
public XmlNode AppendChild ( XmlNode new_node);
public XmlNode PrependChild ( XmlNode new_node);
public XmlNode Append ( XmlNode new_node);

 다음은 앞에서 설명한 내용을 정리한 소스 코드입니다.

using System;
using System.Xml;

namespace 예제_노드_삽입
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            
            XmlDeclaration xmldecl;
            xmldecl = doc.CreateXmlDeclaration("1.0", null, null);
            doc.InsertBefore(xmldecl, doc.DocumentElement);
            
            XmlComment comment;
            comment = doc.CreateComment("XML 노드 삽입");
            doc.AppendChild(comment);
            
            XmlElement root = doc.CreateElement("books");
            doc.AppendChild(root);
            
            XmlElement elem = doc.CreateElement("book");
            elem.InnerText = "XML.NET";
            doc.DocumentElement.AppendChild(elem);
            elem.SetAttribute("price", "12000");

            XmlAttribute attr = doc.CreateAttribute("count");
            attr.Value = "50";
            elem.Attributes.Append(attr);
            
            doc.Save(Console.Out);
            Console.WriteLine();
        }
    }
}
실행화면

XML.NET