Load xml file and get certain data

  • Thread starter Thread starter CY_Chen
  • Start date Start date
C

CY_Chen

Guest
I try to get my config.xml file and get its all data

Here is my config.xml content look like

<CONFIG xmlns:xdi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="XML Schema" FrameWidth="400" FrameHeight="200">
<Frames>
<FRAME FrameName="Frame0">
<Frame_Elements>
<Frame_Element><!--Frame_Element[0]-->
<Basic><!--Basic[0]-->
<Id>0</Id>
<Uid>0</Uid>
</Basic>
<Base><!--Base[0]-->
<BaseFilePath>C:/Image</BaseFilePath>
<BaseFileName>f.png</BaseFileName>
</Base>
</Frame_Element>
<Frame_Element><!--Frame_Element[1]-->
<Basic><!--Basic[1]-->
<Id>1</Id>
<Uid>0</Uid>
</Basic>
<Base><!--Base[1]-->
<BaseFilePath>C:/Image</BaseFilePath>
<BaseFileName>large.png</BaseFileName>
</Base>
</Frame_Element>
<Frame_Element><!--Frame_Element[2]-->
<Basic><!--Basic[2]-->
<Id>2</Id>
<Uid>0</Uid>
</Basic>
<Base><!--Base[2]-->
<BaseFilePath>C:/Image</BaseFilePath>
<BaseFileName>medium.png</BaseFileName>
</Base>
</Frame_Element>
<Frame_Element><!--Frame_Element[0]-->
<Basic><!--Basic[3]-->
<Id>3</Id>
<Uid>0</Uid>
</Basic>
<Base><!--Base[3]-->
<BaseFilePath>C:/Image</BaseFilePath>
<BaseFileName>small.png</BaseFileName>
</Base>
</Frame_Element>
</Frame_Elements>
</FRAME>
<FRAME FrameName="Frame1">
<Frame_Elements>
<Frame_Element><!--Frame_Element[4]-->
<Basic><!--Basic[4]-->
<Id>4</Id>
<Uid>0</Uid>
</Basic>
<Base><!--Base[4]-->
<BaseFilePath>C:/Image</BaseFilePath>
<BaseFileName>Main.png</BaseFileName>
</Base>
</Frame_Element>
<Frame_Element><!--Frame_Element[5]-->
<Basic><!--Basic[5]-->
<Id>5</Id>
<Uid>0</Uid>
</Basic>
<Base><!--Base[5]-->
<BaseFilePath>C:/Image</BaseFilePath>
<BaseFileName>icon.png</BaseFileName>
</Base>
</Frame_Element>
</Frame_Elements>
</FRAME>
</CONFIG>

In my xml structure it contain two FRAME FrameName : Frame0 Frame1

Each FRAME FrameName contain lots of Frame_Element

So I use nest foreach to store it

Here is my code


var frame = from element in config.Descendants("FRAME")
where element.Attribute("FrameName").Value != null
select element.Attribute("FrameName").Value;

var imagePath = from element in config.Descendants("Base")
where (element.Element("BaseFilePath").Value != null && element.Element("BaseFileName").Value !=null)
select (element.Element("BaseFilePath").Value+"/"+ element.Element("BaseFileName").Value);




nest foreach code

foreach (var item in frame)
{

foreach (var image in imagePath)
{

filePath.Add(image);

filePath[ICounts++] = image;

}


}

But my result all filePath all set in Frame0

How to fix it let it back my config.xml

Thanks

Continue reading...
 
Back
Top