Example Of Consuming XML Through System.Xml.XmlTextReader

I thought I better post an example of using an System.Xml.XmlTextReader to consume XML because I’m sure I’ll forget how to in the future. Here is the sample code.

StringReader scriptXmlString = new StringReader(SqlScriptXml);
XmlTextReader scriptXmlReader = new XmlTextReader(scriptXmlString);

scriptXmlReader.WhitespaceHandling = WhitespaceHandling.None;

string xmlNodeText;

while (scriptXmlReader.Read())
{

switch (scriptXmlReader.NodeType)
{
case XmlNodeType.Element:

int numAttributes = scriptXmlReader.AttributeCount;
string nodeName = scriptXmlReader.Name;

for (int i = 0; i < numAttributes; i++) { string attributeValue = scriptXmlReader.GetAttribute(i); } break; case XmlNodeType.Text: xmlNodeText = scriptXmlReader.Value; break; case XmlNodeType.EndElement: xmlNodeText = string.Empty; break; default: break; } }[/sourcecode]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s