본문 바로가기

   
Programming/XML

윈폼 XML연결후 피드추가, 삭제 예제

반응형






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.IO;

using System.Xml;

 

namespace WinFormEx

{

       public partial class Ex02 : Form

       {

             public Ex02()

             {

                    InitializeComponent();

             }

 

             private void 블로그관리ToolStripMenuItem_Click(object sender, EventArgs e)

             {

                    //팝업

                    Ex02_url sub = new Ex02_url();

                    sub.Owner = this;

                    sub.Show();

             }

 

             private void Ex02_Load(object sender, EventArgs e)

             {

                    //메인 로드

                    //1. blog.dat 읽기 -> Text

                    //2. 등록된 피드의 제목 읽기 -> XML

                    //3. 제목을 콤보박스 추가

                    ShowList();

             }

 

             public void ShowList()

             {

                    comboBox1.Items.Clear();

 

                    StreamReader reader = new StreamReader("blog.dat");

                    string txt = "";

 

                    //1.

                    while ((txt = reader.ReadLine()) != null)

                    {

                           //2.

                           XmlDocument doc = new XmlDocument();

                           doc.Load(txt);

 

                           string title = doc.DocumentElement.FirstChild.FirstChild.InnerText;

 

                           comboBox1.Items.Add(new BlogItem { Title = title, URL = txt });

                           comboBox1.DisplayMember = "Title";

                    }

 

                    reader.Close();

             }

 

             private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

             {

                    listView1.Items.Clear();//초기화

 

                    //블로그 선택 -> URL

                    BlogItem item = comboBox1.SelectedItem as BlogItem;

 

                    XmlDocument doc = new XmlDocument();

                    doc.Load(item.URL);

 

                    //포스트 정보 -> ListView에 출력

                    //카테고리, 타이틀, 날짜 -> <item>의 자식

                    XmlNodeList list = doc.SelectNodes("//item");

 

                    foreach (XmlElement post in list)

                    {

                           //post 하나당 제목,카테고리,날짜 가져와서 리스트뷰 아이템으로 추가하기

                           string category = post.ChildNodes[1].InnerText;

                           string title = post.ChildNodes[2].InnerText;

                           string date = post.ChildNodes[6].InnerText;

                           string link = post.ChildNodes[3].InnerText;

 

                           ListViewItem lItem = new ListViewItem(category);

                           lItem.SubItems.Add(title);

 

                           //Tue, 01 May 2012 16:53:28 +0900

                           DateTime regDate = DateTime.Parse(date);

                           lItem.SubItems.Add(regDate.ToString());

 

                           //guid or link

                           lItem.Tag = link;

 

                           listView1.Items.Add(lItem);

                    }

 

 

             }

 

             private void listView1_SelectedIndexChanged(object sender, EventArgs e)

             {

                    //포스트 중 1개를 선택 -> 선택된 아이템 -> Tag -> 브라우징

                    if (listView1.SelectedItems.Count > 0)

                    {

                           string link = listView1.SelectedItems[0].Tag.ToString();

 

                           if (브라우저로보기ToolStripMenuItem.Checked)

                           {

                                 webBrowser1.Navigate(link);

                           }

                           else

                           {

                                 //문서로드 -> item중에.. 우리가 원하는 item찾기(현재 보고있는 글) -> ? -> 그안에 있는 글내용(description) 엘리먼트의 값을 얻기위해서..

                                 XmlDocument doc = new XmlDocument();

                                 BlogItem item = comboBox1.SelectedItem as BlogItem;

                                 doc.Load(item.URL);

 

                                 XmlElement desc = doc.SelectSingleNode("//item[link='" + link + "']/description") as XmlElement;

 

                                 richTextBox1.Text = desc.InnerText;

                           }

                    }

             }

 

             private void 브라우저로보기ToolStripMenuItem_Click(object sender, EventArgs e)

             {

                    브라우저로보기ToolStripMenuItem.Checked = true;

                    rSS보기ToolStripMenuItem.Checked = false;

 

                    webBrowser1.Visible = true;

                    richTextBox1.Visible = false;

             }

 

             private void rSS보기ToolStripMenuItem_Click(object sender, EventArgs e)

             {

                    브라우저로보기ToolStripMenuItem.Checked = false;

                    rSS보기ToolStripMenuItem.Checked = true;

 

                    webBrowser1.Visible = false;

                    richTextBox1.Visible = true;

             }

       }

}

 

 



url

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.IO;

using System.Xml;

 

namespace WinFormEx

{

       public partial class Ex02_url : Form

       {

             private FileInfo file;

 

             public Ex02_url()

             {

                    InitializeComponent();

             }

 

             private void Ex02_url_Load(object sender, EventArgs e)

             {

                    file = new FileInfo("blog.dat");

 

                    ShowList();

             }

 

             private void ShowList()

             {

                    //초기화

                    listBox1.Items.Clear();

 

                    //blog.dat에 있는 주소를 로드 -> ListBox 출력

                    StreamReader reader = file.OpenText();

 

                    string txt = "";

                    while ((txt = reader.ReadLine()) != null)

                    {

                           //listBox1.Items.Add(txt);//URL X

                           XmlDocument doc = new XmlDocument();

                           doc.Load(txt);

 

                           //블로그 제목

                           string title = doc.DocumentElement.FirstChild.FirstChild.InnerText;

 

                           BlogItem item = new BlogItem();

                           item.Title = title;

                           item.URL = txt;

 

                           listBox1.Items.Add(item);

                           listBox1.DisplayMember = "Title";

                    }

 

                    reader.Close();

             }

 

             private void button1_Click(object sender, EventArgs e)

             {

                    //추가하기

                    //StreamWriter writer = null;

 

                    //if (file.Exists)

                    //{//있을때.. -> 추가모드 열기(Append)

                    //    writer = file.AppendText();

                    //}

                    //else

                    //{//없을때.. -> 생성모드 열기(Create)

                    //}

 

                    //이미 등록된 주소를 필터링!!

                    foreach (BlogItem item in listBox1.Items)

                    {

                           if (item.URL == textBox1.Text)

                           {

                                 MessageBox.Show("이미 등록된 피드입니다!!");

                                 return;

                           }

                    }

 

 

                    StreamWriter writer = file.CreateText();

 

                    //1. 리스트박스의 블로그 주소를 기록

                    foreach (BlogItem item in listBox1.Items)

                    {

                           writer.WriteLine(item.URL);

                    }

 

                    //2. 새로운 텍스트박스의 주소를 기록

                    writer.WriteLine(textBox1.Text);

                    writer.Close();

 

                    //3. 리스트박스 갱신

                    ShowList();

                    textBox1.Text = "";

                    textBox1.Focus();

 

 

                    //4. 부모의 콤보박스 갱신

                    ((Ex02)this.Owner).ShowList();

             }

 

             private void button2_Click(object sender, EventArgs e)

             {

                    //삭제 -> 선택된 피드

                    //1. 리스트박스에서 항목 삭제

                    //2. 리스트박스에 등록된 주소만을 blog.dat에 다시 기록

                    if (listBox1.SelectedIndex == -1)

                           return;

 

                    StreamWriter writer = file.CreateText();

 

                    listBox1.Items.RemoveAt(listBox1.SelectedIndex);//1.

 

                    //2.

                    foreach (BlogItem item in listBox1.Items)

                    {

                           writer.WriteLine(item.URL);

                    }

 

                    writer.Close();

                    ShowList();

 

                    //부모의 콤보박스 갱신

                    ((Ex02)this.Owner).ShowList();

             }

 

             private void button3_Click(object sender, EventArgs e)

             {

                    this.Close();

             }

       }

}

 

 


BlogItem

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace WinFormEx

{

       class BlogItem

       {

             public string Title { get; set; }

             public string URL { get; set; }

       }

}

 

 


반응형