Question

I am having a xml file with nested elements. I need to load the xml file and save the values of a particular child elements to the sql database.

xml File :

    <section id="A00-A09">
          <desc>Intestinal infectious diseases (A00-A09)</desc>
          <diag>
            <name>A00</name>
            <desc>Cholera</desc>
            <diag>
              <name>A00.0</name>
              <desc>Cholera due to Vibrio cholerae 01, biovar cholerae</desc>
              <inclusionTerm>
                <note>Classical cholera</note>
              </inclusionTerm>
            </diag>
            <diag>
              <name>A00.1</name>
              <desc>Cholera due to Vibrio cholerae 01, biovar eltor</desc>
              <inclusionTerm>
                <note>Cholera eltor</note>
              </inclusionTerm>
            </diag>
            <diag>
              <name>A00.9</name>
              <desc>Cholera, unspecified</desc>
            </diag>
          </diag>
    </section>
     <section id="A15-A19">
          <desc>Tuberculosis (A15-A19)</desc>
          <includes>
            <note>infections due to Mycobacterium tuberculosis and Mycobacterium bovis</note>
          </includes>
        <diag>
            <name>A15</name>
            <desc>Respiratory tuberculosis</desc>
            <diag>
              <name>A15.0</name>
              <desc>Tuberculosis of lung</desc>
              <inclusionTerm>
                <note>Tuberculous bronchiectasis</note>
                <note>Tuberculous fibrosis of lung</note>
                <note>Tuberculous pneumonia</note>
                <note>Tuberculous pneumothorax</note>
              </inclusionTerm>
            </diag>
            <diag>
              <name>A15.4</name>
              <desc>Tuberculosis of intrathoracic lymph nodes</desc>
              <inclusionTerm>
                <note>Tuberculosis of hilar lymph nodes</note>
                <note>Tuberculosis of mediastinal lymph nodes</note>
                <note>Tuberculosis of tracheobronchial lymph nodes</note>
              </inclusionTerm>
              <excludes1>
                <note>tuberculosis s`enter code here`pecified as primary (A15.7)</note>
              </excludes1>
            </diag>
    </diag>
    </section>

I have loaded the xml but not able to get the values from the child elements since it is having same tag name. Do any one can help me to solve this issue

Was it helpful?

Solution

Following console application example demonstrates a way to get <desc> element that is direct child of <section> element, also to get <name> and <desc> under each <diag> element. I'm using XDocument from System.Xml.Linq. But not using linq query syntax as it isn't necessary in this case and you seems doesn't like it :

//load xml string
var doc = XDocument.Parse(xml);
//or use XDocument.Load("path_to_xml_file.xml"); to load from file

//get all <section> element
var sections = doc.Root.Elements("section");
//get <desc> and all <diag> under each <section>
foreach (var section in sections)
{
    var desc = (string)section.Element("desc");
    Console.WriteLine(String.Format("new Section. Desc: {0}", desc));
    var diags = section.Descendants("diag");
    foreach (var diag in diags)
    {
        var name = (string)diag.Element("name");
        var desc2 = (string)diag.Element("desc");
        Console.WriteLine(String.Format("name: {0}, desc: {1}", name, desc2));
    }
}

notice that Descendants() is used to get any child of current element and Element() used to get only direct child of current element.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top