1

I have difficulties to read from that xml! I think XMLDocument would help but i don't know how to get value from each element in childNode!

Especially, the childnode which has any childNode Inside like IdList.

XML format :

 <sdnEntry>

<programList>
  <program>SDNT</program>
</programList>

<idList>
  <id>
    <uid>6028</uid>
    <idType>NIT #</idType>
    <idNumber>900106267-0</idNumber>
    <idCountry>Colombia</idCountry>
  </id>
  <id>
    <uid>6029</uid>
    <idType>N0T #</idType>
    <idNumber>900106267-1</idNumber>
    <idCountry>Colombian</idCountry>      
  </id>
</idList>
</sdnEntry>

Code :

XmlDocument doc = new XmlDocument();
doc.Load(@"D:\SDN1.xml");

XmlElement root = doc.DocumentElement;                

XmlNodeList sdnEntryNodeList = root.GetElementsByTagName("sdnEntry"); 

foreach (XmlNode sdnNode in sdnEntryNodeList)
{
    for (int row = 0; row < sdnEntryNodeList.Count; row++)
    {
        XmlNodeList programListNodeList = sdnNode["programList"].GetElementsByTagName("program");

        foreach (XmlNode programNode in programListNodeList)
        {
            program = programNode.InnerText;
        }

        XmlNodeList idListNodeList = element["idList"].GetElementsByTagName("id");

        foreach (....)
        {

        }
     }       

}

The above code , is it good ? otherwise , i will take all your advice, please comment ....

How to read programList and idList in That complex XML ?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Oudam San
  • 77
  • 1
  • 10

3 Answers3

0

If you want to use XMLDocument , here is a sample for you

var programList = doc.SelectNodes("/sdnEntry/programList/program");
var idList = doc.SelectNodes("/sdnEntry/idList/id");
shenhengbin
  • 4,236
  • 1
  • 24
  • 33
-2

I would simplify you code with the following linq-to-xml queries.

var programs = from program in 
               doc.Root.Element("programList").Descendants("program")
               select program.Value;

var ids = from id in doc.Root.Element("idList").Descendants("id")
          select new
          {
               uid = (string)id.Element("uid"),
               idType = (string)id.Element("idType"),
               idNumber = (string)id.Element("idNumber"),
               idCountry = (string)id.Element("idCountry")
          };
Jason C
  • 142
  • 3
-2

Use linq to xml.

http://msdn.microsoft.com/en-us/library/bb397976.aspx

Sample:

foreach(var node in doc.Elements("idList").Elements("id").Elements("uid")) 
P.K
  • 18,587
  • 11
  • 45
  • 51
  • `Use linq to xml.` , is it support for C# 2.0 , VS2005 – Oudam San Dec 28 '11 at 04:37
  • It is not .net 2.0. But, there are workaround (rather) hacks available to make ling run in .net 2.0. see this link http://stackoverflow.com/questions/2138/linq-on-the-net-2-0-runtime or you can use http://www.albahari.com/nutshell/linqbridge.aspx – P.K Dec 28 '11 at 04:40
  • DO you have another way ? hacks is not good for me . because i developer software for commercial .... – Oudam San Dec 28 '11 at 09:21