C# 来自多个流的一个元素
我需要创建一个将多个像素连接成一个像素的方法。 我创建了以下方法:C# 来自多个流的一个元素,c#,stream,xelement,C#,Stream,Xelement,我需要创建一个将多个像素连接成一个像素的方法。 我创建了以下方法: static void DoStuff(string IP, string login, string password, string port) { CommonMethods cm = new CommonMethods(); WebClient webClient = new WebClient();
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循环中,输出被覆盖。你能给我提供一些将输出连接成一个的解决方案吗?你在每次迭代中都覆盖
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());
}
结果元素可以是加载的输出元素中的第一个。然后,您可以对下一个元素执行一些查询,并将结果添加到结果元素中。如果看不到您正在使用的数据,很难说出更多信息