none
Load xml file and get certain data RRS feed

  • Question

  • 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="http://www.w3.org/2001/XMLSchema" 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



    • Edited by CY_Chen Friday, September 27, 2019 6:03 AM fix it
    Friday, September 27, 2019 6:02 AM

Answers

  • Hi CY_chen,

    Thanks for the feedback.

    According to your description, I updated my code.

    Code:

       static void Main(string[] args)
            {
                XElement config = XElement.Load(@"D:\test.xml");
                var frame = from element in config.Descendants("FRAME")
                            where element.Attribute("FrameName").Value != null
                            select element;
                var filePath = new List<string>();
                var filePath1 = new List<string>();
                foreach (var item in frame)
                {
                    if(item.Attribute("FrameName").Value== "Frame0")
                    {
                        filePath = (from element in item.Descendants("Base")
                                    where (element.Element("BaseFilePath").Value != null && element.Element("BaseFileName").Value != null)
                                    select (element.Element("BaseFilePath").Value + "/" + element.Element("BaseFileName").Value)).ToList();
                    }
                    if (item.Attribute("FrameName").Value == "Frame1")
                    {
                        filePath1 = (from element in item.Descendants("Base")
                                    where (element.Element("BaseFilePath").Value != null && element.Element("BaseFileName").Value != null)
                                    select (element.Element("BaseFilePath").Value + "/" + element.Element("BaseFileName").Value)).ToList();
                    }
                }
                foreach (var item in filePath)
                {
                    Console.WriteLine(item);
                }
                Console.WriteLine("****************************");
                foreach (var item in filePath1)
                {
                    Console.WriteLine(item);
                } 
    
    
                
                Console.ReadKey();
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CY_Chen Tuesday, October 1, 2019 3:18 AM
    Monday, September 30, 2019 6:58 AM
    Moderator

All replies

  • Hi CY_Chen,

    Thank you for posting here.

    >>But my result all filePath all set in Frame0 How to fix it let it back my config.xml

    I don't understand these two sentences. what do you want to back to the xml? 

    If you describe it more clearly, it will be better for us to solve your problem.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, September 27, 2019 7:54 AM
    Moderator
  • Hi Jack

    Thanks for answering and I need to apologize I didn't describe clearly.

    I want to do with below foreach 

    foreach (var item in frame)
    {                     
        foreach (var image in imagePath)
        {
                filePath.Add(image);
                               
                filePath[ICounts++] = image;                        
        }                    
    }

    I hope

    image =Frame0  enter

    foreach (var image in imagePath){}


    filePath.Add(image) will add
     C:/Image/f.png
     C:/Image/large.png
     C:/Image/medium.png

    Then image =Frame1 

    filePath.Add(image) will add 
    C:/Image/Main.png 
    C:/Image/icon.png

    I hope this result

    But in my case 

    image will add C:/Image/f.png C:/Image/large.png C:/Image/medium.png C:/Image/Main.png

    C:/Image/icon.png


     image =Frame1  is nothing

    How to fix it?

    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);

    It will know which Frame it enter(Frame0 Frame1).

    Thanks

    Friday, September 27, 2019 8:18 AM
  • Right.  Your first query extracts all the frame names.  The second query extracts all of the base paths.  There is no connection between the two queries.

    You need to iterate through the Frame objects, then extract the paths within each one.  You can't just use a query.  I don't know which XML library you're using, but this kind of thing should do it:

    foreach( XElement element in config.Descendents("FRAME") )
    {
        string Name = element.Attrubte("FrameName").Value;
        foreach( Xelement inner in element.Descendents("Base") )
        {
            string path = inner.Element("BaseFilePath").Value + "/" +
                inner.Element("BaseFileName").Value;
            filePath.Add( path );
        }
    }

    But remember, that loop is still going put all of the names into a single list.  I don't know how you are expecting to divide them up by frame.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Friday, September 27, 2019 5:44 PM
  • Hi Tim

    Thanks for answering

    Is possible I can get how many Frame_Element contain in each FRAME   

    Like Frame0 contain 3 Frame_Element.  3

    I want to do those thing because I want to load  xml to grow my tabcontrol structure and each TabItems contain images

    So I need to know each TabItem(FRAME) contain images (Frame_Element)

    Thanks


    • Edited by CY_Chen Monday, September 30, 2019 5:41 AM
    Monday, September 30, 2019 5:40 AM
  • Hi CY_chen,

    Thanks for the feedback.

    According to your description, I updated my code.

    Code:

       static void Main(string[] args)
            {
                XElement config = XElement.Load(@"D:\test.xml");
                var frame = from element in config.Descendants("FRAME")
                            where element.Attribute("FrameName").Value != null
                            select element;
                var filePath = new List<string>();
                var filePath1 = new List<string>();
                foreach (var item in frame)
                {
                    if(item.Attribute("FrameName").Value== "Frame0")
                    {
                        filePath = (from element in item.Descendants("Base")
                                    where (element.Element("BaseFilePath").Value != null && element.Element("BaseFileName").Value != null)
                                    select (element.Element("BaseFilePath").Value + "/" + element.Element("BaseFileName").Value)).ToList();
                    }
                    if (item.Attribute("FrameName").Value == "Frame1")
                    {
                        filePath1 = (from element in item.Descendants("Base")
                                    where (element.Element("BaseFilePath").Value != null && element.Element("BaseFileName").Value != null)
                                    select (element.Element("BaseFilePath").Value + "/" + element.Element("BaseFileName").Value)).ToList();
                    }
                }
                foreach (var item in filePath)
                {
                    Console.WriteLine(item);
                }
                Console.WriteLine("****************************");
                foreach (var item in filePath1)
                {
                    Console.WriteLine(item);
                } 
    
    
                
                Console.ReadKey();
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CY_Chen Tuesday, October 1, 2019 3:18 AM
    Monday, September 30, 2019 6:58 AM
    Moderator