-
September 23rd, 2009, 05:35 AM
#1
parser
Hello,
I would like wirte a very simple descent parser. I wonder if if I wrote it with SAX library I'll have a descent rec. parser automatically; btw seems that SAX and C# are not good friends; so I wrote the parser in this way:
Code:
try {
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
XmlReader reader = XmlReader.Create(new XmlTextReader(filename), settings);
while (reader.Read()) {
switch (reader.NodeType) {
case XmlNodeType.Element:
Hashtable attributes = new Hashtable();
string strURI = reader.NamespaceURI;
string strName = reader.Name;
Console.Write(strURI + " " + "<" + strName );
if (reader.HasAttributes) {
for (int i = 0; i < reader.AttributeCount; i++) {
reader.MoveToAttribute(i);
attributes.Add(reader.Name, reader.Value);
Console.Write(" " + reader.Name + "=" + "\"" + reader.Value + "\"");
}
Console.WriteLine(">");
}
else {
Console.WriteLine(">");
}
break;
case XmlNodeType.EndElement:
string strNameEND = reader.Name;
Console.WriteLine("</" + strNameEND + ">");
break;
//case XmlNodeType.Text:
default:
break;
}
}
}
catch (XmlException e) {
Console.WriteLine("error occured: " + e.Message);
}
}
The question is:
1. can I change this above to write a descent rec. parser? I can't see how modify it
2. If not the 1st: SAX can help or not?
thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|