3

I have am trying to create a class which gets a small xml file from a directory and makes it into a object, I always have problems with attributes. I would like the class to have a list of dimensions so that it can be called like. Dimension[x].id, Dimension[x].name....etc. The class will be referred to many times, but I would like it to only fill the xml to an object the first time.

Here is the XML, and thank you in Advanced:

    <?xml version="1.0"?>
     <dimensions>
      <dimensions id="0" name="Test"  serverAddress = "cm.dt.funcom.com" port = "7509" />
      <dimensions id="1" name="Atlantean"  serverAddress = "cm.d1.funcom.com" port = "7501" />
      <dimensions id="2" name="Rimor"  serverAddress = "cm.d2.funcom.com" port = "7502" />
     </dimensions>
5
  • 1
    What have you tried? Where did you look? What happens when you enter "Converting XML into a C# object" in the StackOverflow search box? Commented Feb 21, 2013 at 19:38
  • as i mentioned above i have issues with attributes. I can deseriize this easy if it wasnt for attributes. I have gotten used to json too much lately Commented Feb 21, 2013 at 20:54
  • Please see this post regarding "How to correctly implement IXmlSerializable" stackoverflow.com/questions/5590592/… Commented Feb 21, 2013 at 21:13
  • But what is your exact "issue with attributes"? Commented Feb 21, 2013 at 21:23
  • My issue is not being able to serialize them correctly into an object. I am not used to multiple attributes Commented Feb 21, 2013 at 22:57

1 Answer 1

3

I would try something that should take away the attribute anxiety.

Generate (or use an existing) XSD that would work for your XMLs. Something such as this:

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="dimensions">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element maxOccurs="unbounded" name="dimensions">
          <xsd:complexType>
            <xsd:attribute name="id" type="xsd:unsignedByte" use="required" />
            <xsd:attribute name="name" type="xsd:string" use="required" />
            <xsd:attribute name="serverAddress" type="xsd:string" use="required" />
            <xsd:attribute name="port" type="xsd:unsignedShort" use="required" />
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

Using the xsd.exe tool available with .NET (a Visual Studio command prompt would set the path nicely for you) generate classes (assume the above XSD is saved as converting-xml-into-a-c-sharp-object.xsd):

xsd /c <fullpath-if-not-in-the-current-folder>converting-xml-into-a-c-sharp-object.xsd

The generated code would be something like (just the header):

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.17929
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.0.30319.1.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class dimensions {

    private dimensionsDimensions[] dimensions1Field;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("dimensions")]
    public dimensionsDimensions[] dimensions1 {
        get {
            return this.dimensions1Field;
        }
        set {
            this.dimensions1Field = value;
        }
    }
}

The next step is to write code that references this generated class; there are more than many references you could find; an introduction could be one such as this on SO.

The above XSD ("Russian Doll") authoring style gives names to classes that may not look smart; here is a possible way around it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you :) That was a bundle of help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.