Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

Reading XMl

Is there any way by which i can read each node(with attribute and values) as a string? Is there a way to get this using XmlTextReader.

Wednesday, February 16, 2005

Below is a brute force way of going through an XML file...I've done this with custom config files (as opposed to using XPath) when I'm going to have to go through the entire XML string anyway.

The problem is each "Read" reads an aspect of the XML, not a line. So, you'll need to use the xml.NodeType to determine if the node is an attribute, plain text, CDATA, whatever. Then, use the xml.Value and xml.Name to get the information about the node.

StreamReader sr = new StreamReader(Filename);
XmlTextReader xmlr = new XmlTextReader(sr.BaseStream);
while (xmlr.Read())
    //do tests here

Wednesday, February 16, 2005

Small correction: Read() normally skips over attributes. You can use MoveToAttribute to read attributes manually, but it's much easier to use the XmlReader indexer to get the attribute values of the current node.

Chris Nahr
Thursday, February 17, 2005

*  Recent Topics

*  Fog Creek Home