Using combination of 2 XmlTextReaders fails, skips a node while reading xml.

Hi,

I have an XML file called temp.xml

 

< xml version="1.0" encoding="UTF-8" >

            <docmain>

                        <Chapter>

                                    <Main Id="M ONE">

                                                <Para Id="P ONE"></Para>      

                                                <Para Id="P TWO"></Para>      

                                                <Para Id="P THREE"></Para>

                                    </Main>

                                    <Main Id="M TWO">

                                                <Para Id="P ONE"></Para>      

                                                <Para Id="P TWO"></Para>      

                                                <Para Id="P THREE"></Para>

                                    </Main>

                                    <Main Id="M THREE">

                                                <Para Id="P ONE"></Para>      

                                                <Para Id="P TWO"></Para>      

                                                <Para Id="P THREE"></Para>

                                    </Main>

                        </Chapter>

            </docmain>

           

I am using a combination of 2 XmlTextReaders to parse this XML and get all the “Main” and “Para” tags and display their attribute “Id” in a tree

My result should ideally be like this.

 

-Root

-M ONE

    -P ONE

    -P TWO

    -P THREE

-M TWO

    -P ONE

    -P TWO

    -P THREE

-M THREE

    -P ONE

    -P TWO

    -P THREE

 I am using the first xmltextreader to get all the Main nodes, and then i am using
  
xmltextreader2, to read the outerxml of xmltextreader1 (i.e Main node)
  

This is the code I am using for populating the tree –

 

 

            private void Form1_Load(object sender, System.EventArgs e)

            {

                  XmlTextReader textReader  = null;

                  XmlTextReader textReader1  = null;

                  textReader  = new XmlTextReader("C:\\temp.xml");

                  textReader.WhitespaceHandling = WhitespaceHandling.None;

                 

                  TreeNode root = treeView1.Nodes.Add("Root");

 

                  while (textReader.Read())

                  {

                       

                        if (textReader.Name=="Main"&& textReader.NodeType==XmlNodeType.Element)

                        {

                              NameTable nt = new NameTable();

                              XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);

                              XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);

                              TreeNode node;

                              TreeNode node1;

                              node = root.Nodes.Add(textReader.GetAttribute("Id"));

                              textReader1=new XmlTextReader(textReader.ReadOuterXml(), XmlNodeType.Element, context);

                             

                              textReader1.WhitespaceHandling=WhitespaceHandling.None;

                              while(textReader1.Read())

                              {

                                    if (textReader1.Name=="Para"&& textReader1.NodeType==XmlNodeType.Element)

                                   

                                    node1 =node.Nodes.Add(textReader1.GetAttribute("Id"));

                                   

                                   

                              }

                             

                             

 

                        }

                  }

            }

           

      }

}

I am using a combination of two XmlTextReaders.

However, when I run my program,

I get a result like this :

-Root

-M ONE

    -P ONE

    -P TWO

    -P THREE

-M THREE

    -P ONE

    -P TWO

     -P THREE               

 

The 2nd node M TWO is not displayed at all in the tree. Surprisingly, the reader skips this.

 I think this happens because when  i  read the outerxml, the  reader advances forward and then skips the next node.

Can anyone please help me with this

Thanks,

--

Nikhil



Answer this question

Using combination of 2 XmlTextReaders fails, skips a node while reading xml.

  • Using combination of 2 XmlTextReaders fails, skips a node while reading xml.