Parse XML to C# Class

Hi,

I'm trying to parse the following XML response to a C# class:

<Collection name="Living Room" xmlns="http://bacnet.org/csml/1.2">
  <Struct name="Living Room" nodeType="System">
    <String name="location" value="" />
    <String name="address" value="" />
    <String name="description" value="" />
    <String name="template" value="vstatBEC.xml" />
    <Struct name="Objects">
      <Enumerated name="LOCALOVERRIDE" value="inactive" displayName="Living Room Local Override" description="" physical="/.bacnet/22 Quarry Road/400000/binary-value,55" />
      <Real name="TEMP" value="22.0593" displayName="Living Room Space Temp" description="" physical="/.bacnet/22 Quarry Road/400000/analog-input,206001" />
      <Real name="TEMP_SP" value="20.5" displayName="Living Room Occupied Space Setpoint" description="" physical="/.bacnet/22 Quarry Road/400000/analog-value,600" />
    </Struct>
  </Struct>
  <Struct name="Default System Dashboard" nodeType="Other">
    <Enumerated name="Type" value="System" />
    <Enumerated name="Layout" value="2" />
    <Collection name="Widgets">
      <Struct name="1ae623e1-837f-11e3-b067-94de806b511c">
        <String name="Type" value="Point List" />
      </Struct>
      <Struct name="1ae623ed-837f-11e3-b067-94de806b511c">
        <String name="Type" value="Alarm List" />
      </Struct>
      <Struct name="1ae623fb-837f-11e3-b067-94de806b511c">
        <String name="Type" value="Alarm Summary" />
      </Struct>
    </Collection>
  </Struct>
</Collection>

Open in new window


Visual Studio has created the following class from the above XML, but when trying to parse the xml I get an error that the XML is ivalid 1, 2:

   /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(ElementName = "Collection", IsNullable = false)]
    public partial class Collection
    {

        private CollectionStruct[] structField;

        private string nameField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("Struct")]
        public CollectionStruct[] Struct
        {
            get
            {
                return this.structField;
            }
            set
            {
                this.structField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class CollectionStruct
    {

        private CollectionStructEnumerated[] enumeratedField;

        private CollectionStructCollection collectionField;

        private CollectionStructString[] stringField;

        private CollectionStructStruct structField;

        private string nameField;

        private string nodeTypeField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("Enumerated", IsNullable = true)]
        public CollectionStructEnumerated[] Enumerated
        {
            get
            {
                return this.enumeratedField;
            }
            set
            {
                this.enumeratedField = value;
            }
        }

        /// <remarks/>
        public CollectionStructCollection Collection
        {
            get
            {
                return this.collectionField;
            }
            set
            {
                this.collectionField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("String", IsNullable = true)]
        public CollectionStructString[] String
        {
            get
            {
                return this.stringField;
            }
            set
            {
                this.stringField = value;
            }
        }

        /// <remarks/>
        public CollectionStructStruct Struct
        {
            get
            {
                return this.structField;
            }
            set
            {
                this.structField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string nodeType
        {
            get
            {
                return this.nodeTypeField;
            }
            set
            {
                this.nodeTypeField = value;
            }
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class CollectionStructEnumerated
    {

        private string nameField;

        private string valueField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class CollectionStructCollection
    {

        private CollectionStructCollectionStruct[] structField;

        private string nameField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("Struct", IsNullable = true)]
        public CollectionStructCollectionStruct[] Struct
        {
            get
            {
                return this.structField;
            }
            set
            {
                this.structField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class CollectionStructCollectionStruct
    {

        private CollectionStructCollectionStructString stringField;

        private string nameField;

        /// <remarks/>
        public CollectionStructCollectionStructString String
        {
            get
            {
                return this.stringField;
            }
            set
            {
                this.stringField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class CollectionStructCollectionStructString
    {

        private string nameField;

        private string valueField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class CollectionStructString
    {

        private string nameField;

        private string valueField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class CollectionStructStruct
    {

        private object[] itemsField;

        private string nameField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("Enumerated", typeof(CollectionStructStructEnumerated), IsNullable = true)]
        [System.Xml.Serialization.XmlElementAttribute("Real", typeof(CollectionStructStructReal), IsNullable = true)]
        [System.Xml.Serialization.XmlElementAttribute("Unsigned", typeof(CollectionStructStructUnsigned), IsNullable = true)]
        public object[] Items
        {
            get
            {
                return this.itemsField;
            }
            set
            {
                this.itemsField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class CollectionStructStructEnumerated
    {

        private string nameField;

        private string valueField;

        private string displayNameField;

        private string descriptionField;

        private string physicalField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string displayName
        {
            get
            {
                return this.displayNameField;
            }
            set
            {
                this.displayNameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string description
        {
            get
            {
                return this.descriptionField;
            }
            set
            {
                this.descriptionField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string physical
        {
            get
            {
                return this.physicalField;
            }
            set
            {
                this.physicalField = value;
            }
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class CollectionStructStructReal
    {

        private string nameField;

        private decimal valueField;

        private string displayNameField;

        private string descriptionField;

        private string physicalField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public decimal value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string displayName
        {
            get
            {
                return this.displayNameField;
            }
            set
            {
                this.displayNameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string description
        {
            get
            {
                return this.descriptionField;
            }
            set
            {
                this.descriptionField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string physical
        {
            get
            {
                return this.physicalField;
            }
            set
            {
                this.physicalField = value;
            }
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class CollectionStructStructUnsigned
    {

        private string nameField;

        private byte valueField;

        private string displayNameField;

        private string descriptionField;

        private string physicalField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public byte value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string displayName
        {
            get
            {
                return this.displayNameField;
            }
            set
            {
                this.displayNameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string description
        {
            get
            {
                return this.descriptionField;
            }
            set
            {
                this.descriptionField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string physical
        {
            get
            {
                return this.physicalField;
            }
            set
            {
                this.physicalField = value;
            }
        }
    }

Open in new window


Here is my extension method that is parsing the XML from a string

public static T ParseXML<T>(this string @this) where T : class
        {
            var reader = XmlReader.Create(@this.Trim().ToStream(), new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Document });
            return new XmlSerializer(typeof(T)).Deserialize(reader) as T;
        }

Open in new window

LVL 1
wint100Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ioannis ParaskevopoulosCommented:
Hi,

Try adding the namespace on the XmlRootAttribute of the class as following:

    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(ElementName = "Collection", IsNullable = false, Namespace="http://bacnet.org/csml/1.2")]
    public partial class Collection
    {

Open in new window


Giannis
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wint100Author Commented:
Well, would you Adam and Eve it, I've been trying to solve that for days.

Thank you very much.
0
Pravin AsarPrincipal Systems EngineerCommented:
Have you tried XSD.EXE tool, that comes with Visual Studio

The Visual Studio .NET help files describe the Xsd.exe as follows: The XML Schema Definition tool (Xsd.exe) is installed along with the .NET Framework Tools as part of the .NET Framework SDK. The tool is designed primarily for two purposes:

To generate either C# or Visual Basic class files that conform to a specific XML Schema definition language (XSD) schema. The tool takes an XML Schema as an argument and outputs a file that contains a number of classes that, when serialized with the XmlSerializer, conform to the schema.
To generate an XML Schema document from a .dll file or .exe file. If you need to see the schema of a set of files that you have either created or one that has been modified with attributes, pass the DLL or EXE as an argument to the tool to generate the XML schema.

Step 1: Create XSD File
(e.g. xsd.exe People.xml)
You get People.xsd as output/result.

Step 2: Create C# Class from XSD File
(e.g. xsd.exe People.xsd /c)
This will get C# class named People.

You can find more information @ MSDN Site

https://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.110).aspx
1
Pravin AsarPrincipal Systems EngineerCommented:
Based on your XML here is XSD and CS,

I used VS2012,

xsd.exe is available from the 'Developer Command Prompt for VS2012, not the command window that is part of the VS2012 IDE.

The 'Developer Command Prompt for VS2012' itself can be started from Start Menu -> All Programs -> Microsoft Visual Studio 2012 -> Visual Studio Tools -> Visual Studio Command Prompt For VS2012
Collection.xsd
Collection.cs
1
wint100Author Commented:
Thanks for your help, I did use VS2015 to generate the classes but it missed the namespace, once I added this it all worked
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.