问题
I encounter strange behavior of the XmlReader.ReadInnerXmlAsync()
method. While the following code works...
using (XmlReader r = XmlReader.Create(stream, new XmlReaderSettings() { Async = true }))
{
while (await r.ReadAsync())
{
switch (r.NodeType) {
case XmlNodeType.Element:
if (r.Name.Equals("c"))
{
string x = await r.ReadInnerXmlAsync();
OnReceive("[ " + x + " ]");
}
break;
}
}
}
...and the whole element <c></c>
from the following XML is read as string.
<?xml version='1.0' encoding='UTF-8'?>
<namespace:open>
<namespace:a>
<b></b>
<c>
<d>TEXT01</d>
<d>TEXT01</d>
<d>TEXT01</d>
<d>TEXT01</d>
</c>
<e>
<f>TEST01</f>
</e>
<g/>
<h/>
</namespace:a>
...
I cannot read <namespace:a>
with the same code (r.Name.Equals("namespace:a")
). Why? The code just blocks at string x = await r.ReadInnerXmlAsync();
and I know for sure that the data arrives in seconds.
Is it because of the fact that "If the reader is positioned on a leaf node, calling ReadInnerXml is equivalent to calling Read." (which blocks until more data is sent) which is documented at http://msdn.microsoft.com/de-de/library/system.xml.xmlreader.readinnerxml.aspx? How do I get around this?
How do I read inner or outer XML without having to wait for more XML data?
UPDATE:
I found one solution.
XmlDocument doc = new XmlDocument();
doc.Load(r.ReadSubtree());
doc.DocumentElement.OuterXml;
Maybe someone will provide something more elegant.
回答1:
I found one solution.
XmlDocument doc = new XmlDocument();
doc.Load(r.ReadSubtree());
doc.DocumentElement.OuterXml;
Maybe someone will provide something more elegant.
来源:https://stackoverflow.com/questions/18231945/why-does-xmlreader-readinnerxmlasync-hang-when-reading-nselement