One problem is that your example XML is not well-formed as there is no end tag </item> for the start tag <item> and as there is a end tag </client> without a matching start tag.
Assuming your XML is
< Items>
<item name="item1">
<delimitor>*</delimitor>
<column>
<name>One</name>
<detail>25</detail>
</column>
<column>
<name>Two</name>
<length>30</length>
</column>
<description>two elements</description>
</item>
</ Items>
then you could simply use XmlDocument, SelectNodes, SelectSingleNode and InnerText to find the contents of the elements name and length (if available) as follows:
XmlDocument xmlDocument = new XmlDocument();
// using SelectNodes/SelectSingleNode
int columnNumber = 0;
foreach (XmlElement column in xmlDocument.SelectNodes("/Items//column"))
{
Console.WriteLine("Checking column {0}", ++columnNumber);
XmlElement name = column.SelectSingleNode("name") as XmlElement;
if (name != null)
{
Console.WriteLine("Found name: \"{0}\".", name.InnerText);
}
XmlElement length = column.SelectSingleNode("length") as XmlElement;
if (length != null)
{
Console.WriteLine("Found length: \"{0}\".", length.InnerText);
}
else
{
Console.WriteLine("No length found.");
}
Console.WriteLine();
}
That example does not use an XmlNodeReader however as I don't see any gain in using one. If you really want to use XmlNodeReader then explain why and on which node, do you want to apply the reader to the whole document or only to some nodes
|