Iterate over unkown attributes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • barnum@bluezone.no

    Iterate over unkown attributes

    Hi,

    I have an XML doc with a node with several unknown attributes, e.g.
    <Fruit banana="true" apple="false" lemon="true" />

    Is there a .NET-method to iterate over the attributes without knowing
    their names?
    (XPathNavigator .GetAttribute requires that I know what attribute to
    look for.)

    Thanks!
  • Anthony Jones

    #2
    Re: Iterate over unkown attributes

    <barnum@bluezon e.nowrote in message
    news:6f1b31fb-28b0-45ea-8d74-3aa8cc280308@e2 g2000hsh.google groups.com...
    Hi,
    >
    I have an XML doc with a node with several unknown attributes, e.g.
    <Fruit banana="true" apple="false" lemon="true" />
    >
    Is there a .NET-method to iterate over the attributes without knowing
    their names?
    (XPathNavigator .GetAttribute requires that I know what attribute to
    look for.)
    >
    If you are using XPathNavigator then you can simply use .Select("@*") when
    on the Fruit element.




    --
    Anthony Jones - MVP ASP/ASP.NET

    Comment

    • Martin Honnen

      #3
      Re: Iterate over unkown attributes

      barnum@bluezone .no wrote:
      I have an XML doc with a node with several unknown attributes, e.g.
      <Fruit banana="true" apple="false" lemon="true" />
      >
      Is there a .NET-method to iterate over the attributes without knowing
      their names?
      (XPathNavigator .GetAttribute requires that I know what attribute to
      look for.)
      XPathDocument doc = new XPathDocument(n ew
      StringReader(@" <Fruit banana=""true"" apple=""false"" lemon=""true"" />"));

      foreach (XPathNavigator att in
      doc.CreateNavig ator().Select(" Fruit/@*"))
      {
      Console.WriteLi ne("Attribute with name \"{0}\" has
      value \"{1}\".", att.Name, att.Value);
      }


      --

      Martin Honnen --- MVP XML

      Comment

      • Martin Honnen

        #4
        Re: Iterate over unkown attributes

        barnum@bluezone .no wrote:
        Is there a .NET-method to iterate over the attributes without knowing
        their names?
        As an alternative to the earlier suggestion:

        XPathDocument doc = new XPathDocument(n ew
        StringReader(@" <Fruit banana=""true"" apple=""false"" lemon=""true"" />"));

        XPathNavigator root =
        doc.CreateNavig ator().SelectSi ngleNode("Fruit ");
        if (root.MoveToFir stAttribute())
        {
        do
        {
        Console.WriteLi ne("Attribute with name \"{0}\" has
        value \"{1}\".", root.Name, root.Value);
        }
        while (root.MoveToNex tAttribute());
        }



        --

        Martin Honnen --- MVP XML

        Comment

        Working...