2

In this previously answered question, I found how to declare a class that could be serializable and I'm currently mimicking it for my own purposes. I have a further question in that, how would I instantiate the 'Adressess' class and assign parameters to it? Specifically, I'm unsure how to tie data to the 'MainAddress' class, and have it tied to the overall 'Adressess' class. I'm sort of a beginner to object oriented programming.

Here's the class I'm working with:

[XmlRoot("Adressess")]
public class Adressess
{
    [XmlElement(ElementName = "MainAddress")]
    public MainAddress Main { get; set; }

    [XmlElement(ElementName = "AdditionalAddress")]
    public List<AdditionalAddress> AdditionalAddresses { get; set; }
}

[XmlRoot("MainAddress")]
public class MainAddress 
{
    public string Country { get; set; }
    public string City { get; set; }
}

[XmlRoot("AdditionalAddress")]
public class AdditionalAddress
{
    public string Country { get; set; }
    public string City { get; set; }
}

I basically just want to test that I can make an XML file, but I don't know how to assign data to this class before serializing it.

---EDIT---

Thank you DStanley for the quick reply! I was able to build out my approach and generate the XML file I was hoping for. And thank you Mark S for the descriptive answer! I've seen your type of approach before but now it makes sense in my context. I'm going to try and build out your version for it seems much more stable and flexible.

5
  • 1
    Why do you have different classes for MainAddress and AdditionalAddress? Commented Jun 25, 2014 at 17:23
  • Agreed with @DStanley, no need for multiple address classes here. The abovementioned answer is a rather bad example, especially for a beginner. Commented Jun 25, 2014 at 17:43
  • What do you mean when you say, "set the parameters"? Commented Jun 25, 2014 at 18:06
  • The 'AdditionalAddress' class is part of this example, but I'm using it just because it illustrated that I had multiple classes with differing names (and in mine, I'll have differing properties). I agree that there'd be no need to have different classes with the same properties. Commented Jun 25, 2014 at 19:06
  • By 'set the parameters', I meant assign an instance data to hold. I may have used the word parameters wrong. Commented Jun 25, 2014 at 19:09

2 Answers 2

1

Here's what I did. I started by taking your core requirements, an XML document with a root node of <Addresses>, a single required <Main> address field and 0 to many <AdditionalAddress> fields. I also constrained your Address class into a complex type. The resulting XML schema, which I called AddressSchema.xsd looks like this:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/XMLSchema.xsd"
    xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:element name="Addresses">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Main" type="Address" minOccurs="1" maxOccurs="1" />
        <xs:element name="AdditionalAddresses" type="Address" minOccurs="0" maxOccurs="unbounded" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="Address">
    <xs:sequence>
      <xs:element name="Country" type="xs:string" />
      <xs:element name="City" type="xs:string" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>

So now you have your schema defined, I created a C# class from it with XSD.exe in a Visual Studio command prompt:

xsd /c AddressSchema.xsd

And I get a file:

Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.0.30319.1]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'C:\temp\StackExchange\24414898\XmlDesignQuestion\AddressSchema.cs'.

I won't paste the file here, but you can instantiate an instance of Address like this then:

// Instantiate the Addresses class
var addresses = new Addresses();

// Set the main address
addresses.Main = new Address
{
    City = "Anywhere",
    Country = "USA"
};

// Add the additional addresses
addresses.AdditionalAddresses = new Address[]
{
    new Address
    {
        City = "Coolville",
        Country = "Ireland"
    },
    new Address
    {
        City = "Awesometown",
        Country = "Liberia"
    }
};

The merits of this are that you get a more tightly bound object that you didn't plumb up by hand. Let the tools do the work for you and you'll get more of what you want.

Tune your schema first, then derive from it your class.

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

Comments

1

First of all, I would not have different classes for MainAddress and AdditionalAddress, but with your current structure you could just initialize an Addresses object:

 Addresses a = new Addresses()
 {
    Main = new MainAddress { Country = "United States", City = "New York" },
    AdditionalAddresses = new List<AdditionalAddress>
    {
        new AdditionalAddress {Country = "Germany", City = "Berlin" },
        new AdditionalAddress {Country = "Australia", City = "Sydney" }
    }
 };

or the old-fashioned way:

Addresses a = new Addresses();

a.Main = new MainAddress();
a.Main.Country = "United States";
a.Main.City = "New York";

a.AdditionalAddresses = new List<AdditionalAddress>();

var aa = new AdditionalAddress();
aa.Country = "Germany";
aa.City = "Berlin";
a.AdditionalAddresses.Add(aa);

aa = new AdditionalAddress();
aa.Country = "Australia";
aa.City = "Sydney";
a.AdditionalAddresses.Add(aa);

1 Comment

Does this initialization tie the data to the Addresses object 'a'? If so, how?

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.