[英]Error Deserializing XML Document 1,2
I'm having trouble de-serializing an XML document. 我在反序列化XML文档时遇到麻烦。
<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 我已经将粘贴XML作为类复制并粘贴到了Visual Studio中,并创建了以下类
/// <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."} r = ... {上没有预期的错误。“}
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. 它的参数是此XmlSerializer可以序列化的对象的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.