locked
About XML and how to get the double child nodes via LINQ RRS feed

  • Question

  • Hi:

    I have an problem to use LINQ to get the child nodes in xml file. 

    The files is like following:

      <?xml version="1.0" encoding="UTF-8" ?> 
    - <Parent>
    - <Number id="1">
      <PID>19512</PID> 
    - <ChildS>
      <NAME>test1</NAME> 
      <ID>000001</ID> 
      </ChildS>
    - <ChildS>
      <NAME>test2</NAME> 
      <ID>000002</ID> 
      </ChildS>
    - <ChildS>
      <NAME>test3</NAME> 
      <ID>000003</ID> 
      </ChildS>
      </Number>
      </Parent>
    

    I can get basic info from LINQ like following

                    XDocument docx = XDocument.Parse(msg);
                    datalist = (from xml in docx.Elements("Parent").Elements("Number")
                                select new PData
                                {
                                    PID = xml.Element("PID").Value
                                }).ToList(); 

    But the problem is how to get its Childs, since each PID has its different Childs, how to get that 

    via LINQ, thanks.

    Tuesday, January 13, 2015 6:42 AM

Answers

  • What about this scenario:

    public class Parent
        {
            public Number Number;

        }
        public class ChildS
        {
            public string NAME;
            public int ID;
        }
        public class Number
        {
            public string NAME;
            public int id;
            public ChildS ChildS;
            public int PID;
        }

    String xData = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <Parent><Number id=\"1\"><PID>19512</PID> <ChildS><NAME>test1</NAME> <ID>000001</ID> </ChildS><ChildS><NAME>test2</NAME> <ID>000002</ID> </ChildS><ChildS><NAME>test3</NAME> <ID>000003</ID> </ChildS></Number></Parent>";
                XmlSerializer x = new XmlSerializer(typeof(Parent));
                Parent myTest = (Parent)x.Deserialize(new StringReader(xData));
                Console.WriteLine("V1: " + myTest.Number.PID);
                Console.ReadKey();

           

    Fouad Roumieh

    • Proposed as answer by Fouad Roumieh Tuesday, January 13, 2015 5:48 PM
    • Marked as answer by Caillen Sunday, January 25, 2015 3:22 AM
    Tuesday, January 13, 2015 8:05 AM
  • Thanks. And I have figured out the answer, and it works. 

    Just take the next element("Childs"), and using parent to get its parents nodes info,

    then the PData will get all the childs and its parents. 

    Thanks.

    • Marked as answer by Caillen Sunday, January 25, 2015 3:22 AM
    Wednesday, January 14, 2015 2:32 AM

All replies

  • What about this scenario:

    public class Parent
        {
            public Number Number;

        }
        public class ChildS
        {
            public string NAME;
            public int ID;
        }
        public class Number
        {
            public string NAME;
            public int id;
            public ChildS ChildS;
            public int PID;
        }

    String xData = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <Parent><Number id=\"1\"><PID>19512</PID> <ChildS><NAME>test1</NAME> <ID>000001</ID> </ChildS><ChildS><NAME>test2</NAME> <ID>000002</ID> </ChildS><ChildS><NAME>test3</NAME> <ID>000003</ID> </ChildS></Number></Parent>";
                XmlSerializer x = new XmlSerializer(typeof(Parent));
                Parent myTest = (Parent)x.Deserialize(new StringReader(xData));
                Console.WriteLine("V1: " + myTest.Number.PID);
                Console.ReadKey();

           

    Fouad Roumieh

    • Proposed as answer by Fouad Roumieh Tuesday, January 13, 2015 5:48 PM
    • Marked as answer by Caillen Sunday, January 25, 2015 3:22 AM
    Tuesday, January 13, 2015 8:05 AM
  • Thanks. And I have figured out the answer, and it works. 

    Just take the next element("Childs"), and using parent to get its parents nodes info,

    then the PData will get all the childs and its parents. 

    Thanks.

    • Marked as answer by Caillen Sunday, January 25, 2015 3:22 AM
    Wednesday, January 14, 2015 2:32 AM