(Linq) linq to xml

C#/Winform 2017. 4. 14. 11:53
728x90
728x170

// 간단한 xml 예제

var root = new XElement("root",
                        new XAttribute("attr", "value"),
                        new XElement("child", "test"));

 

결과

<root attr="value">
  <child>test</child>
</root>

 

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

// List 에 담긴 내용을 linq 를 이용해 쉽게 xml 형태로 변환이 가능합니다.

People p1 = new People() { name = "A", age = 4 };
People p2 = new People() { name = "B", age = 5 };
People p3 = new People() { name = "C", age = 6 };
People p4 = new People() { name = "E", age = 7 };
People p5 = new People() { name = "F", age = 8 };
List<People> pList = new List<People>() { p1, p2, p3, p4, p5 };

 

var peoples = new XElement("Peoples",
    from p in pList
    select new XElement("People",
                                   new XAttribute("name", p.name),
                                   new XAttribute("age", p.age)));

 

결과

<Peoples>
  <People name="A" age="4" />
  <People name="B" age="5" />
  <People name="C" age="6" />
  <People name="E" age="7" />
  <People name="F" age="8" />
</Peoples>

 

728x90
그리드형

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

(Linq) linq - join into  (0) 2017.04.14
(Linq) Linq to DataSet  (0) 2017.04.14
(Linq) linq T Type Join result T Type  (0) 2017.04.14
(WPF) wpf 관련 리소스 정리  (0) 2017.04.13
(WPF) 챨스패졸드 책 정리  (0) 2017.04.13
Posted by kjun.kr
,