473,326 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Reading XML attributes

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[i];
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[i]);
} 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[i]);
} else
if (nodeType == XmlNodeType.EntityReference)
{
tn.Nodes.Add("EntityReference: " + xn.Name);
showXmlNodeAtTreeNode(xn.ChildNodes, tn.Nodes[i]);
}
if (nodeType == XmlNodeType.Entity)
{
tn.Nodes.Add("Entity: " + xn.Name);
showXmlNodeAtTreeNode(xn.ChildNodes, tn.Nodes[i]);
}
}
}

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.
Nov 15 '05 #1
6 10197
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[i]);
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[i]);
}
statements
Hope this help

Andy
"Hans Kamp" <in**@hanskamp.com> wrote in message news:<bi**********@news1.tilbu1.nb.home.nl>... 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[i];
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[i]);
} 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[i]);
} else
if (nodeType == XmlNodeType.EntityReference)
{
tn.Nodes.Add("EntityReference: " + xn.Name);
showXmlNodeAtTreeNode(xn.ChildNodes, tn.Nodes[i]);
}
if (nodeType == XmlNodeType.Entity)
{
tn.Nodes.Add("Entity: " + xn.Name);
showXmlNodeAtTreeNode(xn.ChildNodes, tn.Nodes[i]);
}
}
}

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.

Nov 15 '05 #2
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" <ak*****@yahoo.com> wrote in message
news:bc**************************@posting.google.c om...
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[i]);


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[i]);
}


statements
Hope this help

Andy
"Hans Kamp" <in**@hanskamp.com> wrote in message

news:<bi**********@news1.tilbu1.nb.home.nl>...
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[i];
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[i]);
} 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[i]);
} else
if (nodeType == XmlNodeType.EntityReference)
{
tn.Nodes.Add("EntityReference: " + xn.Name);
showXmlNodeAtTreeNode(xn.ChildNodes, tn.Nodes[i]);
}
if (nodeType == XmlNodeType.Entity)
{
tn.Nodes.Add("Entity: " + xn.Name);
showXmlNodeAtTreeNode(xn.ChildNodes, tn.Nodes[i]);
}
}
}

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.

Nov 15 '05 #3
Drebin <tR*************@hotmail.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #4
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" <sk***@pobox.com> wrote in message news:MP************************@news.microsoft.com ...
Drebin <tR*************@hotmail.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #5
Drebin <tR*************@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #6
Hmm... very interesting... I guess you really do learn something new
everyday!! thanks for the clarification!
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Drebin <tR*************@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Carl Lindmark | last post by:
*Cross-posting from microsoft.public.dotnet.languages.csharp, since I believe the question is better suited in this XML group* Hello all, I'm having some problems understanding all the ins and...
9
by: Xarky | last post by:
Hi, I am writing an XML file in the following way. Now I need to read again that file to retrieve data such as Name and Age. Can someone help me out. Thanks in Advance ...
1
by: Maziar Aflatoun | last post by:
Hi everyone, I like to use the dataset object for reading the following XML. <?xml version="1.0" encoding="utf-8"?> <ORDERS> <ORDER> <GENERALCONTACT> <FirstName>FirstName1</FirstName>...
1
by: SteveB | last post by:
I'm porting an application from Apache Xerces to .Net and am having a couple of small problems with deserialization. The XML that I'm reading comes from a variety of sources, and there are two...
4
by: sherifffruitfly | last post by:
Hi, I have an xml file with structured like this: <?xml version="1.0" encoding="UTF-8"?> <Soldiers> <Soldier name="Billy Smith" rank="Private" serial="34" /> (a bunch more soldiers)
1
by: tdpriya1984 | last post by:
Hi, Is it possible to fetch the Elements and Attributes in XML by reading the XSD?? I want to develop a application whose Elements and Attributes changes when i change my Schema. The Elements and...
1
by: nitusa | last post by:
Hi All, First time poster, and newbie C# programmer so be patient with my ignorance. :) For my current project I need to store some information (install dir., file names, passwords, ect.) and...
3
by: vb2008 | last post by:
So I am trying to figure out a new part now but I am getting stuck once again and yes I have been reading up on things and I think I know what my problem is I am just not sure how to fix it. Below is...
1
by: Demon4231 | last post by:
I have a XML file that stores information about an application I am trying to read and it looks like this: <Data> <Login UserName="Username" Password="Password"/> <Application...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.