【问题标题】:Xdocument.Load is failingXdocument.Load 失败
【发布时间】:2012-03-08 15:29:36
【问题描述】:

我无法加载 Xdocument.Load 我无法加载 Xdocument.Load我无法加载 Xdocument.Load我无法加载 Xdocument.Load我无法加载 Xdocument.Load我无法加载 Xdocument.Load我无法加载加载 Xdocument.Load我无法加载 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.");
        }
    }

我无法使用 XML 提要加载 xdocument.Load。

【问题讨论】:

  • 有什么理由使用XmlTextReader 而不是(比如说)LINQ to XML? (您还应该考虑将大代码分解为更小的方法,更改缩进样式,并使用 StringBuilder 而不是字符串连接。)
  • 我不知道如何使用 linq,这就是我使用 XMLTEXTReader 的原因。我将其更改为字符串生成器而不是串联。
  • 我认为@JonSkeet 的意思是,您不妨重写整个内容。 :)
  • @SmilingLily:使用 LINQ to XML 会更简单。哎呀,使用XmlDocument 会更简单,但是 LINQ to XML 让它变得轻而易举。
  • 你知道我可以改变什么来打印与磁贴相关的作者姓名吗?

标签: c# asp.net xml xmltextwriter xmltextreader


【解决方案1】:

不清楚究竟你想要什么,但我怀疑是这样的:

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();

这将为您提供List&lt;string&gt;,其中每个元素类似于“测试 1:John M. pp”。

【讨论】:

  • 我创建了一个新的 xml 文档因为我不想显示真实数据。我可能打错了,但我的代码已经过大小写敏感性检查。所以这不是问题。感谢您的答复。我尝试了您上面列出的代码,但出现了一些错误。在下面粘贴代码,以便您可以看到红线。
  • @SmilingLily:评论只是纯文本。我们看不到红线。如果您使您的实际数据与您的真实数据相对应,它真的会有所帮助......值不必相同,但如果我们看不到元素名称是...
  • @SmilingLily:响应response.GetResponseStream()
  • @SmilingLily:正如我所建议的,您仍在创建XmlReader。你为什么这样做?
  • @SmilingLily:那么你需要以不同的方式处理它,使用Elements 而不是Element。您的问题没有说明您希望在这种情况下发生什么。你必须学会​​更准确地提出你的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-26
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多