Click here to Skip to main content
12,399,640 members (52,873 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C# C#3.5
Hi,

i am reading the below xml,
<?xml version="1.0" encoding="utf-8"?>
<ExtractorData xmlns="urn:www.phas.com/services/2.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Level>
    <Name>TestA</Name>
    <ExtractionInfo>
      <Report ReportId="one" GroupId="B">
        <Name>ReportA</Name>
            <OutputLocationFormat>Test/folderA</OutputLocationFormat>
        <OutputFileType>PDF</OutputFileType>
        <ExecutionType>
          <InstallSpecExecuteReportSaveOutput SpecFilePath="Specs\Target.xml">
            <SearchPath>/content/folder[@name='{0}']/folder[@name='CRF Reports']</SearchPath>
          </InstallSpecExecuteReportSaveOutput>
        </ExecutionType>
      </Report>
    </ExtractionInfo>
  </Level>
  <Level>
    <Name>TestB</Name>
    <ExtractionInfo>
      <Report ReportId="one" GroupId="B">
        <Name>ReportB</Name>
        <OutputLocationFormat>Test/folderA</OutputLocationFormat>
        <OutputFileType>xml</OutputFileType>
        <ExecutionType>
          <InstallSpecExecuteReportSaveOutput SpecFilePath="Specs\Site.xml">
            <SearchPath>/content/folder[@name='{0}']/folder[@name='CRF Reports']</SearchPath>
          </InstallSpecExecuteReportSaveOutput>
        </ExecutionType>
      </Report>
    </ExtractionInfo>
  </Level>
  </ExtractorData>
I am trying to get all the Level Nodes using the below code snippet
List<string> lstlevel = new List<string>();
XmlDocument document = new XmlDocument();
document.Load("D:\\Extract.config");
 
foreach (XmlNode node in document.GetElementsByTagName("Level"))
{
    foreach (XmlNode node1 in node.ChildNodes)
    {
        foreach (XmlNode node2 in node.ChildNodes)
        {
          if (node2.Name == "Name")
           {
              lstlevel.Add(node2.InnerText);
           }
        }
    }
} 


lstlevel is having only one value i.e.,TestA but it should contain Both TestA and TestB.
When i see the count of document.GetElementsByTagName("Level").Count is returining as 1 but it should be 2 as i have 2 Level Nodes.
What's wrong in the above code snippet
Posted 20-Feb-13 0:24am
Updated 20-Feb-13 0:45am
v4
Comments
Karthik Harve 20-Feb-13 6:37am
   
[Edit] added pre tags.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

You are using XMLNode to get values, thus only 1.

You need to use XMLNodeList. e.g:
XmlNodeList elemList = doc.GetElementsByTagName("Level");
So, code should first get all the nodeList and then you should work on individual nodes, like:
//Display all the book titles.
    XmlNodeList elemList = doc.GetElementsByTagName("Level");
    for (int i=0; i < elemList.Count; i++)
    {
      // Work with elemList[i]
    }
Documentation related: MSDN: XmlDocument.GetElementsByTagName Method (String)[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,

try like below.
List<string> lstlevel = new List<string>();
XmlDocument document = new XmlDocument();
document.Load("D:\\Extract.config");
 
XmlNode root = document.FirstChild;
 
if(root.HasChildNodes)
{
    // get all nodes with tag name "Level"
    foreach (XmlNode node in root.ChildNodes)
    {
        foreach (XmlNode node1 in node.ChildNodes)
        {
            foreach (XmlNode node2 in node.ChildNodes)
            {
              if (node2.Name == "Name")
               {
                  lstlevel.Add(node2.InnerText);
               }
            }
        }
    } 
}

hope it works.
  Permalink  
v3
Comments
Sandeep Mewara 21-Feb-13 1:09am
   
Why just first child?

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 20 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100