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

카테고리

분류 전체보기 (2794)
Unity3D (852)
Programming (478)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (185)
협업 (11)
3DS Max (3)
Game (12)
Utility (68)
Etc (98)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (55)
Android (14)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (18)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday

[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

반응형
Posted by blueasa
, |