如何使用xmlreader读取这个xml [英] How to use xmlreader to read this xml
问题描述
我的xml部分是这样的:
My xml section is like this:
<Note>
<SpecialText att1="" />
</Note>
或者
<Note>
This is a note.
</Note>
我需要的是使用 XmlReader 来读取 xml,但我不确定如何确定 innerXml 是另一个 xmlelement 还是只是文本.
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.
我正在这样做:
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.LocalName.ToLower())
{
case MMLElement.SpecialText:
//// read related attributes
break;
}
}
}
但是如果注释下的内容只是文本,我如何阅读内容.如果我使用 reader.ReadInnerXml,它会读取所有内容,所以我没有机会看到它是 SpecialText XmlElement 还是只是文本?
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?
非常感谢
推荐答案
如果你使用 XElement.Load(file)
,那么你可以使用...
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;
注意:在此处获取 Path():https://github.com/ChuckSavage/XmlLib/blob/master/XElementExtensions.cs
Note: Get Path() here: https://github.com/ChuckSavage/XmlLib/blob/master/XElementExtensions.cs
这篇关于如何使用xmlreader读取这个xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!