我在反序列化我拥有的 Xml 文档时遇到了一些困难。请参阅下面的完整文档小样本:
<?xml version="1.0" encoding="utf-8" ?>
<MailClient>
<Client Id="Outlook.com">
<Property Supported="false" Category="Responsive" Name="@media"><![CDATA["@media"]]></Property>
<Property Supported="false" Category="Selectors" Name="*"><![CDATA["*"]]></Property>
<Property Supported="false" Category="Selectors" Name="ElementSelector"><![CDATA["E"]]></Property>
<Property Supported="false" Category="Selectors" Name="[=]"><![CDATA["[=]"]]></Property>
<Property Supported="false" Category="Selectors" Name="[~=]"><![CDATA["[~=]"]]></Property>
<Property Supported="false" Category="Selectors" Name="[^=]"><![CDATA["[^=]"]]></Property>
<Property Supported="false" Category="Selectors" Name="[$=]"><![CDATA["[$=]"]]></Property>
<Property Supported="false" Category="Selectors" Name="[*=]"><![CDATA["[*=]"]]></Property>
</Client>
</MailClient>
关联的类如下所示:
[Serializable, XmlRoot("Client"), XmlType("Client")]
public class MailClient
{
[XmlElement("Client")]
public List<CssRule> CssRules { get; set; }
public MailClient()
{
CssRules = new List<CssRule>();
}
}
[XmlType("Property")]
public class CssRule
{
[XmlAttribute("Name")]
public string Name { get; set; }
[XmlAttribute("Category")]
public string Category { get; set; }
[XmlAttribute("Supported")]
public bool IsSupported{ get; set; }
public CssRule(){}
}
反序列化是通过以下方式完成的:
XmlSerializer serializer = new XmlSerializer(typeof(MailClient));
FileStream xmlFile = new FileStream(ConfigFile, FileMode.Open);
MailClient clients = (MailClient)serializer.Deserialize(xmlFile);
我得到了一个与元素There is an error in XML document (2, 2).
有关的异常: . 所以我传入了一个 xml 根属性:MailClient
{"<MailClient xmlns=''> was not expected."}
XmlSerializer serializer = new XmlSerializer(typeof(MailClient),
new XmlRootAttribute("MailClient"));
这似乎纠正了这个问题。clients
现在包含单个client
但没有任何属性被填充,也就是说category
,name
等等......都保持为空。
谁能看到我在这里出错的地方?在这一点上,我开始认为只使用 Linq to Xml 而不是尝试反序列化它可能会更快