【问题标题】:One XElement from multiple streams来自多个流的一个 XElement
【发布时间】:2013-12-10 15:24:34
【问题描述】:

我需要创建将多个 Xelement 连接为一个的方法。 我创建了以下方法:

        static void DoStuff(string IP, string login, string password, string port)
        {
            CommonMethods cm = new CommonMethods();            
            WebClient webClient = new WebClient();
            XElement output = null;

            try
            {
                webClient = cm.ConnectCiscoUnityServerWebClient(IP, login, password);
                int rowsPerPage = 100;
                int pageNumber = 1;
                Console.WriteLine("Getting logins from " + IP);

                do
                {
                    Console.WriteLine("Downloading " + pageNumber + " page");
                    string uri = @"https://" + IP + ":" + port + "/vmrest/users?bla&rowsPerPage=" + rowsPerPage + "&pageNumber=" + pageNumber;
                    Stream stream = webClient.OpenRead(uri);
                    output = XElement.Load(stream);                    
                    pageNumber++;
                }
                while (output.HasElements);

                Console.WriteLine(output);
            }
            catch (Exception ex)
            {
                cm.LogErrors(ex.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name.ToString());
            }
        }

但在 Do While 循环中,输出被覆盖。您能否提供一些将输出连接成一个的解决方案?

【问题讨论】:

    标签: c# stream xelement


    【解决方案1】:

    您将在每次迭代中覆盖 output 元素的值。而是创建结果元素并在每次迭代时向其添加新元素:

    CommonMethods cm = new CommonMethods();            
    WebClient webClient = new WebClient();
    XElement output = null;
    
    try
    {
        webClient = cm.ConnectCiscoUnityServerWebClient(IP, login, password);
        int rowsPerPage = 100;
        int pageNumber = 1;
        Console.WriteLine("Getting logins from " + IP);
        XElement result = new XElement("result"); // will hold results
    
        do
        {
            Console.WriteLine("Downloading " + pageNumber + " page");
            string uri = @"https://" + IP + ":" + port + 
               "/vmrest/users?bla&rowsPerPage=" + 
               rowsPerPage + "&pageNumber=" + pageNumber;
    
            Stream stream = webClient.OpenRead(uri);
            output = XElement.Load(stream);
            result.Add(output); // add current output to results
            pageNumber++;
        }
        while (output.HasElements);
    
        Console.WriteLine(result);
    }
    catch (Exception ex)
    {
        cm.LogErrors(ex.ToString(), MethodBase.GetCurrentMethod().Name.ToString());
    }
    

    结果元素可以是第一个加载的output 元素。然后您可以对下一个元素进行一些查询并将结果添加到结果元素。如果没有看到您正在使用的数据,很难说出更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      • 2020-12-28
      • 1970-01-01
      • 1970-01-01
      • 2015-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多