我正在序列化一个类,我得到以下异常:
您必须在System.Configuration.SettingsPropertyCollection上实现默认访问器,因为它继承自ICollection.
当执行以下行时:
XmlSerializer xs = new XmlSerializer(typeof(CustomConfiguration));
Run Code Online (Sandbox Code Playgroud)
有帮助吗?
public class CustomConfiguration : ConfigurationObjectBase
{
public CustomConfiguration () { //DO NOTHING. }
[User]
public uint Version
{ get { return ((uint)(this["Version"])); }
set { this["Version"] = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
ConfigurationObjectBase派生自System.configuration.ApplicationSettingsBase.
我正在尝试从xsd架构生成一个类,但我收到以下错误消息:
警告:无法生成类,因为找不到具有复杂类型的顶级元素.
我的xsd文件看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="MonitoringConfiguration"
targetNamespace="urn:MonitoringConfiguration-1.0"
elementFormDefault="qualified"
xmlns="urn:MonitoringConfiguration-1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:complexType name="MonitoringConfiguration">
<xs:sequence>
<xs:element name="Machine" type="Machine" minOccurs="0" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="Machine">
<xs:sequence>
<xs:element name="Component" type="Component" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Component">
<xs:attribute name="Name" type="xs:string" use="required"/>
<xs:attribute name="Type" type="xs:string" use="optional"/>
</xs:complexType>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
我正在使用以下命令行生成类:
xsd MonitoringConfiguration.xsd /languages:CS /Classes
Run Code Online (Sandbox Code Playgroud)
注意我已经定义了一个复杂类型的顶级元素(MonitoringConfiguration).
怎么了?
谢谢