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. 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.
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.
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
...
|
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>...
|
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...
|
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)
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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....
|
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...
| |