관리 메뉴

프로그램 소스

[XML.NET C# 소스] XmlDocument에 노드 탐색 및 삭제하기 본문

XML.NET

[XML.NET C# 소스] XmlDocument에 노드 탐색 및 삭제하기

Only 프로그램 소스 언제나휴일 2016.04.18 16:15

 XmlDocument에 노드 탐색 및 삭제하기


Program.cs


class Tracer

{

    XmlDocument doc;

 

    public void MakeDocument()

    {

        doc = new XmlDocument();

        XmlDeclaration xmldecl;

        xmldecl = doc.CreateXmlDeclaration("1.0", null, null);

        doc.InsertBefore(xmldecl, doc.DocumentElement);

 

        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);

        XmlElement elem2 = doc.CreateElement("book");

        elem2.InnerText = "ADO.NET";

        doc.DocumentElement.AppendChild(elem2);

    }

 

    public void Trace()

    {

        TestXmlNode_RemoveChild();

        TestXmlNode_RemoveAll();

        TestAttributes_Remove();

        TestAttributes_RemoveAll();

        TestAttributes_RemoveAt();

        TestElement_RemoveAllAttributes();

        TestElement_RemoveAttribute();

        TestElement_RemoveAttributeAt();

    }

 

    private void TestElement_RemoveAttributeAt()

    {

        Console.WriteLine("---Start TestElement_RemoveAttributeAt---");

        MakeDocument();

        XmlNode pnode = doc.SelectSingleNode("books");

        XmlNode cnode = pnode.ChildNodes[0];

        XmlElement xelem = cnode as XmlElement;

        if (xelem != null)

        {

            xelem.RemoveAttributeAt(0);

        }

        doc.Save(Console.Out);

        Console.WriteLine();

        Console.WriteLine("---End TestElement_RemoveAttributeAt---");

    }

    private void TestElement_RemoveAttribute()

    {

        Console.WriteLine("---Start TestElement_RemoveAttribute---");

        MakeDocument();

        XmlNode pnode = doc.SelectSingleNode("books");

        XmlNode cnode = pnode.ChildNodes[0];

        XmlElement xelem = cnode as XmlElement;

        if (xelem != null)

        {

            xelem.RemoveAttribute("price");

        }

        doc.Save(Console.Out);

        Console.WriteLine();

        Console.WriteLine("---End TestElement_RemoveAttribute---");

    }

    private void TestElement_RemoveAllAttributes()

    {

        Console.WriteLine("---Start TestElement_RemoveAllAttributes---");

        MakeDocument();

        XmlNode pnode = doc.SelectSingleNode("books");

        XmlNode cnode = pnode.ChildNodes[0];

        XmlElement xelem = cnode as XmlElement;

        if (xelem != null

        {

            xelem.RemoveAllAttributes();

        }

        doc.Save(Console.Out);

        Console.WriteLine();

        Console.WriteLine("---End TestElement_RemoveAllAttributes---");

    }

 

    private void TestAttributes_RemoveAt()

    {

        Console.WriteLine("---Start TestAttributes_RemoveAt---");

        MakeDocument();

        XmlNode pnode = doc.SelectSingleNode("books");

        XmlNode cnode = pnode.ChildNodes[0];

        XmlAttributeCollection col = cnode.Attributes;

        col.RemoveAt(0);

        doc.Save(Console.Out);

        Console.WriteLine();

        Console.WriteLine("---End TestAttributes_RemoveAt---");

    }

    private void TestAttributes_RemoveAll()

    {

        Console.WriteLine("---Start TestAttributes_RemoveAll---");

        MakeDocument();

        XmlNode pnode = doc.SelectSingleNode("books");

        XmlNode cnode = pnode.ChildNodes[0];

        XmlAttributeCollection col = cnode.Attributes;

        col.RemoveAll();

        doc.Save(Console.Out);

        Console.WriteLine();

        Console.WriteLine("---End TestAttributes_RemoveAll---");

    }

    private void TestAttributes_Remove()

    {

        Console.WriteLine("---Start TestAttributes_Remove---");

        MakeDocument();

        XmlNode pnode = doc.SelectSingleNode("books");

        XmlNode cnode = pnode.ChildNodes[0];

        XmlAttributeCollection col = cnode.Attributes;

        XmlAttribute attr = col["price"];

        col.Remove(attr);

        doc.Save(Console.Out);

        Console.WriteLine();

        Console.WriteLine("---End TestAttributes_Remove---");

    }

    private void TestXmlNode_RemoveAll()

    {

        Console.WriteLine("---Start TestXmlNode_RemoveAll---");

        MakeDocument();

        XmlNode pnode = doc.SelectSingleNode("books");

        pnode.RemoveAll();

        doc.Save(Console.Out);

        Console.WriteLine();

        Console.WriteLine("---End TestXmlNode_RemoveAll---");

    }

    private void TestXmlNode_RemoveChild()

    {

        Console.WriteLine("---Start TestXmlNode_RemoveChild---");

        MakeDocument();

        XmlNode pnode = doc.SelectSingleNode("books");

        XmlNode cnode = pnode.ChildNodes[0];

        pnode.RemoveChild(cnode);

        doc.Save(Console.Out);

        Console.WriteLine();

        Console.WriteLine("---End TestXmlNode_RemoveChild---");

   }

}

class Program

{

    static void Main(string[] args)

    {

        Tracer tracer = new Tracer();

        tracer.Trace();

    }

}

[소스XmlDocument에 노드 탐색 및 삭제하기 예제 코드

 

언제나 휴일 티스토리 바로가기

무료 동영상 강의 유튜브 채널 바로가기

0 Comments
댓글쓰기 폼