Click here to Skip to main content
15,436,734 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I am having an XML in the following format. Which i need to get data of first, last, id values from the node name. Can you help how i can get this ?


HTML
<sectionIndex>
     <sectionRef first="A00" last="A09" id="A00-A09">
        Intestinal infectious diseases
     </sectionRef>
     <sectionRef first="A15" last="A19" id="A15-A19">
        Tuberculosis
     </sectionRef>
     <sectionRef first="A20" last="A28" id="A20-A28">
        Certain zoonotic bacterial diseases
     </sectionRef>
     <sectionRef first="A30" last="A49" id="A30-A49">
        Other bacterial diseases
     </sectionRef>
     <sectionRef first="A50" last="A64" id="A50-A64">
        Infections with a predominantly sexual mode of transmission
     </sectionRef>
     <sectionRef first="A65" last="A69" id="A65-A69">
        Other spirochetal diseases
     </sectionRef>
</sectionIndex>


Thanks.

What I have tried:

System.Xml.XmlNodeList Chapters = doc.SelectNodes("ICD10CM.tabular/chapter");


System.Xml.XmlNodeList Sections = doc.SelectNodes("ICD10CM.tabular/chapter/sectionIndex");

foreach (System.Xml.XmlNode CC in Sections)
{
string[] sections = new string[5];
sections[0] = CC.SelectSingleNode("sectionRef").InnerText;
}
Posted
Updated 28-Jun-16 22:02pm
Comments
Bernhard Hiller 29-Jun-16 4:48am    
Isn't there an xsd available for that type? Then you could generate classes with the xsd.exe tool.

1 solution

Move the definition of sections outside the loop, and use an index to access it. Or better, use a List<string> so you don't have to work with a fixed number of elements:
C#
System.Xml.XmlNodeList Chapters = doc.SelectNodes("ICD10CM.tabular/chapter");
System.Xml.XmlNodeList Sections = doc.SelectNodes("ICD10CM.tabular/chapter/sectionIndex");
List<string> sections = new List<string>();
foreach (System.Xml.XmlNode CC in Sections)
    {
    sections.Add(CC.SelectSingleNode("sectionRef").InnerText);
    }
 
Share this answer
 
v2
Comments
Charles Shob 29-Jun-16 4:32am     CRLF
Hi OriginalGriff, Thanks for the code. With this i am getting only Sectionindex values. But, i need inner values of SectionRef such as "first", "last", "id" also.
OriginalGriff 29-Jun-16 5:02am     CRLF
Then you need to access them! Inside your loop: XmlNode node = CC.SelectSingleNode("sectionRef"); You can then access the info as: node.InnerText Which gives you what you get now. node.Attributes["first"].InnerText node.Attributes["last"].InnerText node.Attributes["id"].InnerText Which give you the attributes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900