locked
XML file with multiple nodes RRS feed

  • Question

  • User-593639757 posted
    How do I read the following XML file using linq or some other methods? I need to be able to read the node values in <Instructor> and create a two dimensional array. <School xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <SchoolType> <School></School> <Dept></Dept> <Level></Level> <Degree>Masters</Degree> <Other></Other> <SchoolType></SchoolType> </SchoolType> <RegDate>01-02-2013</RegDate> <Location>/Location> <CourseType> <CourseType-A></CourseType-A> <CourseType-B>Science</CourseType-B> <CourseType-C>Art</CourseType-C> <CourseType-D></CourseType-D> </CourseType> <CourseSelection> <Subject> <Instructor> <FName>Brad</FName> <LName>Pitt</LName> <MName>na</MName> </Instructor> <Instructor> <FName>Robert</FName> <LName>Downey</LName> <MName>x</MName> </Instructor> <InstructorCount>2</InstructorCount> <InstructorMessage></InstructorMessage> </Subject> </CourseSelection> <Undecided>true</Undecided> <StartDate>02-03-2013</StartDate> <ApprovedBy>Tom Cruise</ApprovedBy> </School >
    Friday, August 23, 2013 9:33 AM

Answers

  • User-760709272 posted

    Load it into an XmlDocument and access the nodes you need via the SelectNodes / SelectSingleNode methods using xpath syntax.  Google for plenty of examples on how to do this.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 23, 2013 10:48 AM
  • User-933407369 posted

    hi  mikeganem,

    From what I understand you want to to read the node values in xml file.

    I would suggest you to try the following workground.

    Read XDocument: 

    <?xml version="1.0" encoding="utf-8" ?>
    
    <foo>
    
        <bar>Test 1</bar>
    
        <baz>
    
            <bar>Test 2</bar>
    
        </baz>
    
        <bar>Test 3</bar>
    
    </foo>
    
    Code:
    
    XDocument doc = XDocument.Load("input.xml");
    
    XElement root = doc.Root;
    
    foreach (XElement e in root.Elements("bar"))
    
    {
    
       Console.WriteLine("Elements : " + e.Value);
    
    }
    
     
    
    foreach (XElement e in root.Descendants("bar"))
    
    {
    
        Console.WriteLine("Descendants : " + e.Value);
    
    }

    Result:

    Elements : Test 1

    Elements : Test 3

    Descendants : Test 1

    Descendants : Test 2

    Descendants : Test 3

    LINQ DEMO:

    XElement root = XElement.Parse(@"<Root>
    
      <Paragraph>
    
        <Text>This is the start of</Text>
    
      </Paragraph>
    
      <Comment>
    
        <Text>This comment is not part of the paragraph text.</Text>
    
      </Comment>
    
      <Paragraph>
    
        <Annotation Emphasis='true'>
    
          <Text> a sentence.</Text>
    
        </Annotation>
    
      </Paragraph>
    
      <Paragraph>
    
        <Text>  This is a second sentence.</Text>
    
      </Paragraph>
    
    </Root>");
    
    // LINQ to XML query  Merge two records
    
                string str1 =
    
                    root
    
                    .Elements("Paragraph")
    
                    .Descendants("Text")
    
                    .Select(s => s.Value)
    
                    .Aggregate(
    
     
    
                        new StringBuilder(),
    
                        (s, i) => s.Append(i),
    
                        s => s.ToString()
    
                    );
    
                // LINQ to XML query   two records list
    
                var str12 =
    
                   root.Elements("Paragraph").Descendants("Text").ToList();

    If you need more assistance, please let me know.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 28, 2013 5:58 AM

All replies

  • User-760709272 posted

    Load it into an XmlDocument and access the nodes you need via the SelectNodes / SelectSingleNode methods using xpath syntax.  Google for plenty of examples on how to do this.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 23, 2013 10:48 AM
  • User-933407369 posted

    hi  mikeganem,

    From what I understand you want to to read the node values in xml file.

    I would suggest you to try the following workground.

    Read XDocument: 

    <?xml version="1.0" encoding="utf-8" ?>
    
    <foo>
    
        <bar>Test 1</bar>
    
        <baz>
    
            <bar>Test 2</bar>
    
        </baz>
    
        <bar>Test 3</bar>
    
    </foo>
    
    Code:
    
    XDocument doc = XDocument.Load("input.xml");
    
    XElement root = doc.Root;
    
    foreach (XElement e in root.Elements("bar"))
    
    {
    
       Console.WriteLine("Elements : " + e.Value);
    
    }
    
     
    
    foreach (XElement e in root.Descendants("bar"))
    
    {
    
        Console.WriteLine("Descendants : " + e.Value);
    
    }

    Result:

    Elements : Test 1

    Elements : Test 3

    Descendants : Test 1

    Descendants : Test 2

    Descendants : Test 3

    LINQ DEMO:

    XElement root = XElement.Parse(@"<Root>
    
      <Paragraph>
    
        <Text>This is the start of</Text>
    
      </Paragraph>
    
      <Comment>
    
        <Text>This comment is not part of the paragraph text.</Text>
    
      </Comment>
    
      <Paragraph>
    
        <Annotation Emphasis='true'>
    
          <Text> a sentence.</Text>
    
        </Annotation>
    
      </Paragraph>
    
      <Paragraph>
    
        <Text>  This is a second sentence.</Text>
    
      </Paragraph>
    
    </Root>");
    
    // LINQ to XML query  Merge two records
    
                string str1 =
    
                    root
    
                    .Elements("Paragraph")
    
                    .Descendants("Text")
    
                    .Select(s => s.Value)
    
                    .Aggregate(
    
     
    
                        new StringBuilder(),
    
                        (s, i) => s.Append(i),
    
                        s => s.ToString()
    
                    );
    
                // LINQ to XML query   two records list
    
                var str12 =
    
                   root.Elements("Paragraph").Descendants("Text").ToList();

    If you need more assistance, please let me know.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 28, 2013 5:58 AM