Reading XML attributes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hans Kamp

    #1

    Reading XML attributes

    My program fails reading XML attributes. A fragment of the code is:

    private void showXmlNodeAtTr eeNode(XmlNodeL ist xnl, TreeNode tn)
    {
    int i;

    for (i = 0; i < xnl.Count; i++)
    {
    XmlNode xn = xnl[i];
    XmlNodeType nodeType = xn.NodeType;
    if (nodeType == XmlNodeType.Xml Declaration)
    {
    tn.Nodes.Add("D eclaration");
    } else
    if (nodeType == XmlNodeType.Ele ment)
    {
    tn.Nodes.Add("E lement: " + xn.Name);
    showXmlNodeAtTr eeNode(xn.Child Nodes, tn.Nodes[i]);
    } else
    if (nodeType == XmlNodeType.Tex t)
    {
    tn.Nodes.Add("T ext: " + xn.InnerText);
    } else
    if (nodeType == XmlNodeType.Att ribute)
    {
    tn.Nodes.Add("A ttribute: " + xn.Name);
    showXmlNodeAtTr eeNode(xn.Child Nodes, tn.Nodes[i]);
    } else
    if (nodeType == XmlNodeType.Ent ityReference)
    {
    tn.Nodes.Add("E ntityReference: " + xn.Name);
    showXmlNodeAtTr eeNode(xn.Child Nodes, tn.Nodes[i]);
    }
    if (nodeType == XmlNodeType.Ent ity)
    {
    tn.Nodes.Add("E ntity: " + xn.Name);
    showXmlNodeAtTr eeNode(xn.Child Nodes, tn.Nodes[i]);
    }
    }
    }

    private void parseButton_Cli ck(object sender, System.EventArg s e)
    {
    XmlDocument xd = new XmlDocument();

    xd.LoadXml(xmlB ox.Text);

    XmlNode xn = xd.FirstChild;

    xmlView.Nodes.C lear();

    xmlView.Nodes.A dd("Start");

    showXmlNodeAtTr eeNode(xd.Child Nodes, xmlView.Nodes[0]);

    }

    When I type the following in the EditBox:

    <a b='c'>d</a>

    the TreeView only shows:

    Start
    Element: a
    Text: d

    The attribte b='c' is not displayed anywhere.

    How do I solve that?

    Hans Kamp.


  • Andy Renk

    #2
    Re: Reading XML attributes

    In the example you provided <a b='c'>d</a>

    b='c' is not considered a node, it is an attribute of a node. In this case it is
    the attribute of node 'a'

    To get the properties you can change the statement

    else[color=blue]
    > if (nodeType == XmlNodeType.Att ribute)
    > {
    > tn.Nodes.Add("A ttribute: " + xn.Name);
    > showXmlNodeAtTr eeNode(xn.Child Nodes, tn.Nodes[i]);[/color]

    to

    if (xn.Attributes != null && xn.Attributes.C ount >0)
    {
    for (int loop2 = 0; loop2 < xn.Attributes.C ount; loop2++)
    {
    tn.Nodes.Add("A ttribute: "+ xn.Attributes[loop2].Name
    +" = "
    +xn.Attributes[loop2].Value);
    }
    }

    notice that attributes have two values, its name 'b' and its value 'c'
    BTW the above code should be placed right before your
    [color=blue]
    > if (nodeType == XmlNodeType.Ent ity)
    > {
    > tn.Nodes.Add("E ntity: " + xn.Name);
    > showXmlNodeAtTr eeNode(xn.Child Nodes, tn.Nodes[i]);
    > }[/color]

    statements


    Hope this help

    Andy


    "Hans Kamp" <info@hanskamp. com> wrote in message news:<bilj2s$98 3$1@news1.tilbu 1.nb.home.nl>.. .[color=blue]
    > My program fails reading XML attributes. A fragment of the code is:
    >
    > private void showXmlNodeAtTr eeNode(XmlNodeL ist xnl, TreeNode tn)
    > {
    > int i;
    >
    > for (i = 0; i < xnl.Count; i++)
    > {
    > XmlNode xn = xnl[i];
    > XmlNodeType nodeType = xn.NodeType;
    > if (nodeType == XmlNodeType.Xml Declaration)
    > {
    > tn.Nodes.Add("D eclaration");
    > } else
    > if (nodeType == XmlNodeType.Ele ment)
    > {
    > tn.Nodes.Add("E lement: " + xn.Name);
    > showXmlNodeAtTr eeNode(xn.Child Nodes, tn.Nodes[i]);
    > } else
    > if (nodeType == XmlNodeType.Tex t)
    > {
    > tn.Nodes.Add("T ext: " + xn.InnerText);
    > } else
    > if (nodeType == XmlNodeType.Att ribute)
    > {
    > tn.Nodes.Add("A ttribute: " + xn.Name);
    > showXmlNodeAtTr eeNode(xn.Child Nodes, tn.Nodes[i]);
    > } else
    > if (nodeType == XmlNodeType.Ent ityReference)
    > {
    > tn.Nodes.Add("E ntityReference: " + xn.Name);
    > showXmlNodeAtTr eeNode(xn.Child Nodes, tn.Nodes[i]);
    > }
    > if (nodeType == XmlNodeType.Ent ity)
    > {
    > tn.Nodes.Add("E ntity: " + xn.Name);
    > showXmlNodeAtTr eeNode(xn.Child Nodes, tn.Nodes[i]);
    > }
    > }
    > }
    >
    > private void parseButton_Cli ck(object sender, System.EventArg s e)
    > {
    > XmlDocument xd = new XmlDocument();
    >
    > xd.LoadXml(xmlB ox.Text);
    >
    > XmlNode xn = xd.FirstChild;
    >
    > xmlView.Nodes.C lear();
    >
    > xmlView.Nodes.A dd("Start");
    >
    > showXmlNodeAtTr eeNode(xd.Child Nodes, xmlView.Nodes[0]);
    >
    > }
    >
    > When I type the following in the EditBox:
    >
    > <a b='c'>d</a>
    >
    > the TreeView only shows:
    >
    > Start
    > Element: a
    > Text: d
    >
    > The attribte b='c' is not displayed anywhere.
    >
    > How do I solve that?
    >
    > Hans Kamp.[/color]

    Comment

    • Drebin

      #3
      Re: Reading XML attributes

      While this has come up - I thought that attributes of an existing element
      and sub-elements could (within reason) be treated as the same. So:

      <book>
      <id>3</id>
      </book>

      should be treated the same as:

      <book id="3" />

      But I keep running into the problem where I have to explicitely know whether
      it's an attribute or child node. What's up with that??


      "Andy Renk" <aktivex@yahoo. com> wrote in message
      news:bc93153d.0 308281350.1f43f 8db@posting.goo gle.com...[color=blue]
      > In the example you provided <a b='c'>d</a>
      >
      > b='c' is not considered a node, it is an attribute of a node. In this case[/color]
      it is[color=blue]
      > the attribute of node 'a'
      >
      > To get the properties you can change the statement
      >
      > else[color=green]
      > > if (nodeType == XmlNodeType.Att ribute)
      > > {
      > > tn.Nodes.Add("A ttribute: " + xn.Name);
      > > showXmlNodeAtTr eeNode(xn.Child Nodes, tn.Nodes[i]);[/color]
      >
      > to
      >
      > if (xn.Attributes != null && xn.Attributes.C ount >0)
      > {
      > for (int loop2 = 0; loop2 < xn.Attributes.C ount; loop2++)
      > {
      > tn.Nodes.Add("A ttribute: "+ xn.Attributes[loop2].Name
      > +" = "
      > +xn.Attributes[loop2].Value);
      > }
      > }
      >
      > notice that attributes have two values, its name 'b' and its value 'c'
      > BTW the above code should be placed right before your
      >[color=green]
      > > if (nodeType == XmlNodeType.Ent ity)
      > > {
      > > tn.Nodes.Add("E ntity: " + xn.Name);
      > > showXmlNodeAtTr eeNode(xn.Child Nodes, tn.Nodes[i]);
      > > }[/color]
      >
      > statements
      >
      >
      > Hope this help
      >
      > Andy
      >
      >
      > "Hans Kamp" <info@hanskamp. com> wrote in message[/color]
      news:<bilj2s$98 3$1@news1.tilbu 1.nb.home.nl>.. .[color=blue][color=green]
      > > My program fails reading XML attributes. A fragment of the code is:
      > >
      > > private void showXmlNodeAtTr eeNode(XmlNodeL ist xnl, TreeNode tn)
      > > {
      > > int i;
      > >
      > > for (i = 0; i < xnl.Count; i++)
      > > {
      > > XmlNode xn = xnl[i];
      > > XmlNodeType nodeType = xn.NodeType;
      > > if (nodeType == XmlNodeType.Xml Declaration)
      > > {
      > > tn.Nodes.Add("D eclaration");
      > > } else
      > > if (nodeType == XmlNodeType.Ele ment)
      > > {
      > > tn.Nodes.Add("E lement: " + xn.Name);
      > > showXmlNodeAtTr eeNode(xn.Child Nodes, tn.Nodes[i]);
      > > } else
      > > if (nodeType == XmlNodeType.Tex t)
      > > {
      > > tn.Nodes.Add("T ext: " + xn.InnerText);
      > > } else
      > > if (nodeType == XmlNodeType.Att ribute)
      > > {
      > > tn.Nodes.Add("A ttribute: " + xn.Name);
      > > showXmlNodeAtTr eeNode(xn.Child Nodes, tn.Nodes[i]);
      > > } else
      > > if (nodeType == XmlNodeType.Ent ityReference)
      > > {
      > > tn.Nodes.Add("E ntityReference: " + xn.Name);
      > > showXmlNodeAtTr eeNode(xn.Child Nodes, tn.Nodes[i]);
      > > }
      > > if (nodeType == XmlNodeType.Ent ity)
      > > {
      > > tn.Nodes.Add("E ntity: " + xn.Name);
      > > showXmlNodeAtTr eeNode(xn.Child Nodes, tn.Nodes[i]);
      > > }
      > > }
      > > }
      > >
      > > private void parseButton_Cli ck(object sender, System.EventArg s e)
      > > {
      > > XmlDocument xd = new XmlDocument();
      > >
      > > xd.LoadXml(xmlB ox.Text);
      > >
      > > XmlNode xn = xd.FirstChild;
      > >
      > > xmlView.Nodes.C lear();
      > >
      > > xmlView.Nodes.A dd("Start");
      > >
      > > showXmlNodeAtTr eeNode(xd.Child Nodes, xmlView.Nodes[0]);
      > >
      > > }
      > >
      > > When I type the following in the EditBox:
      > >
      > > <a b='c'>d</a>
      > >
      > > the TreeView only shows:
      > >
      > > Start
      > > Element: a
      > > Text: d
      > >
      > > The attribte b='c' is not displayed anywhere.
      > >
      > > How do I solve that?
      > >
      > > Hans Kamp.[/color][/color]


      Comment

      • Jon Skeet

        #4
        Re: Reading XML attributes

        Drebin <tRhEeMdOrVeEbi n@hotmail.com> wrote:[color=blue]
        > While this has come up - I thought that attributes of an existing element
        > and sub-elements could (within reason) be treated as the same. So:
        >
        > <book>
        > <id>3</id>
        > </book>
        >
        > should be treated the same as:
        >
        > <book id="3" />[/color]

        Absolutely not! They're *completely* different bits of XML, and should
        be treated as such. Why do you think they should be treated as the
        same?

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Drebin

          #5
          Re: Reading XML attributes

          Because everywhere I've read, those should be completely interchangeable - and for many processes that we use, they ARE interchangeable (we can do, like what I typed below).. But in code, when *I* have to read XML data from a specific node - I don't know how people make it that invisible? I have to test to see if it's an attribute or sub-element..

          Are you sure this is by design? Any examples?? TIA


          "Jon Skeet" <skeet@pobox.co m> wrote in message news:MPG.19b90c 8b3d0b134998a41 3@news.microsof t.com...
          Drebin <tRhEeMdOrVeEbi n@hotmail.com> wrote:[color=blue]
          > While this has come up - I thought that attributes of an existing element
          > and sub-elements could (within reason) be treated as the same. So:
          >
          > <book>
          > <id>3</id>
          > </book>
          >
          > should be treated the same as:
          >
          > <book id="3" />[/color]

          Absolutely not! They're *completely* different bits of XML, and should
          be treated as such. Why do you think they should be treated as the
          same?

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Jon Skeet

            #6
            Re: Reading XML attributes

            Drebin <tRhEeMdOrVeEbi n@hotmail.com> wrote:[color=blue]
            > Because everywhere I've read, those should be completely interchangeable[/color]

            Are any of these articles/whatever online? I don't remember seeing
            anything like that before - I'd be interested to see the context, if
            you have any links handy.
            [color=blue]
            > - and for many processes that we use, they ARE interchangeable (we can do,
            > like what I typed below).[/color]

            Try writing HTML in that way and you'll find it doesn't work at all.

            DTDs, schemas etc are very specific about the differences
            [color=blue]
            > But in code, when *I* have to read XML data from a specific node - I
            > don't know how people make it that invisible? I have to test to see
            > if it's an attribute or sub-element..[/color]

            Presumably they have schemas where it can occur either way, and they
            specifically write code to test for
            [color=blue]
            > Are you sure this is by design?[/color]

            Absolutely. They're separate concepts.
            [color=blue]
            > Any examples?? TIA[/color]

            HTML (eg v4-strict, so it's actually XML) is a good example. You can't
            get away with:

            [... other stuff ...]
            <body p="Some text here">
            [... other stuff ...]

            which according to your view of things (as I'm understanding them -
            please correct me if I've got the wrong end of the stick) would be
            equivalent to:

            [... other stuff ...]
            <body>
            <p>Some text here</p>
            </body>
            [... other stuff ...]

            The latter is fine.

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Drebin

              #7
              Re: Reading XML attributes

              Hmm... very interesting... I guess you really do learn something new
              everyday!! thanks for the clarification!


              "Jon Skeet" <skeet@pobox.co m> wrote in message
              news:MPG.19b958 f04d7c828098a41 a@news.microsof t.com...[color=blue]
              > Drebin <tRhEeMdOrVeEbi n@hotmail.com> wrote:[color=green]
              > > Because everywhere I've read, those should be completely interchangeable[/color]
              >
              > Are any of these articles/whatever online? I don't remember seeing
              > anything like that before - I'd be interested to see the context, if
              > you have any links handy.
              >[color=green]
              > > - and for many processes that we use, they ARE interchangeable (we can[/color][/color]
              do,[color=blue][color=green]
              > > like what I typed below).[/color]
              >
              > Try writing HTML in that way and you'll find it doesn't work at all.
              >
              > DTDs, schemas etc are very specific about the differences
              >[color=green]
              > > But in code, when *I* have to read XML data from a specific node - I
              > > don't know how people make it that invisible? I have to test to see
              > > if it's an attribute or sub-element..[/color]
              >
              > Presumably they have schemas where it can occur either way, and they
              > specifically write code to test for
              >[color=green]
              > > Are you sure this is by design?[/color]
              >
              > Absolutely. They're separate concepts.
              >[color=green]
              > > Any examples?? TIA[/color]
              >
              > HTML (eg v4-strict, so it's actually XML) is a good example. You can't
              > get away with:
              >
              > [... other stuff ...]
              > <body p="Some text here">
              > [... other stuff ...]
              >
              > which according to your view of things (as I'm understanding them -
              > please correct me if I've got the wrong end of the stick) would be
              > equivalent to:
              >
              > [... other stuff ...]
              > <body>
              > <p>Some text here</p>
              > </body>
              > [... other stuff ...]
              >
              > The latter is fine.
              >
              > --
              > Jon Skeet - <skeet@pobox.co m>
              > http://www.pobox.com/~skeet/
              > If replying to the group, please do not mail me too[/color]


              Comment

              Working...