2007. 7. 9. 03:49

보호되어 있는 글입니다.
내용을 보시려면 비밀번호를 입력하세요.

posted by 써니루루 2007. 7. 8. 23:10
http://www.ibm.com/developerworks/kr/library/wa-ajaxintro5/

Ajax를 이용해 받아온 데이터를 DOM으로 제어하는 경우가 많습니다.

기본적인 사항을 위 페이지에서 보실 수 있습니다.
posted by 써니루루 2007. 3. 26. 11:56

dataset에서 GetXml()이나 GetXmlSchema() 메소드를 이용한다.


위 SQL 파일을 DB 에서 실행해서 해당 Database를 만들어야 예제를 실행할 수 있다.

아래 예제코드는 위에서 입력된 자료를 이용해서 XML로 가져오는 예제코드이다.

using System;
using System.Data;
using System.Data.SqlClient;

namespace CH13
{
    class GetXmlAndSchema
    {
        static void Main()
        {
            string strConn = "Data Source=localhost;Initial Catalog=booksourcedb;Integrated Security=True";
            string strSql = "SELECT * FROM book";
            SqlDataAdapter dataAdapter = new SqlDataAdapter(strSql, strConn);

            DataSet dataSet = new DataSet("booklist");
            dataAdapter.Fill(dataSet, "book");

            string strXml = dataSet.GetXml();
            string strSchema = dataSet.GetXmlSchema();

            Console.WriteLine("[DataSet 내용을 XML 문서로 쓰기]");
            Console.WriteLine("------------------------------------");
            Console.WriteLine(strXml);

            Console.WriteLine("\n[DataSet 구조를 Schema 문서로 쓰기]");
            Console.WriteLine("------------------------------------");
            Console.WriteLine(strSchema);
        }
    }
}


Ref. .NET 개발자를 위한 XML p.600

posted by 써니루루 2007. 3. 26. 11:40

invalid-file

예제코드

XSLT를 이용해서 문서를 변환하는 예제를 보여준다.

using System;
using System.Xml;
using System.Xml.Xsl;
using System.Diagnostics;

namespace booksource.ch12
{
  class Transformation
 {
  static void Main()
  {
      //XSLT 문서 지정
      string xslFilePath = @"..\..\ch12\booklist.xsl";
      XslCompiledTransform transformer = new XslCompiledTransform();
      transformer.Load(xslFilePath);
   
      //원본 XML 문서 지정
      string xmlFilePath = @"..\..\ch12\booklist.xml";
      XmlReader xmlReader = XmlReader.Create(xmlFilePath);
     
      //출력 형태 지정
      string resultFilePath = @"C:\Temp\result.htm";
      XmlWriter xmlWriter = XmlWriter.Create(resultFilePath);

      //변환하기
      transformer.Transform(xmlReader, xmlWriter);

      //인터넷 익스플로러 실행
      Process.Start("IExplore.exe", @"C:\Temp\result.htm");
  }
 }
}




Ref. .NET 개발자를 위한 XML p.581

posted by 써니루루 2007. 3. 26. 11:32


XML 문서를 XmlDocument를 이용해 생성한다.
기본으로 유니코드로 저장하기 때문에 한글이 깨지는 문제가 발생하는데, 이 때문에 이 방법 보다는 XmlWriter를 이용해서 XmlWriter.create를 이용해 문서를 생성하는 방법이 더 나은것 같다.

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

using System.Xml;

namespace CH11
{
    class p575_NewDOM
    {
        static void Main()
        {
            XmlDocument xDoc = new XmlDocument();

            XmlElement eBookList = xDoc.CreateElement("booklist");
            xDoc.AppendChild(eBookList);

            XmlElement eBook = xDoc.CreateElement("book");
            XmlElement eTitle = xDoc.CreateElement("title");
            XmlText txtTitle = xDoc.CreateTextNode("XML Programming with .NET");

            eTitle.AppendChild(txtTitle);
            eBook.AppendChild(eTitle);

            eBook.SetAttribute("id", "b10");
            eBook.SetAttribute("kind", "컴퓨터");
            eBookList.AppendChild(eBook);

            xDoc.Save(@"..\..\booklist2.xml");
        }
    }
}

ref. .NET 개발자를 XML p.575

posted by 써니루루 2007. 3. 26. 11:21

이번 예제에서는 Element, Attribute를 DOM을 이용해 삭제하는 방법을 다룬다.

(읽어들일 XML문서는 이전에 포스팅한 글을 참조한다)

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

using System.Xml;
using System.IO;

namespace CH11
{
    class p574_Remove
    {
        static void Main()
        {
            string filePath = @"..\..\booklist.xml";
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(filePath);

            XmlElement eBookList = xDoc.DocumentElement;

            XmlNodeList nlBooks = xDoc.GetElementsByTagName("book");
            foreach (XmlElement eBook in nlBooks)
            {
                eBook.RemoveAttribute("kind");
            }

            XmlElement eLastBook = (XmlElement)eBookList.LastChild;
            eBookList.RemoveChild(eLastBook);

            StringWriter strWriter = new StringWriter();
            xDoc.Save(strWriter);
            Console.WriteLine(strWriter.ToString());
        }
    }
}


Ref. .NET 닷넷 개발자를 위한 XML p.574

posted by 써니루루 2007. 3. 26. 11:14

(불러들이는 XML 파일은 이전에 포스트한 글을 참조한다)

이 글에서는 Xml 문서를 읽어들여 값을 수정하는 예제를 보여준다.
.InnerText와 SetAttribute()를 보도록 하자.

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

using System.Xml;
using System.IO;

namespace CH11
{
    class p568_AddNewContent
    {
        static void Main()
        {
            string filePath = @"..\..\booklist.xml";
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(filePath);

            XmlElement eBookList = xDoc.DocumentElement;

            XmlElement eFirstBook = (XmlElement)eBookList.FirstChild;
            XmlElement eTitle = (XmlElement)eFirstBook.ChildNodes[0];
            eTitle.InnerText = ".NET 개발자를 위한 XML";
            eFirstBook.SetAttribute("kind", "프로그래밍 언어");

            StringWriter strWriter = new StringWriter();
            xDoc.Save(strWriter);

            Console.WriteLine(strWriter.ToString());
        }
    }
}

Ref. .NET 닷넷 개발자를 위한 XML p.570
 

posted by 써니루루 2007. 3. 26. 11:07

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

using System.Xml;
using System.IO;

namespace CH11
{
    class p568_AddNewContent
    {
        static void Main()
        {
            string filePath = @"..\..\booklist.xml";
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(filePath);

            XmlElement eBookList = xDoc.DocumentElement;

            XmlElement eBook = xDoc.CreateElement("book");
            eBook.SetAttribute("id", "b4");
            eBook.SetAttribute("kind", "컴퓨터");
            eBookList.AppendChild(eBook);

            XmlElement eTitle = xDoc.CreateElement("title");
            XmlText txtTitle = xDoc.CreateTextNode("XML Programming .NET");
            eTitle.AppendChild(txtTitle);
            eBook.AppendChild(eTitle);

            XmlElement eAuthor = xDoc.CreateElement("author");
            XmlText txtAuthor = xDoc.CreateTextNode("신민철");
            eAuthor.AppendChild(txtAuthor);
            eBook.AppendChild(eAuthor);

            XmlElement ePublisher = xDoc.CreateElement("publisher");
            XmlText txtPublisher = xDoc.CreateTextNode("프리렉");
            ePublisher.AppendChild(txtPublisher);
            eBook.AppendChild(ePublisher);

            XmlElement ePrice = xDoc.CreateElement("price");
            XmlText txtPrice = xDoc.CreateTextNode("30000");
            ePrice.AppendChild(txtPrice);
            eBook.AppendChild(ePrice);

            StringWriter strWriter = new StringWriter();
            xDoc.Save(strWriter);

            Console.WriteLine(strWriter.ToString());
        }
    }
}



이번 예제는 XML 문서를 XmlDocument로 element들을 생성하고 만들어진 element들을 XmlDocument의 DocumentElement에 AppendChild()를 통해 적용시키는 예제이다.

이러한 방식으로 JavaScript에서도 이용할 수 있다. 어차피 DOM은 여기나 저기나 똑같으니..
posted by 써니루루 2007. 3. 26. 10:57

사용한 XML 문서는 이전 글을 참조하기 바란다.


 

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

using System.Xml.XPath;

namespace CH11
{
    class p566_XPathNavagator
    {
        static void Main()
        {
            string filePath = @"..\..\booklist.xml";
            XPathDocument xPathDoc = new XPathDocument(filePath);
            XPathNavigator xPathNavi = xPathDoc.CreateNavigator();

            // XPath 반복기
            XPathNodeIterator xPathNodeIterator = xPathNavi.Select(
                "//booklist/book/title[../@kind='컴퓨터']");

            while (xPathNodeIterator.MoveNext())
            {
                XPathNavigator naviTitle = xPathNodeIterator.Current;
                Console.WriteLine(naviTitle.Value);
            }
        }
    }
}

posted by 써니루루 2007. 3. 26. 10:50

읽어들일 XML 파일 'booklist.xml'

<?xml version="1.0" encoding="utf-8" ?>
<booklist>
  <book id="b1" kind="컴퓨터">
    <title>기초에서 실무까지 XML</title>
    <author>신민철</author>
    <publisher>프리렉</publisher>
    <price>35000</price>
  </book>
 
  <book id="b2" kind="소설">
    <title>사랑과 전쟁</title>
    <author>이사랑</author>
    <publisher>전쟁문화사</publisher>
    <price>15000</price>
  </book>
 
  <book id="b3" kind="잡지">
    <title>마이크로 소프트</title>
    <author>빌 게이츠</author>
    <publisher>마소문화사</publisher>
    <price>20000</price>
  </book>
 
  <book id="b4" kind="소설">
    <title>액션가면부인 바람났네</title>
    <author>짱구</author>
    <publisher>짱구출판사</publisher>
    <price>12000</price>
  </book>
</booklist>


Xml Node를 XPath를 이용해서 선택하는 예제이다.

예제에서는 kind가 '컴퓨터'인 node를 선택하는 예제코드를 보여준다.



using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

using System.Xml;

namespace CH11
{
    class p564_FindXPath
    {
        static void Main()
        {
            string filePath = @"..\..\booklist.xml";
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(filePath);

            string xPath = "//booklist/book/title[../@kind='컴퓨터']";
            XmlNodeList nodeList = xDoc.SelectNodes(xPath);

            for (int i = 0; i < nodeList.Count; i++)
            {
                string title = nodeList[i].InnerText;
                Console.WriteLine(title);
            }
        }
    }
}


ref. .NET 닷넷 개발자를 위한 XML p.564