[C#] xml 파싱
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 |
현재 Process의 한영 정보 얻기 (0) | 2011.10.10 |
C# 경로(Path) 요소 분리하기 (0) | 2011.09.16 |