XmlReader and NodeTypes
I have an XML file I need to read from to get some text values. I am having trouble with the reader.NodeType = XMLNodeType.Text and understanding what constitutes a node to be of NodeType.Text.
My XML reader is only picking up NodeType = Element and ignoring any text in between the node declarations.
Sample XML:
<?xml version="1.0" encoding="utf-8"?>
<myxmlfile>
<ActivitiesHelp>
<helptext>Some text goes here I need to read.</helptext>
</ActivitiesHelp>
</myxmlfile>
And my code looks like this:
elementName = formName.Replace(" ", "");
while (reader.Read())
{
// when we find an element node, we remember its name
if ((reader.NodeT开发者_StackOverflow社区ype == XmlNodeType.Element) && (reader.Name == elementName))
{
reader.Read();
//if the next node = 'helptext' then get that text data
if (reader.Name == "helptext")
{
// for text nodes...get the text values
if ((reader.NodeType == XmlNodeType.Text) && (reader.HasValue))
{
formMessage = reader.Value;
}
}
}
}
The if statement for Nodetype == XmlNodeType.Text
never hits true.
What am I doing wrong, and how do you specify a nodetype of Text in XML file?
Don't you need to do this:
if (reader.Name == "helptext")
{
reader.Read();
if ((reader.NodeType == XmlNodeType.Text) && (reader.HasValue))
{
formMessage = reader.Value;
}
}
i.e. the helptext node is an Element, the text it's childnode and is of type TextNode
精彩评论