473,385 Members | 1,693 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,385 software developers and data experts.

Node Type both Element and Text

I have an XML node that has both attributes and text. I am having difficulty
getting the xmlreader to deal with this.

Sample XML Node:
<HomePhoneNumber Attribute1="Y" Attribute2="Y" Attribute3="Y">(425)
555-1212</HomePhoneNumber>

I'd like to iterate on this and output the node's path, name, each attribute
name and value pair, as well as the phone number itself.
Mar 19 '07 #1
2 3844
Kevin wrote:
I have an XML node that has both attributes and text. I am having difficulty
getting the xmlreader to deal with this.

Sample XML Node:
<HomePhoneNumber Attribute1="Y" Attribute2="Y" Attribute3="Y">(425)
555-1212</HomePhoneNumber>

I'd like to iterate on this and output the node's path, name, each attribute
name and value pair, as well as the phone number itself.
Do it like this:

using (XmlReader xmlReader = XmlReader.Create(@"file.xml")) {
while (xmlReader.Read()) {
if (xmlReader.NodeType == XmlNodeType.Element) {
if (xmlReader.Name == "HomePhoneNumber") {
Console.WriteLine("Element \"{0}\":", xmlReader.Name);
while (xmlReader.MoveToNextAttribute()) {
Console.WriteLine("Attribute {0}={1}", xmlReader.Name,
xmlReader.Value);
}
Console.WriteLine("Element content: \"{0}\"",
xmlReader.ReadString());
Console.WriteLine();
}
}
}
}
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 19 '07 #2
Great thanks - I should explain my problem more - I am trying to write a
function that traverses an XML document and outputs the full path
(/Node1/Node2/Node3), and a list of attribute/value pairs, as well as the
content of the node.

The below solution is able to output the latter 2, but path seems difficult
to get from XmlReader and I find myself writing a recursive function to get
this.

For example, an an input file:
<root>
<node1 attributeX="Y">XXX</node1>
<node2>
<node3 attributeY="N">YYY</node3>
</node2>
</root>

would output the following:

Path,Value
-------------------
/root/node1/AttributeX,Y
/root/node1,XXX
/root/node2/node3/attributeY,N
/root/node2/node3,YYY

"Martin Honnen" wrote:
Kevin wrote:
I have an XML node that has both attributes and text. I am having difficulty
getting the xmlreader to deal with this.

Sample XML Node:
<HomePhoneNumber Attribute1="Y" Attribute2="Y" Attribute3="Y">(425)
555-1212</HomePhoneNumber>

I'd like to iterate on this and output the node's path, name, each attribute
name and value pair, as well as the phone number itself.

Do it like this:

using (XmlReader xmlReader = XmlReader.Create(@"file.xml")) {
while (xmlReader.Read()) {
if (xmlReader.NodeType == XmlNodeType.Element) {
if (xmlReader.Name == "HomePhoneNumber") {
Console.WriteLine("Element \"{0}\":", xmlReader.Name);
while (xmlReader.MoveToNextAttribute()) {
Console.WriteLine("Attribute {0}={1}", xmlReader.Name,
xmlReader.Value);
}
Console.WriteLine("Element content: \"{0}\"",
xmlReader.ReadString());
Console.WriteLine();
}
}
}
}
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 23 '07 #3

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

Similar topics

2
by: Ralf Wahner | last post by:
Dear Masters of XSLT Could I ask you for a clue on the following question? I'd like to use XSLT to transform an XML source file to LaTeX. In the following small example the <para> Element...
1
by: Mike Kamermans | last post by:
Is there a way to determine what a node() find returns? For instance, if I have a snip of XSLT that looks like: <xsl:for-each select="node()"> ... </xsl:for-each> Is there a way to, for each...
6
by: Ron Brennan | last post by:
I want if possible to use something like: element.getElementsByTagName('text') but that doesn't work. Is there another value for the parameter, or is it not possible with getElementsByTagName?...
3
by: Newbie | last post by:
Hi Making the switch to .net from VB6, and I noticed that the XMLNode object does not expose a text property. How do I write the text value of an element(vb6 - nod.Text = ....)? BTW I have googled...
6
by: SHC | last post by:
Hi all, I created an application from the Console Application (.NET) of VC++ .NET 2003, and I did "Build" the application of the attached .cpp file, volcanoes.xml and geology.dtd on my VC++ .NET...
16
by: Joel Byrd | last post by:
I am having this ridiculous problem of trying to set the innerHTML of a div node. It works in all other browsers, but internet explorer is giving me an "Unknown runtime error". This is actually...
0
by: HP17 | last post by:
Hi all, I am a bit desperate already trying to display a XML file using XSLT. I create a table and use <xsl:for-each> to extract every element node. This is working fine. What I want now is so...
21
by: Mark Cooney | last post by:
Good afternoon, yes i am a newby so pls keep it simple. Yesterday I started a small project where I have to make an API call and receive back from that the long xml transcript down below. Now,...
3
by: drajani | last post by:
I have the below input XML sent by APP1. <?xml version="1.0" encoding="UTF-8"?> <abc1:QWAARequest xmlns:abc1="http://www.mysite.com/myLink/v1/Test.xsd" ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.