XmlTextReader - try again
-
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.
-
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.
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 )
-
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.
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
-
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