Results 1 to 2 of 2

Thread: XmlTextReader in C#

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default XmlTextReader in C#

    Hello,
    I must write a little parser (or scanner) for my own tag language; an example of it:
    Qt Code:
    1. <html>
    2. <head></head>
    3. <body>
    4. <using xmlns:namespace="myfile" />
    5. myText
    6. <p><namespace:myComponent Attribs="val"> //it crashes on 'namespace:myCOmponent'
    7. </body>
    8. </html>
    To copy to clipboard, switch view to plain text mode 
    As you can see there is a namaspace that is declared in a my own way (using the keyword 'using'); now, using XmlReader like it is, doens't work (exception occurs since standard XML needs a different namespace declaration);
    so I must to know if there is a way to cope this problem; here below my parser and an attempt to cope that namespace (but it doesn't work at all):
    Qt Code:
    1. public Parser(string fileContent) {
    2. _settings.ConformanceLevel = ConformanceLevel.Fragment;
    3. _settings.IgnoreWhitespace = true;
    4. _settings.IgnoreComments = true;
    5. _textReader = XmlReader.Create(fileContent, _settings);
    6. _textReader.Read();
    7. XmlNodeType nType = _textReader.NodeType;
    8. while (_textReader.Read()) {
    9. switch (_textReader.NodeType) {
    10. case XmlNodeType.Element: // The node is an element.
    11. Console.Write("<" + _textReader.Name);
    12. Console.WriteLine(">");
    13. if (_textReader.Name == "using") {
    14. while (_textReader.MoveToNextAttribute()) { //attempt to do that
    15. if (_textReader.Prefix == "xmlns") {
    16. Console.WriteLine("<{0}:{1}>", _textReader.Prefix, _textReader.LocalName);
    17. nsmanager = new XmlNamespaceManager(_textReader.NameTable);
    18. nsmanager.AddNamespace(_textReader.LocalName, "mynamespace");
    19. }
    20. }
    21. break;
    22. case XmlNodeType.Text: //Display the text in each element.
    23. Console.WriteLine(_textReader.Value);
    24. break;
    25. case XmlNodeType.EndElement: //Display the end of the element.
    26. Console.Write("</" + _textReader.Name);
    27. Console.WriteLine(">");
    28. break;
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by mickey; 25th June 2010 at 17:43.
    Regards

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: XmlTextReader in C#

    XML is widely used because it is extensively standardized. Standards are good for you and everyone else for just this reason.

    The solution here would be to use standards compliant XML. If you want to depart from XML, you won't be able to use an XML parser. Feel free to invent your own markup language, and write your own parser for it.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.