
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