How to convert Streamreader data to XmlDocument?

我与影子孤独终老i 提交于 2019-12-31 02:23:28

问题


In C#, I am trying to get call a webservice which returns an XML file.

I can make a HttpWebRequest to the webservice and store the output in a StreamReader. But how can I convert this data into an XMLDocument?


回答1:


Use XmlDocument.Load() - I'm using the overload that accepts an XmlReader to cash in on XmlReader.Create's auto-encoding detection:

XmlDocument document = new XmlDocument();
using(Stream stream = request.GetResponse().GetResponseStream()) {        
    using(XmlReader reader = XmlReader.Create(stream)) {
        document.Load(stream);
    }
}



回答2:


Use System.Xml.Linq.XDocument.Load(streamreader);




回答3:


You should store the received output in a StringWriter or just a stringand the load it using XmlDocument.Load(string).



来源:https://stackoverflow.com/questions/4271618/how-to-convert-streamreader-data-to-xmldocument

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