Reshma
Reshma

Reputation: 904

To return the multiple values from for loop

I have parsed the xml document and used a for loop to loop for getting different values in string, but when I try to return the value I get only the last value obtained, I want to return all the individual values so that I can store that values in any file format, Below is my code,

XmlDocument xmlDOC = new XmlDocument();
       xmlDOC.LoadXml(periodID_Value_Before_OffSet); // string storing my XML 
       var value = xmlDOC.GetElementsByTagName("value");
       var xmlActions = new string[value.Count];
       string values = "";
       string Period1 = "";
       string periodlevel_period1 = "";
       var listOfStrings = new List<string>();
       string modified_listofstrings = listOfStrings.ToString();
       string arrayOfStrings = ""; 
       for (int i = 0; i < value.Count; i++)
       {
           var xmlAttributeCollection = value[i].Attributes;
           if (xmlAttributeCollection != null)
           {
               var action = xmlAttributeCollection["periodid"];
               xmlActions[i] = action.Value;
               values += action.Value + ",";
               string vals = values.Split(',')[1];
               string counts = values;
               string[] periods = counts.Split(',');
               Period1 = periods[i];
               // periodlevel_period1 = Client.GetAttributeAsString(sessionId, Period1, "name", "");
                  modified_listofstrings = Client.GetAttributeAsString(sessionId, Period1, "name", "");
                  modified_listofstrings.ToArray().ToString();
                  //listOfStrings = periodlevel_period1;
               }
           }
    return modified_listofstrings;

This modified_listofstrings string only return last on value, I want to return the array of the values all obtained while looping.

----------Updated question---------- below is my Sample XMl

   <string xmlns="http://tempuri.org/">
   <ResultSetHierarchy totalResultsReturned="1" totalResults="1" firstIndex="0" maxCount="-1"> 
   <object id="SC.1938773693.238"> 
     <measure.values> 
       <series id="SC.1938773693.108280985"> 
       <value periodid="SC.1938773693.394400760" value="17" /> 
       <value periodid="SC.1938773693.1282504058" value="15" /> 
       <value periodid="SC.1938773693.1631528570" value="13" />
       </series>
       </object> 
   </ResultSetHierarchy>
 </string>

I want output as "SC.1938773693.394400760":"17" and so on for all periodid

Upvotes: 0

Views: 1411

Answers (2)

Vimal CK
Vimal CK

Reputation: 3563

Based on the provided information I have updated the answer.

List<string> items = new List<string>();
XmlDocument xmlDOC = new XmlDocument();
xmlDOC.Load(@"E:\Delete Me\ConsoleApplication1\ConsoleApplication1\bin\Debug\List.xml");
var elements = xmlDOC.GetElementsByTagName("value");

foreach (var item in elements)
{
   XmlElement value = (XmlElement)item;
   items.Add(string.Format("{0}:{1}", value.GetAttribute("periodid"), value.GetAttribute("value")));
}

Upvotes: 2

Baldrick
Baldrick

Reputation: 11840

It looks like you're trying to:

  1. Load an XmlDocument
  2. Get a list of all the attributes of name 'periodid'
  3. Look each periodid up using a webservice call
  4. Return a list of the lookup results

If that is correct, the following method should do what you need:

    public List<string> GetListOfData()
    {
        XmlDocument xmlDOC = new XmlDocument();

        xmlDOC.LoadXml("<Html><value periodid='Yabba'>YabbaValue</value><value periodid='Dabba'>DabbaValue</value><value periodid='Doo'>DooValue</value></Html>"); // string storing my XML 

        var value = xmlDOC.GetElementsByTagName("value");

        var listOfStrings = new List<string>();
        for (int i = 0; i < value.Count; i++)
        {
            var xmlAttributeCollection = value[i].Attributes;

            if (xmlAttributeCollection != null)
            {
                var action = xmlAttributeCollection["periodid"];
                string Period1 = action.Value;
                listOfStrings.Add(QPR_webService_Client.GetAttributeAsString(sessionId, Period1, "name", "") + ":" + value[i].InnerText);
            }
        }
        return listOfStrings;
    }

Upvotes: 0

Related Questions