简体   繁体   English

反序列化Xml文档没有结果

[英]Deserializing an Xml document no results

I'm having some difficulties de-serializing an Xml document I have. 我在反序列化我拥有的Xml文档时遇到一些困难。 See small complete sample of doc below: 请参阅下面的文档小样本:

<?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>

The associated classes look like this: 关联的类如下所示:

[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(){}
}

And the de-serializing is done with: 反序列化通过以下方式完成:

XmlSerializer serializer = new XmlSerializer(typeof(MailClient));
FileStream xmlFile = new FileStream(ConfigFile, FileMode.Open);
MailClient clients = (MailClient)serializer.Deserialize(xmlFile);

I was getting an exception There is an error in XML document (2, 2). 我遇到异常There is an error in XML document (2, 2). relating to the MailClient element: {"<MailClient xmlns=''> was not expected."} . MailClient元素有关的内容: {"<MailClient xmlns=''> was not expected."} So I passed in a xml root attribute: 所以我传入了一个xml根属性:

XmlSerializer serializer = new XmlSerializer(typeof(MailClient), 
new XmlRootAttribute("MailClient"));

Which seemed to correct the problem. 这似乎可以解决问题。 clients now contains a single client but none of the propertys have been populated, that is to say category , name etc... all remain empty. clients现在只包含一个client但没有填充任何属性,也就是说, categoryname等...全部保持为空。

Can anyone see where I've gone wrong here? 谁能看到我在哪里出问题了? At this point I'm beginning to think it might be quicker for me to just use Linq to Xml rather than trying to de-serialize this 在这一点上,我开始认为对Linml使用Xml而不是尝试反序列化可能更快

In your MailClient class, you have XmlRoot("Client") . MailClient类中,您具有XmlRoot("Client") Change that to the actual root element name, specifically XmlRoot("MailClient") . 将其更改为实际的根元素名称,特别是XmlRoot("MailClient") That way, you don't have to use the XmlRootAttribute in code. 这样,您不必在代码中使用XmlRootAttribute

For collections, you should use the XmlArray and XmlArrayItem attributes. 对于集合,应使用XmlArrayXmlArrayItem属性。

And where you are using XmlType attributes, you should be using XmlElement attributes. 在使用XmlType属性的地方,应该使用XmlElement属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM