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/