Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XmlTextReader get values of elements

Status
Not open for further replies.

timothy

Programmer
Mar 20, 2000
39
US
All I want to do is get the value of the element is the following piece of code...

if(System.IO.File.Exists(m_appConfig))
{
XmlTextReader MyReader = new XmlTextReader(m_appConfig);
MyReader.Read();
while(MyReader.Read()){
MyReader.MoveToElement();
if(MyReader.NodeType==XmlNodeType.Element){
AppPaths.Add(MyReader.Name,MyReader.Value);
}
}
MyReader.Close();
}

Problem is "Value" is an empty string. A few more iterations and "Text" NodeType shows the value. Can't I get it all at once? Please let me know what you think.

Thanks for your thoughts.
 
That's by design.

If an element has a value, the W3C spec says that the value actually isn't in the element node itself, but is in a TextNode attached to it as a child node.

If you're using XPath to get it, you can use the text() function to get it.

If you are using a DOM, you can use the InnerText property as long as there aren't any other text nodes underneath it. Otherwise you should examine the ChildNodes collection for a node of type XmlText.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Thanks for your help.

I ended up with this...

if(System.IO.File.Exists(m_appConfig)){
string name = "";
string val = "";

XmlTextReader MyReader = new XmlTextReader(m_appConfig);
MyReader.Read();
while(MyReader.Read()){
MyReader.MoveToElement();
val = MyReader.Value;
if(MyReader.NodeType==XmlNodeType.Element){
name=MyReader.Name;
MyReader.Read();
val = MyReader.Value;
m_appPaths.Add(name,val);
}
}
MyReader.Close();
}


I don't like it, but it works.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top