简体   繁体   English

Xdocument.Load失败了

[英]Xdocument.Load is failing

I am unable to load Xdocument.Load I am unable to load Xdocument.LoadI am unable to load Xdocument.LoadI am unable to load Xdocument.LoadI am unable to load Xdocument.LoadI am unable to load Xdocument.LoadI am unable to load Xdocument.LoadI am unable to load Xdocument.LoadI am unable to load Xdocument.Load 我无法加载Xdocument.Load我无法加载Xdocument.Load我无法加载Xdocument.Load我无法加载Xdocument.Load我无法加载Xdocument.Load我无法加载Xdocument.Load我无法加载Xdocument。 LoadI无法加载Xdocument.Load我无法加载Xdocument.Load

   public void AuthorNames(string Uri)
    {

        CredentialCache credentialCache = new CredentialCache();
        credentialCache.Add(
        new Uri("https://www.RESTWEBSERVICESSITE.com"),
        "Basic",
        new NetworkCredential("USERID", "PWD"));


        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Uri);
        request.AllowAutoRedirect = true;
        request.PreAuthenticate = true;
        request.Credentials = credentialCache;
        request.AutomaticDecompression = DecompressionMethods.GZip;

        try
        {
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {

                XmlReader responseReader = XmlReader.Create(response.GetResponseStream());

                //XmlDocument doc = new XmlDocument();

                **XDocument docs = XDocument.Load();**

               // responseReader.Read();
                //XDocument docs = XDocument.Load(response.GetResponseStream());


          List<string> books = docs.Descendants("INTEL")
          // Not really necessary, but makes it simpler
        .Select(x => new {
           Title = (string) x.Element("TITLE"),
           Author = x.Element("INTEL_AUTH")
           })
      .Select(x => new {
           Title = x.Title,
           FirstName = (string) x.Author.Element("FNAME"),
           MiddleInitial = (string) x.Author.Element("MNAME"),
           LastName = (string) x.Author.Element("LNAME"),
        })
   .Select(x => string.Format("{0}: {1} {2} {3}",
                           x.Title,
                           x.FirstName, x.MiddleInitial, x.LastName))
   .ToList();

   for (int i = 0; i < books.Count; i++)
   {
    for (int j = 0; j < books.Count; j++)
    {
    Response.Write("--" + books[i] + "---" + books[j]);
    }
   }

} }

        }
        catch (Exception ex)
        {
            Response.Write("Remote server Returned an Error.");
        }
    }

I am unable to load xdocument.Load with the XML feed. 我无法使用XML Feed加载xdocument.Load。

It's not clear exactly what you want, but I suspect it's something like this: 目前尚不清楚想要什么,但我怀疑它是这样的:

XDocument doc = ...; // However you want to load this.
// Note: XML is case-sensitive, which is one reason your code failed before
List<string> books = doc
    .Descendants("Intel")
    // Not really necessary, but makes it simpler
    .Select(x => new {
               Title = (string) x.Element("Title"),
               Author = x.Element("Intel_auth")
            })
    .Select(x => new {
               Title = x.Title,
               FirstName = (string) x.Author.Element("fname"),
               MiddleInitial = (string) x.Author.Element("mname"),
               LastName = (string) x.Author.Element("lname"),
            });
    .Select(x => string.Format("{0}: {1} {2} {3}",
                               x.Title,
                               x.FirstName, x.MiddleInitial, x.LastName))
    .ToList();

This will give you a List<string> where each element is something like "Test 1: John M. pp". 这将给你一个List<string> ,其中每个元素都是“Test 1:John M. pp”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM