Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. XmlTextReader - try again

XmlTextReader - try again

Scheduled Pinned Locked Moved .NET (Core and Framework)
xmlhelptutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    coleydog
    wrote on last edited by
    #1

    Hi All, My last message post dropped the Xml lines so I'll substitute round brackets. I am reading Xml data using XmlTextReader on a file stream. I read the elements and the element attributes with no problem but I can't figure out how to identify if the current element has further child elements or is finalized (ends with />. I can handle (MyElement Name="MyName" Value="MyValue")(/MyElement) (the reader.Read() shows 'Element' (with 2 attributes) then EndElement - no problem) I have problems with (MyElement Name="MyName" Value="MyValue" /) (the reader.Read() shows 'Element' (with 2 attributes) then the next element) In the second case I can't figure out if the next element is a child element or not. Or to be more precise I don't know when the original element ends. Thanks.

    C M 2 Replies Last reply
    0
    • C coleydog

      Hi All, My last message post dropped the Xml lines so I'll substitute round brackets. I am reading Xml data using XmlTextReader on a file stream. I read the elements and the element attributes with no problem but I can't figure out how to identify if the current element has further child elements or is finalized (ends with />. I can handle (MyElement Name="MyName" Value="MyValue")(/MyElement) (the reader.Read() shows 'Element' (with 2 attributes) then EndElement - no problem) I have problems with (MyElement Name="MyName" Value="MyValue" /) (the reader.Read() shows 'Element' (with 2 attributes) then the next element) In the second case I can't figure out if the next element is a child element or not. Or to be more precise I don't know when the original element ends. Thanks.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      coleydog wrote:

      My last message post dropped the Xml lines so I'll substitute round brackets.

      The 'Ignore HTML tags in this message (good for code snippets)' is good for that. Or if Auto-encode HTML when pasting? is checked ( which it is by default ) copying and pasting your code instead of typing it will do it for you.

      Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      1 Reply Last reply
      0
      • C coleydog

        Hi All, My last message post dropped the Xml lines so I'll substitute round brackets. I am reading Xml data using XmlTextReader on a file stream. I read the elements and the element attributes with no problem but I can't figure out how to identify if the current element has further child elements or is finalized (ends with />. I can handle (MyElement Name="MyName" Value="MyValue")(/MyElement) (the reader.Read() shows 'Element' (with 2 attributes) then EndElement - no problem) I have problems with (MyElement Name="MyName" Value="MyValue" /) (the reader.Read() shows 'Element' (with 2 attributes) then the next element) In the second case I can't figure out if the next element is a child element or not. Or to be more precise I don't know when the original element ends. Thanks.

        M Offline
        M Offline
        Mark J Miller
        wrote on last edited by
        #3

        Use the IsEmptyElement property of the XmlTextReader: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.IO; namespace ReadingXmlText { class Program { static void Main(string[] args) { String data = "<root><standard attributeA=\"value\" attributeB=\"value2\"></standard><emptyElement attributeA=\"value\" attributeB=\"value2\" /></root>"; StringReader stream = new StringReader(data); XmlTextReader rdr = new XmlTextReader(stream); while (rdr.Read()) { Console.Write(rdr.Name + ": "); Console.Write(rdr.NodeType); if (rdr.IsEmptyElement) Console.WriteLine(" (Empty)"); else Console.WriteLine(); } } } }

        Mark's blog: developMENTALmadness.blogspot.com Funniest variable name: lLongDong - spotted in legacy code, was used to determine how long a beep should be. - Dave Bacher

        C 1 Reply Last reply
        0
        • M Mark J Miller

          Use the IsEmptyElement property of the XmlTextReader: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.IO; namespace ReadingXmlText { class Program { static void Main(string[] args) { String data = "<root><standard attributeA=\"value\" attributeB=\"value2\"></standard><emptyElement attributeA=\"value\" attributeB=\"value2\" /></root>"; StringReader stream = new StringReader(data); XmlTextReader rdr = new XmlTextReader(stream); while (rdr.Read()) { Console.Write(rdr.Name + ": "); Console.Write(rdr.NodeType); if (rdr.IsEmptyElement) Console.WriteLine(" (Empty)"); else Console.WriteLine(); } } } }

          Mark's blog: developMENTALmadness.blogspot.com Funniest variable name: lLongDong - spotted in legacy code, was used to determine how long a beep should be. - Dave Bacher

          C Offline
          C Offline
          coleydog
          wrote on last edited by
          #4

          Thanks, That's exactly the property I was looking for. Cheers. :)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups