By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,890 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,890 IT Pros & Developers. It's quick & easy.

XML Serialization question using XSD.exe generated code

P: n/a
Hi
I have the following XML file:

================================================== =========
<?xml version="1.0" encoding="UTF-16"?>
<Policy xmlns="http://tempuri.org/richard.xsd">
<TransType />
<LOB />
<Name>
<FirstName />
<LastName />
</Name>
</Policy>
================================================== =========

I created the following XSD and CS code using xsd.exe:
================================================== =========
<?xml version="1.0" ?>
<xs:schema id="NewDataSet"
targetNamespace="http://tempuri.org/richard.xsd"
xmlns:mstns="http://tempuri.org/richard.xsd"
xmlns="http://tempuri.org/richard.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:element name="Policy">
<xs:complexType>
<xs:sequence>
<xs:element name="TransType" type="xs:string" minOccurs="0" />
<xs:element name="LOB" type="xs:string" minOccurs="0" />
<xs:element name="Name" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="FirstName" type="xs:string"
minOccurs="0" />
<xs:element name="LastName" type="xs:string"
minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="NewDataSet" msdata:IsDataSet="true"
msdata:EnforceConstraints="False">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element ref="Policy" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
================================================== =========
================================================== =========
using System.Xml.Serialization;
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://tempuri.org/richard.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespac e="http://tempuri.org/richard.xsd",
IsNullable=false)]
public class Policy {

/// <remarks/>
public string TransType;

/// <remarks/>
public string LOB;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Name ")]
public PolicyName[] Name;
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://tempuri.org/richard.xsd")]
public class PolicyName {

/// <remarks/>
public string FirstName;

/// <remarks/>
public string LastName;
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://tempuri.org/richard.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespac e="http://tempuri.org/richard.xsd",
IsNullable=false)]
public class NewDataSet {

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Poli cy")]
public Policy[] Items;
}
================================================== =========

My question is whether the following is the only way to fill in an
object of type Policy, or if somebody can show me a better way (the
part I'm most concerned about is the "PolicyName[]" part:

================================================== =========
Policy myPolicy = new Policy();
myPolicy.TransType = "Transaction Type";
myPolicy.LOB = "Homeowners";

PolicyName myName = new PolicyName();
myName.FirstName = "John";
myName.LastName = "Smith";

myPolicy.Name = new PolicyName[1];
myPolicy.Name[0] = myName;

XmlSerializer serializer = new XmlSerializer(typeof(Policy));

TextWriter writer = new StreamWriter("policy.xml");
serializer.Serialize(writer, myPolicy);
writer.Close();
================================================== =========

Thanks.

- Richard

Nov 29 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.