Tarikh
Tarikh

Reputation: 29

how to read nested node from XML file

This is an example of what my XML file typically looks like. It can contain any amount of assignments and any amount of modules and can contain 3 levels. (I haven't included the other 2 levels for now).

<course name="engineering">
    <level4>
        <module name="electric" credit="22">
            <assignment name="wer" marks="22" weight="50">
            </assignment>
            <assignment name="asd" marks="50" weight="50">
            </assignment>
        </module>
    </level4>
</course>

This is what I have so far

while (tr.Read())
{
    tr.MoveToElement();
    if (tr.Name == "Course") {
        courseXML.Name = tr.GetAttribute("name");
    }
    if (tr.Name == "Level4") {

    }
}

I am reading the XML file but I stumbled upon a problem. How do I get to module elements and the assignment elements and how would I be able to iterate through them because my XML file can contain any amount of modules and assignment.

Upvotes: 1

Views: 719

Answers (1)

Jeroen Doppenberg
Jeroen Doppenberg

Reputation: 1558

If the xml is not to big you can do it like this:

    public static List<XmlObject> FindTagsWithChildTags(string xml, string tag,string childTag,string attribute)
    {
        XDocument xdoc = XDocument.Load(xml);
        if (xdoc.Descendants(tag).Any())
        {
            var lv1s = (from lv1 in xdoc.Descendants(tag)
                        select new XmlObject
                        {
                            Element = "",
                            Value = lv1.Attribute(attribute).Value.ToLower(),
                            Field = attribute ,
                            XmlObjects = (from lv2 in lv1.Descendants(childTag)
                                         select new XmlObject
                                         {
                                             Element="",
                                             Field=lv1.FirstAttribute.Name.LocalName,
                                             Value = lv1.FirstAttribute.Value.ToLower()
                                         }).ToList()
                        }).ToList();

            return lv1s;
        }
        return new List<XmlObject>();
    }

if you have a very big xml you can read it like this

    List<Info> infos = new List<Info>();
    XmlReaderSettings settings = new XmlReaderSettings();
    settings.ValidationType = ValidationType.Schema;
   // settings.Schemas.Add("urn:empl-hire", "hireDate.xsd");
    using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(xmlPath, settings))
    {            
        reader.MoveToContent();
      while(reader.ReadToFollowing("ServiceInformation"))
      {
          string serviceId = reader.GetAttribute("serviceId");
          string serviceUrl = "";
          if (reader.ReadToDescendant("ServiceURL"))
          {
              serviceUrl = reader.ReadElementContentAsString();
          }
          Info info = new Info();
          info.ID = serviceId;
          info.Value1 = serviceUrl;
          infos.Add(info);
      }

    }
    return infos;

These are examples i used i hope they help you!

Upvotes: 1

Related Questions