블로그 이미지
Kanais
Researcher & Developer 퍼즐을 완성하려면 퍼즐 조각들을 하나 둘씩 맞춰나가야 한다. 인생의 퍼즐 조각들을 하나 둘씩 맞춰나가다 보면 인생이란 퍼즐도 완성되는 날이 오려나...?

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

Notice

05-01 06:35

Recent Post

Recent Comment

Recent Trackback

Archive

2015. 6. 5. 15:40 Programming/.NET


작성날짜 : 2011-04-24



참고 서적 : About ADO.NET


AttributeCount 
속성

파생 클래스에서 재정의되면 현재 노드에 포함된 특성(Attribute) 수를 가져온다특성 수란 Attribute의 수를 말한다예를 틀면 <font size=”12px” color=”green”>이라는 XML 태그가 있다고 하면 특성이 size color 두 가지이기 때문에 AttributeCount 2가 된다기본 문형을 보면 다음과 같다.

public abstract int AttributeCount

        {

            get;

        }

 

속성 값은 현재 노드에 포함된 특성의 수이다위의 기본형을 볼 때 특성의 개수가 int 형의 숫자로 반환되는 것을 볼 수 있다. AttributeCount 속성은 Element, DocumentType  XmlDeclaration 노드에만 관련이 있고 다른 노드와는 관련이 없다. AttributeCount 속성에 대해 알아 보기 위해 다음과 같은 xml 문서를 작성한다.

 



XML 
문서를 찾아서 불러오면 다음과 같은 결과를 볼 수 있다.

 


위의 xml 문서를 살펴보자먼저 선언문을 볼 수 있는데 <?xml version=”1.0” encoding=”euc-kr”>에서 version encoding이 특성(Attribute)에 해당되는 부분이다. <xml>이라는 엘리먼트(Element)(엘리먼트는 요소라고도 한다) version encoding이라는 두 개의 특성 값을 가지고 있으므로 AttributeCount 2가 된다또한 ‘About ADO.NET’이라는 값을 가지고 있는 첫 번째 <Book> 엘리먼트도 Color, Count 두 개의 특성 값을 가지고 있다그래서 이 경우도 AttributeCount 2가 된다.

AttributeCount 속성을 보여주는 코드를 작성해 보자

protected void Page_Load(object sender, EventArgs e)

{

    string filename = "C:/xml/Book.xml";

    XmlTextReader xtReader = null;

    try

    {

        xtReader = new XmlTextReader(filename);

        xtReader.WhitespaceHandling = WhitespaceHandling.None;

while (xtReader.Read())

        {

            if (xtReader.HasAttributes)

            {

Response.Write("========================<br>");

                Response.Write(" " + xtReader.Name + "의 특성<br>");

Response.Write("========================<br>");

                for (int i = 0; i < xtReader.AttributeCount; i++)

                {

                    xtReader.MoveToAttribute(i);

                    Response.Write(xtReader.Name + " : " + xtReader.Value);

                    Response.Write("<br>");

                }

                xtReader.MoveToElement();

          }

        }

    }

    catch (Exception ex)

    {

        Response.Write("오류가 발생하였습니다.<br>");

        Response.Write("오류내용" + ex.ToString());

    }

    finally

    {

      if (xtReader != null)

        {

            xtReader.Close();

        }

    }

}

 

위의 코드를 실행하면 속성의 개수만큼 반복문이 실행괴면서 특성의 이름과 값이 출력되는 것을 볼 수 있다.

 

위의 결과에서 보는 것처럼 각각의 엘리먼트에 특성이 있는 경우 그 특성의 이름과 개수가 출력되는 것을 볼 수 있다.



posted by Kanais