DJ메탈짱™의 Free Style

[C#] LINQ, XML파싱, xml to LINQ, Properties.Resources,foreach 본문

일(job)/MS(Microsoft)

[C#] LINQ, XML파싱, xml to LINQ, Properties.Resources,foreach

뽀&쏭 2015. 12. 31. 13:11

 

 

xml파싱하여 XML Document 설정하는

(방법1) - \bin\Debug\ xml파일을 copy하여 설정하는 방법.

XDocument xmlDoc = XDocument.Load(@"samplexml.xml");

 

(방법2) – Resource xml파일을 추가한 방법.

XDocument xmlDoc = XDocument.Parse(Properties.Resources.samplexml);

 

var source = from aa in xmlDoc.Descendants("root")// element 기준점

            select new

            {

                 cabinClass = aa.Descendants("aaa").ElementAt(0).Value,// root하위에 aaa element value

                 cabinCapacity = aa.Descendants("bbb").ElementAt(0).Value,

            };

 

foreach (var cabin in source)

        keTextBox2.Text +="cabinClass: " + cabin.cabinClass + "\r\n";

        keTextBox2.Text+= "cabinCapacity: " + cabin.cabinCapacity + "\r\n";

}

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

 

<simple sample 1>

XDocument doc = XDocument.Parse(Properties.Resources.SeatMapReply_Class_F);

var config = from cabin in doc.Descendants("fittedConfiguration")

select new

{

cabinClass = cabin.Element("cabinClass").Value,

cabinCapacity =cabin.Element("cabinCapacity").Value,                                     

};

 

this.keTextBox2.Text = "";

foreach (var cabin in config)

{

keTextBox2.Text +="cabinClass: " + cabin.cabinClass+ "\n";

        keTextBox2.Text+= "cabinCapacity: " +cabin.cabinCapacity + "\n";

}


<simple sample 2>

XDocument xmlDoc = XDocument.Load("test.xml");

var persons = from person in xmlDoc.Descendants("Person")

select new

             {

                Name = person.Element("Name").Value,

                City = person.Element("City").Value,

                Age = person.Element("Age").Value,

             };

 

this.keTextBox2.Text = "";

foreach (var person in persons)

{

keTextBox2.Text =keTextBox2.Text + "Name: " +person.Name + "\n";

        keTextBox2.Text= keTextBox2.Text + "City: " +person.City + "\n";

        keTextBox2.Text= keTextBox2.Text + "Age: " +person.Age + "\n\n";

}