locked
XML into a List RRS feed

  • Question

  • User-284642143 posted

    I have the below XSD (which is a snippet of the entire XSD)

      <xs:include schemaLocation="storeexport.xsd"/>
      <xs:element name="stores">
        <xs:annotation>
          <xs:documentation> Executed API call. List of stores. </xs:documentation>
        </xs:annotation>
        <xs:complexType>
          <xs:sequence>
            <xs:element name="store" maxOccurs="unbounded">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="name" type="xs:string">
                    <xs:annotation>
                      <xs:documentation> Store name </xs:documentation>
                    </xs:annotation>
                  </xs:element>
                  <xs:element name="companyid" type="xs:int">
                    <xs:annotation>
                      <xs:documentation> Company ID </xs:documentation>
                    </xs:annotation>
                  </xs:element>
                  <xs:element name="storeid" type="xs:int">
                    <xs:annotation>
                      <xs:documentation> Store ID. </xs:documentation>
                    </xs:annotation>
                  </xs:element>
                  <xs:element name="directurl" type="xs:anyURI">
                    <xs:annotation>
                      <xs:documentation> direct url for specific details </xs:documentation>
                    </xs:annotation>
                  </xs:element>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>

    Which results in the below XML

     <?xml version="1.0" encoding="utf-8"?>
    <stores>
      <store>
        <name>str1234</name>
        <companyid>123</companyid>
        <storeid>123</storeid>
        <directurl>http://www.xsd2xml.com</directurl>
      </store>
    </stores>

    That XML converted to C# class

    namespace Xml2CSharp
    {
    	[XmlRoot(ElementName="store")]
    	public class Store {
    		[XmlElement(ElementName="name")]
    		public string Name { get; set; }
    		[XmlElement(ElementName="companyid")]
    		public string Companyid { get; set; }
    		[XmlElement(ElementName="storeid")]
    		public string Storeid { get; set; }
    		[XmlElement(ElementName="directurl")]
    		public string Directurl { get; set; }
    	}
    
    	[XmlRoot(ElementName="stores")]
    	public class Stores {
    		[XmlElement(ElementName="store")]
    		public Store Store { get; set; }
    	}
    
    }

    Now judging by the name "StoreS", I'm expecting more than 1 store (only 1 store being returned so far). So i was expecting some kind of list to iterate through - possibly using a foreach but my attempts to iterate through fail with the error "foreach statement cannot operate on variables of type 'Stores' because 'Stores' does not contain a public definition for 'GetEnumerator'" - Should i need to manually create a List above? if so how would i do this as i tried replicating the above structure and adding a List<Store> but that didnt show any results.

    Thanks 

    Friday, September 20, 2019 10:05 AM

Answers

  • User475983607 posted

    The Store property need to be a collection.

        [XmlRoot(ElementName = "stores")]
        public class StoresArray
        {
            public List<Store> Stores { get; set; }
        }

    Or just deserialize a List<T> type...

    List<Store> Stores

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 20, 2019 10:40 AM
  • User475983607 posted

    EssCee

    Although you have added this into another class - is this the correct way (or just a demo) as i just replaced the existing property within the same class?

    I would never wrap a single collection with a class.  I would just use List<T> as explained above.  I provided the class pattern as an example.  Usually the class has other members like...

        
        public class MyClass
        {
            public List<Store> Stores { get; set; }
    pubic int RegionId {get; set;} }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 20, 2019 10:56 AM

All replies

  • User475983607 posted

    The Store property need to be a collection.

        [XmlRoot(ElementName = "stores")]
        public class StoresArray
        {
            public List<Store> Stores { get; set; }
        }

    Or just deserialize a List<T> type...

    List<Store> Stores

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 20, 2019 10:40 AM
  • User-284642143 posted

    You just beat me to it :-) What i had was different property names and same Element names which was causing the issue. Having one resolved it which is essentially what you posted.

    Although you have added this into another class - is this the correct way (or just a demo) as i just replaced the existing property within the same class?

    Friday, September 20, 2019 10:51 AM
  • User475983607 posted

    EssCee

    Although you have added this into another class - is this the correct way (or just a demo) as i just replaced the existing property within the same class?

    I would never wrap a single collection with a class.  I would just use List<T> as explained above.  I provided the class pattern as an example.  Usually the class has other members like...

        
        public class MyClass
        {
            public List<Store> Stores { get; set; }
    pubic int RegionId {get; set;} }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 20, 2019 10:56 AM