I have some code:
WebRequest request = HttpWebRequest.Create(url);
WebResponse response = request.GetResponse();
using (System.IO.StreamReader sr =
new System.IO.StreamReader(response.GetResponseStream()))
{
System.Xml.Linq.XDocument doc = new System.Xml.Linq.XDocument();
doc.Load(new System.IO.StringReader(sr.ReadToEnd()));
}
I can’t load my response in my XML document. I get the following error:
Member 'System.XMl.Linq.XDocument.Load(System.IO.TextReader' cannot be accessed
with an instance reference; qualify it with a type name instead.
This is becoming really frustrating. What am I doing wrong?
Unlike
XmlDocument.Load,XDocument.Loadis a static method returning a newXDocument:It seems pretty pointless to read the stream to the end then create a
StringReaderthough. It’s also pointless creating theStreamReaderin the first place – and if the XML document isn’t in UTF-8, it could cause problems. Better:For .NET 4, where there’s an
XDocument.Load(Stream)overload:For .NET 3.5, where there isn’t:
Or alternatively, just let LINQ to XML do all the work:
EDIT: Note that the compiler error did give you enough information to get you going: it told you that you can’t call
XDocument.Loadasdoc.Load, and to give the type name instead. Your next step should have been to consult the documentation, which of course gives examples.