60786

How to use xmlreader to read this xml

My xml section is like this:

<Note> <SpecialText att1="" /> </Note>

Or

<Note> This is a note. </Note>

What I need is to use XmlReader to read the xml, but I am not sure how to determine if the innerXml is another xmlelement or is just text.

I am doing this:

while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { switch (reader.LocalName.ToLower()) { case MMLElement.SpecialText: //// read related attributes break; } } }

but how can I read the content if the thing under the Note is just text. If I use reader.ReadInnerXml, it will read everything, so I won't have chance to see if it is a SpecialText XmlElement or just text?

Many Thanks

Answer1:

Now this may sound condescending, but I think that this is something easily answered by digging in the reference. Then again I may not fully understand your problem. If the following answer is not what you are looking for, just post more details and I'll be glad to help.

To determine if the content is just text, just check for it and then do with it whatever you like:

while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { switch (reader.LocalName.ToLower()) { case MMLElement.SpecialText: //// read related attributes break; } } else if (reader.NodeType == XmlNodeType.Text) { string thisIsjustText = reader.value; } //whatever comes next }

Answer2:

If you use XElement.Load(file), you can then use...

XElement xfile = XElement.Load(file); XElement note = xfile.Path("path/to/note"); if(note.HasElements) // read the element else string text = (string)note;

Note: Get Path() here: https://github.com/ChuckSavage/XmlLib/blob/master/XElementExtensions.cs

Recommend

  • Properly format extracted date time with python [closed]
  • Parsing XML doesn't take backspace in Android
  • Avoid recursion on this function XML related
  • parse the xml by specifying the attribute names
  • SAXParser: handle only specific parent childs
  • How to read XML data using C#
  • How to convert XMLList to XML in FLEX
  • I can't call my method from class to form
  • XML SAX Parser not working - NullPointerException [closed]
  • Error in .jcall()
  • Get XML Attributes in WPF with a TreeView
  • Detect the cell phone connected to wireless network - C#
  • What's the simplest way to parse an XML string in Node.js?
  • Getting the BuildAgent information from a Build
  • Generate Weighted Random Boolean Matrix in Matlab
  • Why my input is showing one thread executing after another thread, not at the same time?
  • Find text(A) and replace it with text(B) + HTML
  • How to get div text also with it's input's values
  • AutoComplete Texbox error - write to protected memory
  • Not getting Segmentation Fault in C
  • NetBeans moving tabs navigation to the right
  • Find by regex and replace match to lowercase in Bash
  • Custom URI routing by query string with CodeIgniter?
  • xpages partial - full search
  • How can I manually set the document id in a corpus?
  • jQuery Mobile - Dialogs without changing hash
  • Insert records if not exist SQL Server 2005
  • if some function is not optimized does it mean that all functions where it is declared are not optim
  • Accessing Rows In A LINQ Result Without A Foreach Loop?
  • Hide HTML elements without javascript, only CSS
  • Copy to all folders batch file?
  • C: Incompatible pointer type initializing
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Moving mysql files across servers
  • How to make Safari send if-modified-since header?
  • Weird JavaScript statement, what does it mean?
  • How to pass list parameters for each object using Spring MVC?
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • JaxB to read class hierarchy
  • Django query for large number of relationships