-1

I have a problem where I am trying to get the values of my nested elements in my XML file.

-<Modules>
 -<Modules4>
   -<Module>
       <Name>dasd</Name>
       <Code>dasdasdas</Code>
       <Credits>40</Credits>
    </Module>
   -<Module>
     <Name>dasdasd</Name>
     <Code>dasdasd</Code>
     <Credits>40</Credits>
   </Module>
</Modules4>

 <Modules5/>

 <Modules6/>

 </Modules>

This is the XML file, i am trying to create a condition where if the reader finds the "Modules4" Element, then get the nested elements "Name", "Code" and "Credits" if this question has been answered, please direct me to it as I can not find it anywhere.

    using (XmlReader reader = XmlReader.Create("SavedData.xml"))
            while (reader.Read())
        {
                if (reader.IsStartElement())
                {
                    if (reader.Name == "Modules4"){

                    switch (reader.Name)
                    {
                        case "Name":
                            string name = reader.ReadString();
                            Console.WriteLine(name);
                            break;

                        case "Code":
                            string Code = reader.ReadString();
                            Console.WriteLine(Code);
                            break;

                        case "Credits":

                            break;


                    }
                }
            }
        }
    }

This is my code.

Thanks.

3
  • First, are you sure you need an XmlReader (ie your data is ~ 1GB or more) ? Because this is so much easier with XElement. Commented Nov 23, 2017 at 11:58
  • I've never used it, just thought XmlReader was easier. Commented Nov 23, 2017 at 12:05
  • You thought very wrong. Do look at System.Xml.Linq; Commented Nov 23, 2017 at 12:08

2 Answers 2

3

The problem with your current code is that the switch is only reached when reader.Name is "Modules4", never one of its children.

If I were you I'd use the ReadSubtree() method:

...
if (reader.IsStartElement())
{
    if (reader.Name == "Modules4") 
    {
        ReadModules4(reader.ReadSubtree());
    }
}
...

Then you can process the subtree in a separate method:

void ReadModules4(XmlReader reader)
{
    while (reader.Read())
    {
        switch (reader.Name)
        {
            ...
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I like using combination of xml reader and xml linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);

            reader.ReadToFollowing("Modules4");
            XmlReader module4Subtree = reader.ReadSubtree();
            while (!module4Subtree.EOF)
            {
                if (module4Subtree.Name != "Module")
                {
                    module4Subtree.ReadToFollowing("Module");
                }
                if (!module4Subtree.EOF)
                {
                    XElement module = (XElement)XElement.ReadFrom(module4Subtree);
                    Module.modules.Add(new Module() { name = (string)module.Element("Name"), code = (string)module.Element("Code"), credits = (int)module.Element("Credits") });
                }

            }
        }
    }
    public class Module
    {
        public static List<Module> modules = new List<Module>();

        public string name { get; set; }
        public string code { get; set; }
        public int credits { get; set; }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.