locked
Operating on multiple objects from XML RRS feed

  • Question

  • User-1458727574 posted

    Hi,

    I have some example XML:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <customers>
      <customer>
    	<firstname>John</firstname>
    	<lastname>Smith</lastname>
    	<email>johnsmith@email.com</email>
      </customer>
      <customer>
    	<firstname>Fred</firstname>
    	<lastname>Bloggs</lastname>
    	<email>FredBloggs@email.com</email>
      </customer>
      <customer>
    	<firstname>Jane</firstname>
    	<lastname>Doe</lastname>
    	<email>janedoe@email.com</email>
      </customer>
    </customers>
      

    I can generate a couple of classes for it, a customers class and an individual customer class. When I load the XML and deserialise it into a customers class, it picks up a list of customers, each in their own customer object. All I want to do is iterate through them and delete the email string. FYI, this is not my full code, rather this is just an example as the full code is a little larger. I'm wrestling with something like a foreach statement that loops through all the individual customer objects with the customers object.

    Thursday, June 28, 2018 3:04 PM

Answers

  • User-1458727574 posted

    This sorted it:

                for (int i = 0; i < vendors.Items.Length; i++)
    

    No idea why I forgot this basic bit of looping code. Just got my brain latched onto using a foreach loop.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 28, 2018 3:55 PM

All replies

  • User475983607 posted

    Pretty simple if you have an array of Customers.  Not sure of your type case but a foreach loop should work fine.

    foreach(Customer c in Customers)
    {
        c.Email = string.Empty;
    }

    Thursday, June 28, 2018 3:25 PM
  • User-1458727574 posted

    ok, now I get an error message of use.

    foreach statement cannot operate on variables of type Customers because Customers does not contain a public instance definition for GetEnumerator

    Thursday, June 28, 2018 3:38 PM
  • User753101303 posted

    Hi,

    The problem is really on something such as :

    foreach (var item in data)
    {
       item.email = "";
    }
    

    One could provide a full sample but IMO it is always better to tell the exact problem you have :
    - it avoid for those trying to help to spend a bit of time on a sample for a part of your code that works already
    - it could fail for some reason we don't imagine and it could even not help at all

    Sometimes knowing the error message allows to understand right now which wrong thing happens on your side (or one can try to repro your issue and make sure to first run into the same issue before trying a fix).

    Edit: an so the definition for Customers is ? I was assuming a List<Customer> for example...

    Thursday, June 28, 2018 3:40 PM
  • User-1458727574 posted

    Ok, being dumb now. I can just loop around the number of customers in customer by getting the length of customers. Been a long day :/

    Thursday, June 28, 2018 3:45 PM
  • User753101303 posted

    It depends on which type is used. foreach works with "enumerable" data structures ie any data structure you can "browse" by going from a "current" element to the next.

    For now you are using a data structure that doesn't support that so you have to explicitely fetch each element by using its index. If you need furrther help tell how what is the definition for your "Customers"  type.

    Thursday, June 28, 2018 3:50 PM
  • User-1458727574 posted

    This sorted it:

                for (int i = 0; i < vendors.Items.Length; i++)
    

    No idea why I forgot this basic bit of looping code. Just got my brain latched onto using a foreach loop.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 28, 2018 3:55 PM
  • User753101303 posted

    Despite its name, vendors seems to be an object with an Items list property rather than a list itself so ;

    foreach(var item in vendors) likely fails

    foreach(var item in vendors.Items) could perhaps work.

    Once again it all depends on the actual type you are using.. It needs to be a list, an array etc... anything that is exposing the https://msdn.microsoft.com/en-us/library/system.collections.ienumerable(v=vs.110).aspx interface.

    Edit:

    var a = new List<int> { 1, 2, 3 };
    var b = new int[] { 1, 2, 3 };
    foreach (var item in a) Console.WriteLine(item); // works
    foreach (var item in b) Console.WriteLine(item); // works
    
    var o = new Object();
    foreach (var item in o){ } // doesn't compile
    

    Thursday, June 28, 2018 4:05 PM