I'm using a simple XmlReader on the following file structure:
<application>
<nodetitle permission="perm1">Some Dept</nodetitle>
<project>Project A</project>
<links>
<link>
<pagename>page1.aspx</pagename>
</link>
<link>
<pagename permission="perm2">page2.aspx</pagename>
</link>
<link>
<pagename>page3.aspx</pagename>
</link>
</links>
</application>
I'm rusty on the XML API and my problem is reading the sibling <link> elements in a single pass - my instinct is to look to create some kind of inner loop?
while (reader.Read())
{
...
if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "links"))
{
// read all <link> elements in a single pass
}
...
}
UPDATE - 06-25-2011
I'm going to try and be a little more specific. I'm not using XPath. Using either the XmlReader or Linq to Xml (am totally unfamiliar with this), I want a way to extract the link elements and attributes and check their values.
Well if you use an XML doc you could do something like this:
XmlNodeList nodeList = xmlDocument
.DocumentElement.SelectNodes("//application/links/link");
foreach (XmlNode node in nodeList) {
...
}
In the loop just get the inner <pageName>
element and read the attributes and/or text.
That said, XPath is much easier, faster and memory efficient than a reader or a full Xml doc. Linq2XML is also really good to work with, since you can use Linq syntax on the parse tree.
Edit:
I felt kind of dirty for mentioning XmlDocument so I created a simple Linq2Xml example to show how easy this is even omitting any Linq syntax, just in case you want to go for it:
string path = @"C:\path\to\my\xmlfile.xml";
XDocument doc = XDocument.Load(path, LoadOptions.None);
var nodes = doc.Root.Element("links").Elements("link");
foreach (var node in nodes) {
var pageNameElement = node.Element("pagename");
XAttribute permAttribute = pageNameElement.Attribute("permission");
string permission = "";
if (permAttribute != null)
permission = permAttribute.Value;
string text = pageNameElement.Value;
// Do something with the values...
}
You can of course initialize the XDocument with a stream, if that's what you already have. Hope this helps :)