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 Id="P TWO"></
<Para Id="P THREE"></
</Main>
<Main Id="M TWO">
<Para Id="P ONE"></
<Para Id="P TWO"></
<Para Id="P THREE"></
</Main>
<Main Id="M THREE">
<Para Id="P ONE"></
<Para Id="P TWO"></
<Para Id="P THREE"></
</Main>
</Chapter>
</docmain>
I am using a combination of 2 XmlTextReaders to parse this XML and get all the “Main” and “
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
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=="
{
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=="
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

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