I'm having trouble de-serializing an XML document.
<rules version="61">
<emie>
<domain exclude="false">facebook.com</domain>
<domain exclude="false">google.com</domain>
<domain exclude="false">bbc.co.uk</domain>
</emie>
<docMode>
<domain docMode="7">outlook.com</domain>
<domain docMode="7">yahoo.com</domain>
</docMode>
</rules>
I've copied and pasted into visual studio - using the Paste XML as class and that's created the following class
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class rules
{
private rulesDomain[] emieField;
private rulesDomain1[] docModeField;
private byte versionField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("domain", IsNullable = false)]
public rulesDomain[] emie
{
get
{
return this.emieField;
}
set
{
this.emieField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("domain", IsNullable = false)]
public rulesDomain1[] docMode
{
get
{
return this.docModeField;
}
set
{
this.docModeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte version
{
get
{
return this.versionField;
}
set
{
this.versionField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class rulesDomain
{
private bool excludeField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public bool exclude
{
get
{
return this.excludeField;
}
set
{
this.excludeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class rulesDomain1
{
private string docModeField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string docMode
{
get
{
return this.docModeField;
}
set
{
this.docModeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
I then run the following code to deserialise
XmlSerializer mySerializer = new XmlSerializer(typeof(XmlSerializer));
Classes.rules r;
using (StreamReader reader = new StreamReader(@"file.xml"))
{
r = (Classes.rules)mySerializer.Deserialize(reader);
}
It's errors on the line r =... {" was not expected."}
I'm pretty certain the error lies in the class and the declarations at the top but I haven't been able to change it so it works, could anyone help?
Change
XmlSerializer mySerializer = new XmlSerializer(typeof(XmlSerializer));
to
XmlSerializer mySerializer = new XmlSerializer(typeof(Classes.rules));
Its parameter is type of the object that this XmlSerializer can serialize.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.