开发者

How to parse an XML document and call the event handlers (startElement, text), in C# without using the stack to remember the context of the tags?

I have an XML document and I am using XmlTextReader of System.Xml namespace to parse this XML document and I am calling the event handlers for different types of nodes like startElement(), Text etc...

   **while (reader.Read())
    {
        switch (reader.NodeType)
        {
            case XmlNodeType.Element:
            {
                StringBuilder nodeNam = new StringBuilder(reader.Name);
                startElement(nodeNam);**

Here startElement(), text, endElement() etc are my custom created event handlers. I have given the definition for these event handler开发者_如何学JAVAs. In order to remember the context of the tags in the XML document, I am using stack. ie. pushing a tag into stack on startElement() and processing the contents ( calling text()) of that tag by getting the current context from the top of the stack. And later when I encounter the end of that tag I popped that tag. This is how I achieved parsing and coded the event handlers.

But I want to achieve this parsing and event handler creation without using a stack. Can anyone kindly let me know how to achieve this in C # without using stack to remember the current context of the tags of XML Document. Is there any better way to achieve XML parsing and generating event handlers as and when the parser encounters startElement(), endElement(), Text(), startDocument() of the XML Document.

     IN MY XML PARSING, I JUST WANT TO IDENTIFY startElement(), endElement(), characters(), etc, for each and every node (tag) in the document AND CALL THE EVENT HANDLER FUNCTIONS FOR start of an element (startElement() ), end of an element ( endElement() ), text of the node (characters() ). AND I DONT 

need the parsing in much finer detail. Using XmlTextReader I have to use a stack to rememeber the context of the current tag in order to call the corresponding event handler functions (startElement(), endElement(), characters() ). BUT I WANT TO ACHIEVE THIS EVENT HANDLER CALLING WITHOUT USING STACK. Is there any simple method to achieve this in C #.


An alternative for you could be to look at XPath and use this to query your XML document.
It is not clear if such a navigation would be simpler in your case. There are nice tools like MVPXML or Sketchpath which help you to create query statements and test them interactively.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜