Pete's information junkyard

XML parsing with XmlTextReader

Example code for using the XmlTextReader for parsing an xml file and process it to a custom object.

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Text;

namespace bla {
public class ReadXml {
public static void Main(string[] args) {
ReadXml rx = new ReadXml();
rx.Read(args[0]);
}

public void Read(string Filename) {
// using (StreamReader fs = new StreamReader(Filename, Encoding.UTF8)) {
using (FileStream fileStream = new FileStream(Filename, FileMode.Open, FileAccess.Read)) {
MyXmlItem xmlfile = ParseFile(fileStream);
Console.WriteLine(xmlfile);
}
}

public MyXmlItem ParseFile(Stream xmlstream) {
XmlTextReader reader = new XmlTextReader(xmlstream);
MyXmlItem item = null;

while (reader.Read()) {
switch (reader.NodeType) {
case XmlNodeType.Element:
Console.WriteLine("ParseFirst: found element '{0}'", reader.Name);
item = ParseElement(reader, 0);
break;
case XmlNodeType.XmlDeclaration:
case XmlNodeType.Whitespace:
case XmlNodeType.Comment:
Console.WriteLine("ParseFirst: skipping node '{0}' ({1})", reader.Name, reader.NodeType);
break;
default:
Console.WriteLine("ParseFirst: Unhandled {1}='{0}'", reader.Name, reader.NodeType);
break;
}

if (item != null)
break;
}

return item;
}

private string Tabs(int ident) {
string res = "";
for (int i=0; i<ident; i++)
res+= "\t";
return res;
}

private MyXmlItem ParseElement(XmlTextReader reader, int ident) {
Console.WriteLine(
"{4}ParseElement:Element name '{0}', IsEmptyElement={1}, HasAttributes={2}, HasValue={3}",
reader.Name,
reader.IsEmptyElement,
reader.HasAttributes,
reader.HasValue,
Tabs(ident)
);

bool isEmpty = reader.IsEmptyElement;
MyXmlItem item = new MyXmlItem();
item.Name = reader.Name;

if (reader.HasAttributes) {
while (reader.MoveToNextAttribute()) {
// Adding attribute
Console.WriteLine("{2}ParseElement:Attribute {0}={1}", reader.Name, reader.Value, Tabs(ident));
item.Attributes.Add(reader.Name, reader.Value);
}
}

if (!isEmpty) {
while (reader.Read()) {
switch (reader.NodeType) {
case XmlNodeType.Text:
Console.WriteLine("{0}ParseElement:Setvalue", Tabs(ident));
item.Value = reader.Value;
break;
case XmlNodeType.EndElement:
Console.WriteLine("{1}ParseElement:EndElement (name={0})", reader.Name, Tabs(ident));
return item;
case XmlNodeType.Element:
Console.WriteLine("{0}ParseElement:Found child element declaration", Tabs(ident));
// found a child element
MyXmlItem childItem = ParseElement(reader, ident+1);
if (childItem != null) {
Console.WriteLine("{0}ParseElement:add child element to current item", Tabs(ident));
item.Childs.Add(childItem);
}
break;
default:
Console.WriteLine("{1}ParseElement:unhandled nodetype={0}", reader.NodeType, Tabs(ident));
break;
}
}
}

return item;
}
}

public class MyXmlItem {
public string Name = string.Empty;
public string Value = null;

public List<MyXmlItem> Childs = new List<MyXmlItem>();
public Dictionary<string, string> Attributes = new Dictionary<string, string>();

public override string ToString() {
return this.ToString(0);
}

public string ToString(int ident) {
StringBuilder sb = new StringBuilder();
for (int i=0; i<ident; i++) {
sb.Append("\t");
}
sb.Append("<").Append(this.Name);

foreach (string key in Attributes.Keys) {
sb.Append(" ").Append(key).Append("=\"").Append(Attributes[key]).Append("\"");
}

sb.Append(">");

if (Childs.Count > 0) {
sb.Append("\n");
foreach (MyXmlItem child in Childs) {
sb.Append(child.ToString(ident+1));
}
//sb.Append("\n");
for (int i=0; i<ident; i++) {
sb.Append("\t");
}
} else if (this.Value != null) {
sb.Append(this.Value);
}

sb.Append("</").Append(this.Name).Append(">\n");

return sb.ToString();
}
}
}

Tags: C# Willow XML
Created on 05-08-2011






stopsoftwarepatents.eu petition banner