coffeeScripter
coffeeScripter

Reputation: 33

XmlReader.Name returns an empty value

I am reading in data from an external server. The data comes in fine and I can set all the text values but when I try to read in tagname there it doesn't work. My overall goal is to populate a dataGridView with tag names in one column and tag values in another. To reiterate I have the tag value but not the tag name. The problem code:

if (xmlRead.NodeType == XmlNodeType.Text){
   Console.WriteLine(xmlRead.Name + " : " + xmlRead.Value);
}

where xmlRead is my XmlReader.

The result is : Beef

Example data format is <type>Beef</type>

Upvotes: 3

Views: 2692

Answers (2)

Jon Hanna
Jon Hanna

Reputation: 113342

Say you had an XML structured like:

<element>Some Text</element>

First an XmlReader would first hit a node with type XmlNodeType.Element the name element, and an empty Value.

Then it would hit a node with type XmlNodeType.Text, no name, and the value "Some Text".

Then it would hit a node with the type XmlNodeType.EndElement, the name element and an empty Value.

You're trying to get the name at the point where there is none. Either get the name when you read the element and then later read the text node, or perhaps upon reading the element, and getting its name then immediately call ReadElementContentAsString() to get the contents of inner text node(s).

Upvotes: 10

John Saunders
John Saunders

Reputation: 161821

A Text node has no name, so xmlRead.Name will never return one.

Upvotes: 0

Related Questions