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.

3
  • 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. Commented Jun 24, 2011 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 :) Commented Jun 24, 2011 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 Commented Jun 24, 2011 at 21:54

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Have never used LINQ to XML, is it possible to query without using an XDocument? Looking for a link relevant to this scenario...
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 (linqpad.com), loading in your XML and exploring the API.
Daz, can you give me the syntax or a doc link, for digging into the elements and attributes? Thanks :)
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 :)

1 Comment

Working through this now, feel free to offer any Linq syntax :)
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.