Pasing XML Document on CE

Posted on 2008-11-05
Last Modified: 2013-11-17

I've a XML Document with a structur like this:
<?xml version="1.0" encoding="utf-8"?>
  <test1 name="Kalender" bgimage="Menu2">
    <control id="1" event="My1_Click" xy="1" name="Raume" active="Pic2o" type="1" />
    <control id="2" event="My2_Click" xy="2" name="Kalender" active="Pic1o" type="1" />
    <control id="3" event="My3_Click" xy="3" name="Einnstellungen" active="Pic3o" type="1" />
  <test2 name="Raeume" bgimage="Menu">
    <control id="1" event="My1_Click" xy="1" name="Raume" active="Pic1o" type="1"/>
    <control id="2" event="My2_Click" xy="2" name="Kalender" active="Pic2o" type="1" />
    <control id="3" event="My3_Click" xy="3" name="Einnstellungen" active="Pic3o" type="1" />
  <test name="Test" bgimage="Menu">
    <control id="1" xy="1" name="Raume" type="2" width="50" height="100"/>
    <control id="2" xy="2" name="Kalender" type="3" width="250" height="20"/>
    <control xy="3" name="Einnstellungen" type="4" width="100" height="20"/>
    <control id="4" event="My1_Click" xy="2" name="Kalender" active="Pic2o" type="1" />

To access the nodes at the moment I use:
private static XmlDocument screens = new XmlDocument();
screens.Load(Globals.inipath + "screens.xml");
// Access node I need
XmlNodeList nodes = screens.SelectNodes("screens/test/control");
// Get Attributes
foreach (XmlNode node in nodes)
int ty = Convert.ToInt32(node.Attributes["type"].Value);
                switch (ty)
                case 1:
                    ta = node.Attributes["event"].Value;
                 case 2:
                    wi = node.Attributes["width"].Value;

Yes it works, but I've ever depending from the ID to select what Attributes I've to read.

Is there a way to access any Attribute included by a loop like XMLTextReader (MoveToNextAttribute) can do?
For sure I could complete change to XMLTextReader but I think than I've to miss SelectNodes.

Is it possbile to combinate both?
I'm developing with the CF 2.0 and can't upgrade it.


Question by:andre72
    LVL 27

    Expert Comment

    >>Yes it works, but I've ever depending from the ID to select what Attributes I've to read

    I don't quite understand that sentence. You can explain ine German if you wish.

    Secondly you select all control nodes and select the type attribute, then select on its value.

    Node.Attributes is a NamedNodeMap which is a combination of an iterator (with item, nextNode and reset) and getQualifiedItem and getNamedItem methods. So if you can explain what you want to do exactly, it will be possible to do it.

    Author Comment

    Ok, Deutsch ist leichter ...
    Also, jeder Control Eintrag hat ja eine unterschiedliche Anzahl an Attributen, diese heissen nun auch wiederum anders (mal event="", andere haben width="" usw.)

    Mit dem XMLTextReader könnte ich das ja in einer Schleife durchlkaufen, müßte aber ebenso vorher meinen Knoten suchen (while (reader.Read()) während ich mit SelectNodes ja direkt entsprechenden Knoten anspreche (obwohl das intern wohl auch nichts anderes machen wird, als diesen in einer Schleife zu suchen).
    Also was ich suche ist eigentlich folgend Möglichkeit:
    nodes = screens.SelectNodes("screens/" + room + "/control");
    foreach (XmlNode node in nodes)
                         foreach(attribute in irgendwas)
    LVL 27

    Accepted Solution

    Node.attributes returns a NamedNodeMap which supports an iterator. The iterator will return nodes (of type attribute). The NamedNodeMap was introduced so that one could directly access the attribute nodes without have to get each individual node. So it goes :-

          foreach (XMLNode attNode in Node.Attributes)  

    Author Closing Comment

    Bis auf Deinen Tippfehler perfekt - mit foreach (XmlAttribute attNode in node.Attributes) geht es. Vielen Dank

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now