aaaarrrrggggg Who wants java examples in a .net book , 2006-03-09
Reviewer rating:
i was looking for example on using the System.xml.serialize name space. This is the example copied from the book
public enum AddressType {
Home,
Office,
Billing,
Shipping,
Mailing,
Day,
Evening,
FAX
}
If you'll look again at Example 9-7, you'll see that each state is actually listed by its full name, not the abbreviation as listed in the State enumeration. Here I've added an XmlEnumAttribute for each state name. Note that I've skipped some in the interest of space:
public enum State {
[XmlEnum(Name="Alaska")]
AK,
[XmlEnum(Name="Alabama")]
AL,
[XmlEnum(Name="Arkansas")]
AR,
[XmlEnum(Name="Arizona")]
AZ,
// ...
[XmlEnum(Name="Washington")]
WA,
[XmlEnum(Name="Wisconsin")]
WI,
[XmlEnum(Name="West Virginia")]
WV,
[XmlEnum(Name="Wyoming")]
WY
}
The Address class has one attribute, type, and four elements. Here I've added XmlAttributeAttribute and XmlElementAttribute, as appropriate. The AttributeName and ElementName fields of each attribute are used to set the names of the XML attributes and elements, respectively:
public class Address {
[XmlAttribute(AttributeName="type")]
public AddressType AddressType;
[XmlElement(ElementName="street")]
public string[ ] Street;
[XmlElement(ElementName="city")]
public string City;
[XmlElement(ElementName="state")]
public State State;
[XmlElement(ElementName="zip")]
public string Zip;
}
Similar to Address, the TelephoneNumber class has one attribute and three elements. Again, I've decorated each member with the appropriate attribute. Note also that here, as in Address, I've set the names of the attributes and elements to match the ones in the XML; that is, they all start with lowercase letters:
public class TelephoneNumber {
[XmlAttribute(AttributeName="type")]
public AddressType AddressType;
[XmlElement(ElementName="areacode")]
public string AreaCode;
[XmlElement(ElementName="exchange")]
public string Exchange;
[XmlElement(ElementName="number")]
public string Number;
}
Now we come to the meat of the personnel record, the Employee. This class has three attributes: firstname, middleinitial, and lastname, which I've treated with the appropriate attribute. However, the Employee class also has two additional elements, addresses and telephones. These two elements actually contain nested arrays of elements, so I've used the XmlArray and XmlArrayItem attributes to help the serializer figure out what to do with the XML elements it reads:
public class Employee {
[XmlAttribute(AttributeName="firstname")]
public string FirstName;
[XmlAttribute(AttributeName="middleinitial")]
public string MiddleInitial;
[XmlAttribute(AttributeName="lastname")]
public string LastName;
[XmlArray(ElementName="addresses")]
[XmlArrayItem(ElementName="address")]
public Address [ ] Addresses;
[XmlArray(ElementName="telephones")]
[XmlArrayItem(ElementName="telephone")]
public TelephoneNumber [ ] TelephoneNumbers;
[XmlAttribute(AttributeName="hiredate")]
public DateTime HireDate;
}
Here's the document element, personnel, which is decorated with XmlRootAttribute. Although the Employees member is an array of Employee objects, it is not a nested array, like addresses and telephones. By adding the XmlElement attribute directly to the member, the XmlSerializer knows that this member is to be serialized as an array of employee elements, without a separate top-level element:
[XmlRoot(ElementName="personnel")]
public class Personnel {
[XmlElement(ElementName="employee")]
public Employee [ ] Employees;
}
Finally, I've made some changes to the Serializer class, which I introduced in Example 9-5. Serializer's Main( ) method still uses the CreatePersonnel( ) to create some personnel records, but it then instantiates an XmlSerializer to deserialize the objects it created back out to a file:
public class Serializer {
public static void Main(string [ ] args) {
Personnel personnel = CreatePersonnel( );
XmlSerializer serializer = new XmlSerializer(typeof(Personnel));
using (FileStream stream = File.OpenWrite("Personnel.xml")) {
serializer.Serialize(stream,personnel);
}
}
}
notice its in java??? for crying out loud if your going to say .net and xml use .net examples not JAVA