I need to create method which will concatenate multiple Xelements into one. I created below method:
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());
}
}
but in Do While loop the output is overwritten. Could you please provide me some solution which will concatenate the output into one?
You are overriding values of output
element on each iteration. Instead create resulting element and add new elements to it on each iteration:
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());
}
Resulting element can be first of loaded output
elements. Then you can do some queries to next elements and add results to resulting element. It's hard to tell more without seeing data you are working with.