Why does XmlReader.ReadInnerXmlAsync hang when reading <ns:element>?

一世执手 提交于 2019-12-13 04:38:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!