Reading XML attributes

H

Hans Kamp

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

private void showXmlNodeAtTreeNode(XmlNodeList xnl, TreeNode tn)
{
int i;

for (i = 0; i < xnl.Count; i++)
{
XmlNode xn = xnl;
XmlNodeType nodeType = xn.NodeType;
if (nodeType == XmlNodeType.XmlDeclaration)
{
tn.Nodes.Add("Declaration");
} else
if (nodeType == XmlNodeType.Element)
{
tn.Nodes.Add("Element: " + xn.Name);
showXmlNodeAtTreeNode(xn.ChildNodes, tn.Nodes);
} else
if (nodeType == XmlNodeType.Text)
{
tn.Nodes.Add("Text: " + xn.InnerText);
} else
if (nodeType == XmlNodeType.Attribute)
{
tn.Nodes.Add("Attribute: " + xn.Name);
showXmlNodeAtTreeNode(xn.ChildNodes, tn.Nodes);
} else
if (nodeType == XmlNodeType.EntityReference)
{
tn.Nodes.Add("EntityReference: " + xn.Name);
showXmlNodeAtTreeNode(xn.ChildNodes, tn.Nodes);
}
if (nodeType == XmlNodeType.Entity)
{
tn.Nodes.Add("Entity: " + xn.Name);
showXmlNodeAtTreeNode(xn.ChildNodes, tn.Nodes);
}
}
}

private void parseButton_Click(object sender, System.EventArgs e)
{
XmlDocument xd = new XmlDocument();

xd.LoadXml(xmlBox.Text);

XmlNode xn = xd.FirstChild;

xmlView.Nodes.Clear();

xmlView.Nodes.Add("Start");

showXmlNodeAtTreeNode(xd.ChildNodes, 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.
 
A

Andy Renk

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
if (nodeType == XmlNodeType.Attribute)
{
tn.Nodes.Add("Attribute: " + xn.Name);
showXmlNodeAtTreeNode(xn.ChildNodes, tn.Nodes);


to

if (xn.Attributes != null && xn.Attributes.Count >0)
{
for (int loop2 = 0; loop2 < xn.Attributes.Count; loop2++)
{
tn.Nodes.Add("Attribute: "+ 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
if (nodeType == XmlNodeType.Entity)
{
tn.Nodes.Add("Entity: " + xn.Name);
showXmlNodeAtTreeNode(xn.ChildNodes, tn.Nodes);
}


statements


Hope this help

Andy


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

private void showXmlNodeAtTreeNode(XmlNodeList xnl, TreeNode tn)
{
int i;

for (i = 0; i < xnl.Count; i++)
{
XmlNode xn = xnl;
XmlNodeType nodeType = xn.NodeType;
if (nodeType == XmlNodeType.XmlDeclaration)
{
tn.Nodes.Add("Declaration");
} else
if (nodeType == XmlNodeType.Element)
{
tn.Nodes.Add("Element: " + xn.Name);
showXmlNodeAtTreeNode(xn.ChildNodes, tn.Nodes);
} else
if (nodeType == XmlNodeType.Text)
{
tn.Nodes.Add("Text: " + xn.InnerText);
} else
if (nodeType == XmlNodeType.Attribute)
{
tn.Nodes.Add("Attribute: " + xn.Name);
showXmlNodeAtTreeNode(xn.ChildNodes, tn.Nodes);
} else
if (nodeType == XmlNodeType.EntityReference)
{
tn.Nodes.Add("EntityReference: " + xn.Name);
showXmlNodeAtTreeNode(xn.ChildNodes, tn.Nodes);
}
if (nodeType == XmlNodeType.Entity)
{
tn.Nodes.Add("Entity: " + xn.Name);
showXmlNodeAtTreeNode(xn.ChildNodes, tn.Nodes);
}
}
}

private void parseButton_Click(object sender, System.EventArgs e)
{
XmlDocument xd = new XmlDocument();

xd.LoadXml(xmlBox.Text);

XmlNode xn = xd.FirstChild;

xmlView.Nodes.Clear();

xmlView.Nodes.Add("Start");

showXmlNodeAtTreeNode(xd.ChildNodes, 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.
 
D

Drebin

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 said:
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
if (nodeType == XmlNodeType.Attribute)
{
tn.Nodes.Add("Attribute: " + xn.Name);
showXmlNodeAtTreeNode(xn.ChildNodes, tn.Nodes);


to

if (xn.Attributes != null && xn.Attributes.Count >0)
{
for (int loop2 = 0; loop2 < xn.Attributes.Count; loop2++)
{
tn.Nodes.Add("Attribute: "+ 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
if (nodeType == XmlNodeType.Entity)
{
tn.Nodes.Add("Entity: " + xn.Name);
showXmlNodeAtTreeNode(xn.ChildNodes, tn.Nodes);
}


statements


Hope this help

Andy


"Hans Kamp" <[email protected]> wrote in message
My program fails reading XML attributes. A fragment of the code is:

private void showXmlNodeAtTreeNode(XmlNodeList xnl, TreeNode tn)
{
int i;

for (i = 0; i < xnl.Count; i++)
{
XmlNode xn = xnl;
XmlNodeType nodeType = xn.NodeType;
if (nodeType == XmlNodeType.XmlDeclaration)
{
tn.Nodes.Add("Declaration");
} else
if (nodeType == XmlNodeType.Element)
{
tn.Nodes.Add("Element: " + xn.Name);
showXmlNodeAtTreeNode(xn.ChildNodes, tn.Nodes);
} else
if (nodeType == XmlNodeType.Text)
{
tn.Nodes.Add("Text: " + xn.InnerText);
} else
if (nodeType == XmlNodeType.Attribute)
{
tn.Nodes.Add("Attribute: " + xn.Name);
showXmlNodeAtTreeNode(xn.ChildNodes, tn.Nodes);
} else
if (nodeType == XmlNodeType.EntityReference)
{
tn.Nodes.Add("EntityReference: " + xn.Name);
showXmlNodeAtTreeNode(xn.ChildNodes, tn.Nodes);
}
if (nodeType == XmlNodeType.Entity)
{
tn.Nodes.Add("Entity: " + xn.Name);
showXmlNodeAtTreeNode(xn.ChildNodes, tn.Nodes);
}
}
}

private void parseButton_Click(object sender, System.EventArgs e)
{
XmlDocument xd = new XmlDocument();

xd.LoadXml(xmlBox.Text);

XmlNode xn = xd.FirstChild;

xmlView.Nodes.Clear();

xmlView.Nodes.Add("Start");

showXmlNodeAtTreeNode(xd.ChildNodes, 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.
 
J

Jon Skeet

Drebin said:
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" />

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?
 
D

Drebin

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 said:
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" />

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?
 
J

Jon Skeet

Drebin said:
Because everywhere I've read, those should be completely interchangeable

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.
- and for many processes that we use, they ARE interchangeable (we can do,
like what I typed below).

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
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..

Presumably they have schemas where it can occur either way, and they
specifically write code to test for
Are you sure this is by design?

Absolutely. They're separate concepts.
Any examples?? TIA

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.
 
D

Drebin

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


Jon Skeet said:
Drebin said:
Because everywhere I've read, those should be completely interchangeable

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.
- and for many processes that we use, they ARE interchangeable (we can do,
like what I typed below).

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
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..

Presumably they have schemas where it can occur either way, and they
specifically write code to test for
Are you sure this is by design?

Absolutely. They're separate concepts.
Any examples?? TIA

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.
 
Top