XML file and SearchNode

  • Thread starter Thread starter Krzysztof
  • Start date Start date
K

Krzysztof

Hi all
I have large XML file (5 levels of RootNode.ChildNodes.ChildNodes... and so
on... Of course XmlType are Text and Atributs) and I must search in it some
elements by various criteria. I think about "my privet method
SelectXmlElement", but it's not so easy. To much levels for "foreach" or
other IEnumerable...

So before I implement this "horrible" method(s) I have one question. Is
something in VS.Net to auto search in XML? I read some about XPath, but it's
look like strange magic...

But if XPath is reason -> please, just one simple example will be helpfull

Regards
Krzysztof
 
Hi,
You could use XPath for XML search. Below is several simple examples:

Let our data.xml file contains:
<mytag>
<mytag2>
<mysubtag val="abc"/>
</mytag2>
<mysubtag val="def"/>
</mytag>"

XPathDocument doc = new XPathDocument("data.xml");
// Search for all <mysubtag/> elements
XPathNavigator n = doc.CreateNavigator();
XPathNodeIterator iter = n.Select("//mysubtag");
while (iter.MoveNext())
{
// execute some processing for node iter.Current
}
//--------------------------------------------------------
XPathDocument doc = new XPathDocument("data.xml");
// Search for all <mysubtag/> elements where val is "abc"
XPathNavigator n = doc.CreateNavigator();
XPathNodeIterator iter = n.Select("//mysubtag[@val = 'abc']");
while (iter.MoveNext())
{
// execute some processing for node iter.Current
}
//--------------------------------------------------------
XPathDocument doc = new XPathDocument("data.xml");
// Search for all <mysubtag/> elements inside <mytag2>
XPathNavigator n = doc.CreateNavigator();
XPathNodeIterator iter = n.Select("//mytag2/mysubtag");
while (iter.MoveNext())
{
// execute some processing for node iter.Current
}

--
Andrew Gnenny
pulsar2003@/no-spam/email.ru (Please remove /no-spam/ for reply)
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE
 
Back
Top