1

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.

IrishChieftain
  • 15,108
  • 7
  • 50
  • 91
  • Is there any reason you don't want to just use xpath to select the nodes you want? Using an XmlReader seems much more difficult. – rsbarro Jun 24 '11 at 21:22
  • I guess not, just never used it before. I want to be able to extract all info (atts & values) for each link before moving on to the next iteration of the main while loop - would welcome a code sample :) – IrishChieftain Jun 24 '11 at 21:26
  • I had a look at Api and found method to read subtree, method is reader.ReadSubtree. It returns you an instance of XmlReader object – Kunal Jun 24 '11 at 21:54

3 Answers3

4

LINQ to XML makes this stuff crazy easy sauce:

XDocument doc = XDocument.Parse("xml here or use .Load()");
var links = doc.Descendants("link");

You can extend off from here reading attributes (XAttribute) off the XNode individually or again using LINQ to obtain all of the attributes for all nodes etc etc.

Darren Lewis
  • 8,338
  • 3
  • 35
  • 55
  • Have never used LINQ to XML, is it possible to query without using an XDocument? Looking for a link relevant to this scenario... – IrishChieftain Jun 24 '11 at 23:42
  • You can start at the XNode level using the static XNode.ReadFrom method which takes your XmlReader as a param and returns the XML fragment as an XNode. If you don't have it already I'd highly recommend downloading LINQPAD (http://www.linqpad.com), loading in your XML and exploring the API. – Darren Lewis Jun 25 '11 at 10:14
  • Daz, can you give me the syntax or a doc link, for digging into the elements and attributes? Thanks :) – IrishChieftain Jun 25 '11 at 16:45
3

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 :)

kprobst
  • 16,165
  • 5
  • 32
  • 53
1

Although i wouldnt prefer XmlReader over LINQ or XmlDocument, this should help u working with xmlReader(scroll down) http://vbdotnetforum.com/index.php?/topic/493-creating-an-xml-reader/

--

U might want to use LINQ, it shouldnt be hard to understand.. Here is a starter http://www.developingfor.net/c-30/upgrade-your-c-skills-part-5-linq-to-xml.html

CodingSlayer
  • 883
  • 1
  • 7
  • 14