我现在的代码看起来像这样。
using (XmlReader r = XmlReader.Create(stream, new XmlReaderSettings() { Async = true }))
{
while (await r.ReadAsync())
{
switch (r.NodeType) {
case XmlNodeType.Element:
if (r.IsEmptyElement) // no attributes
{
OnReceive("<" + r.Name + " />");
}
else // has attributes
{
OnReceive("<" + r.Name + ">");
while (r.MoveToNextAttribute())
{
OnReceive(r.Name + " -> " + r.Value);
}
}
break;
}
}
}
我的XML如下所示。
<?xml version='1.0' encoding='UTF-8'?>
<start>
<a>
<b></b>
<c>
<d>TEXT01</d>
<d>TEXT01</d>
<d>TEXT01</d>
<d>TEXT01</d>
</c>
<e>
<f>TEST01</f>
</e>
<g/>
<h/>
</a>
...
当我点击XML元素<a>
时,我想将它读到最后(直到我点击</a>
),然后用整个元素及其子元素触发事件。
我该怎么办?是否有一些机制允许我像这样读取流(等到元素结束)然后继续使用其他XML部分?
答案 0 :(得分:0)
那对ReadInnerXmlAsync
来说可能是可行的。
switch (r.NodeType) {
case XmlNodeType.Element:
if (r.Name.Equals("a"))
{
string x = await r.ReadInnerXmlAsync();
OnReceive(x);
}
break;
}