XML Serialization

S

Sjaakie

Hi,
I've read some articles on serialization, but I don't see how to
serialize the xml-structure below (if it is possible at all). Could you
point me in the right direction?

TIA

<?xml version="1.0" encoding="utf-8" ?>
<Settings>
<ApplicationTitle>...</ApplicationTitle>
<DebugEmail>...</DebugEmail>
<Proxy>
<Enabled>...</Enabled>
<Url>...</Url>
<Port>...</Port>
<Login>...</Login>
<Password>...</Password>
</Proxy>
<Feeds>
<Feed>
<Description>...</Description>
<Url>...</Url>
<DateTimeCorrection>...</DateTimeCorrection>
<RefreshInterval>...</RefreshInterval>
</Feed>
<Feed>
<Description>...</Description>
<Url>...</Url>
<DateTimeCorrection>...</DateTimeCorrection>
<RefreshInterval>...</RefreshInterval>
</Feed>
</Feeds>

</Settings>
 
G

Guest

use the xsd utility to generate a schema for this xml file. Use this schema
with the xsd utility to generate the class file.

1) xsd myFile.xml
2)xsd myFile.xsd /c

Use the generated Class in your project to Serialize the xml.

Hope this helps.

Cheers!
 
O

Ole Nielsby

Sjaakie said:
I've read some articles on serialization, but I don't see how to serialize
the xml-structure below [...]

Just curious: what's the point of serializing XML?
Isn't XML already serial?
Why would you want to drown a dead fish?
 
S

Sjaakie

Ole Nielsby schreef:
Sjaakie said:
I've read some articles on serialization, but I don't see how to serialize
the xml-structure below [...]

Just curious: what's the point of serializing XML?
Isn't XML already serial?
Why would you want to drown a dead fish?

You're right, I meant DEserializing
 
S

Sjaakie

Tantr,
I used the xsd util as described. But somehow I cannot use the output to
deserialize the file. The VS2005 debugger throws the following message:

error CS0030: cannot convert type SettingsFeedsFeed[] to
SettingsFeedsFeed

I use this code to deserialize the file:


FileStream fs = new FileStream(@"Data\Settings.xml", FileMode.Open);
XmlSerializer serializer = new XmlSerializer(typeof(Settings));
settings = (Settings)serializer.Deserialize(fs);


Below are snippets of the generated cs file and the complete xsd.
Can you help me out?

*** .cs file ***

public partial class Settings {
...


[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("Feed",
typeof(SettingsFeedsFeed),
Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
public SettingsFeedsFeed[][] Feeds {
get {
return this.feedsField;
}
set {
this.feedsField = value;
}
}
...

}


[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class SettingsFeedsFeed {

private string descriptionField;

private string urlField;

private string dateTimeCorrectionField;

private string refreshIntervalField;



[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Description {
get {
return this.descriptionField;
}
set {
this.descriptionField = value;
}
}

...
}

*** .xsd file ***

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Settings">
<xs:complexType>
<xs:sequence>
<xs:element name="ApplicationTitle" type="xs:string"
minOccurs="0" />
<xs:element name="DebugEmail" type="xs:string" minOccurs="0" />
<xs:element name="Proxy" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Enabled" type="xs:string" minOccurs="0" />
<xs:element name="Url" type="xs:string" minOccurs="0" />
<xs:element name="Port" type="xs:string" minOccurs="0" />
<xs:element name="Login" type="xs:string" minOccurs="0" />
<xs:element name="Password" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Feeds" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Feed" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Description" type="xs:string"
minOccurs="0" />
<xs:element name="Url" type="xs:string"
minOccurs="0" />
<xs:element name="DateTimeCorrection"
type="xs:string" minOccurs="0" />
<xs:element name="RefreshInterval" type="xs:string"
minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="NewDataSet" msdata:IsDataSet="true"
msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="Settings" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>



Tantr Mantr schreef:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top