블로그 이미지
Every unexpected event is a path to learning for you.

카테고리

분류 전체보기 (2318)N
Unity3D (567)N
Programming (471)
Unreal (4)
Gamebryo (56)
Tip & Tech (183)
협업 (34)
3DS Max (3)
Game (12)
Utility (116)
Etc (92)
Link (31)
Portfolio (19)
Subject (90)
iOS,OSX (38)
Android (13)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (1)
Memories (19)
Interest (37)
Thinking (36)
한글 (26)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (19)
Exercise (3)
나만의 맛집 (2)
냥이 (9)
육아 (7)
Total1,339,011
Today24
Yesterday214
Statistics Graph

달력

« » 2019.11
          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

공지사항

태그목록

[C#] xml 파싱

Programming/C# / 2011.10.25 12:51

xml Product_Key의 Name을 읽어서 ComboBox에 Items를 추가한다.

ComboBox의 List중에서 하나를 선택하면 Key의 값을 TextBox에 나타내 준다.

1. form1 디자인

ComboBox 이름 : cbname

TextBox 이름 : tbkey

2. xml 구조

<YourKey>
<Product_Key Name="1">
<Key>1</Key>
</Product_Key>
<Product_Key Name="2">
<Key>2</Key>
</Product_Key>
<Product_Key Name="3">
<Key>3</Key>
</Product_Key>
<Product_Key Name="4">
<Key>4</Key>
</Product_Key>
</YourKey>

3. form1 코드

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace xmlparsing
{
public partial class Form1 : Form
{
bool select = false;

public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
this.cbname.SelectedIndexChanged += new EventHandler(cbname_SelectedIndexChanged);
}

void Form1_Load(object sender, EventArgs e)
{
getList();
}

void cbname_SelectedIndexChanged(object sender, EventArgs e)
{
select = true;
getList();
}

private void getList()
{
string url = Environment.CurrentDirectory + "\\keys.xml";
try
{
XmlDocument xml = new XmlDocument();
xml.Load(url);
XmlElement keyList = xml.DocumentElement;

XmlNodeList xnList = xml.SelectNodes("/YourKey/Product_Key");

foreach (XmlNode node1 in xnList)
{
if (select)
{
string sKeyName = node1.Attributes["Name"].Value;
string sKey = node1["Key"].InnerText;
if (cbname.SelectedItem.ToString() == node1.Attributes["Name"].Value)
{
tbkey.Text = sKey;
}
}
else
{
string sKeyName = node1.Attributes["Name"].Value;
cbname.Items.Add(sKeyName);
}
}
}
catch (Exception)
{
throw;
}
}

}
}

------------------------------------------------------------------------------------------

자식 노드가 여러개인경우

1. form1 디자인

ComboBox 이름 : cbname

ListBox 이름 : lbname

2. xml 구조

<YourKey>
<Product_Key Name="1">
<Key>1</Key>

<Key>2</Key>
<Key>3</Key>
</Product_Key>
<Product_Key Name="2">
<Key>4</Key>

<Key>5</Key>

<Key>6</Key>
</Product_Key>
</YourKey>

3. form1 코드

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace xmlparsing
{
public partial class Form1 : Form
{
bool select = false;

public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
this.cbname.SelectedIndexChanged += new EventHandler(cbname_SelectedIndexChanged);
}

void Form1_Load(object sender, EventArgs e)
{
getList();
}

void cbname_SelectedIndexChanged(object sender, EventArgs e)
{
lbname.Items.Clear();
select = true;
getList();
}

private void getList()
{
//string url = @"c:\\keys.xml";
string url = Environment.CurrentDirectory + "\\keys.xml";
try
{
XmlDocument xml = new XmlDocument();
xml.Load(url);
XmlElement keyList = xml.DocumentElement;

XmlNodeList xnList = xml.SelectNodes("/YourKey/Product_Key");

foreach (XmlNode node1 in xnList)
{
if (select)
{

if (cbname.SelectedItem.ToString() == node1.Attributes["Name"].Value)
{

XmlNodeList nodeChilds = node1.ChildNodes;

for (int i = 0; i < nodeChilds.Count; i++)
{
XmlElement eChild = (XmlElement)nodeChilds[i];
string sKey = eChild.InnerText;
lbname.Items.Add(sKey);
}
}
}
else
{
string sKeyName = node1.Attributes["Name"].Value;
cbname.Items.Add(sKeyName);
}
}
}
catch (Exception)
{
throw;
}
}

}
}

[출처] [C#] xml 파싱|작성자 한심이79

'Programming > C#' 카테고리의 다른 글

[링크] WinForm Study  (0) 2011.10.25
XML 특정 하위노드 및 자식노드를 좀 간단히 읽어보기  (0) 2011.10.25
[C#] xml 파싱  (0) 2011.10.25
C#으로 xml 처리하기  (0) 2011.10.25
현재 Process의 한영 정보 얻기  (0) 2011.10.10
C# 경로(Path) 요소 분리하기  (0) 2011.09.16
Posted by blueasa
TAG

댓글을 달아 주세요