【问题标题】:Deserialization of the XML File in C#C#中XML文件的反序列​​化
【发布时间】:2015-05-26 12:08:58
【问题描述】:

我正在尝试反序列化一个如下所示的 XML 文件:

<xml>
    <classes>
        <class name="EventLog">
            <attribute name="TYPE" type="int"></attribute>
            <attribute name="DESCRIPTION" type="string"></attribute>
            <attribute name="ISSUEDBY" type="string></attribute>
            <attribute name="DATE" type="hr_clock"></attribute>
        </class>
        <class name="test">
            <attribute name="ttt" type="int"></attribute>
            <attribute name="ppp" type="string"></attribute>
            <attribute name="xxx" type="string"></attribute>
            <attribute name="aaa" type="hr_clock"></attribute>
        </class>
    </classes>
    <filters>
        <filter name="COILORDERFILTER">
            <attribute name="COILID" type="string"></attribute> 
            <attribute name="RELIABID" type="string"></attribute>   
        </filter>     
        <filter name="DriveDataFilter">
            <attribute name="DRIVEID" type="string"></attribute>        
        </filter>
    </filters>
</xml>

我只需要节点classes 之间的类。我为反序列化创建了以下类:

[Serializable()]
[System.Xml.Serialization.XmlRoot("classes")]
public class ClassCollection
{
    [XmlArray("class")]
    [XmlArrayItem("attribute", typeof(SingleClass))]
    public SingleClass[] singleClass { get; set; }
}

[Serializable()]
public class SingleClass
{
    [System.Xml.Serialization.XmlAttribute("name")]
    public string name { get; set; }

    [System.Xml.Serialization.XmlAttribute("type")]
    public string type { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        ClassCollection classes = null;
        string path = @"C:\Users\test\Desktop\Eventlog.xml";

        XmlSerializer serializer = new XmlSerializer(typeof(ClassCollection));

        StreamReader reader = new StreamReader(path);

        try
        {
            classes = (ClassCollection)serializer.Deserialize(reader);
        }
        catch (InvalidOperationException excep)
        {
            Console.WriteLine(excep.ToString());
        }

        Console.Read();
    }
}

谁能告诉我“怎么了”?

【问题讨论】:

  • 代码是否抛出异常
  • “出了什么问题”你必须告诉我们出了什么问题
  • 第一次影响,我会说您正在尝试反序列化类节点,但您将完整的 xml 提供给序列化程序
  • 请注意,这种序列化不需要Serializable 属性...stackoverflow.com/questions/392431/…
  • 注意:这里缺少一个结束引号:&lt;attribute name="ISSUEDBY" type="string&gt;&lt;/attribute&gt; - 你需要先解决这个问题

标签: c# xml deserialization


【解决方案1】:

这里有一个提示;将您的示例 xml 放在一个文件中 - my.xml,然后:

xsd my.xml
xsd my.xsd /classes

这将创建与 xml 匹配的 my.cs

或者:您需要了解所有标记;根是&lt;xml/&gt;,所以你需要[XmlRoot("xml")],例如。你几乎可以肯定的意思是:

    [XmlArray("classes")]
    [XmlArrayItem("class", typeof(SingleClass))]
    public SingleClass[] singleClass { get; set; }

这行得通,例如:

[XmlRoot("xml")]
public class ClassCollection
{
    [XmlArray("classes")]
    [XmlArrayItem("class", typeof(SingleClass))]
    public SingleClass[] singleClass { get; set; }
}
public class SingleClass
{
    [XmlElement("attribute")]
    public List<Attribute> Attributes { get; set; }

    [XmlAttribute("name")]
    public string Name { get; set; }
}
public class Attribute {
    [XmlAttribute("name")]
    public string name { get; set; }

    [XmlAttribute("type")]
    public string type { get; set; }
}

【讨论】:

    【解决方案2】:

    不确定您的问题,因为没有错误

    但第一个:

    [XmlRoot("classes")]
    

    这意味着第一个 XML 节点应该是“类”,现在从您的示例中它是“xml”。所以创建一个新对象:

    [XmlRoot("xml")]
    class MyRoot {
        //...
    

    另外,我认为通常你会希望将这样的内容作为默认序列化程序工作的第一行:

    <?xml version="1.0" encoding="utf-8" ?>
    

    另外,我认为您的数据类定义中有一些错误。您应该评论一些内容并使用较少的数据进行一些测试,以便正确理解正在发生的事情。

    【讨论】:

    • “通常你会想要拥有”是不必要的 - xml 描述标题不是强制性的
    • 我记得当我没有那个标头时,XmlSerializer 出现了一些问题。我只是凭记忆写下那句话,但你可能是对的,这不是强制性的。但我认为无论如何这是一个很好的做法。
    【解决方案3】:

    如果你不能修改你的 xml,你的问题和这个非常相似: Ignore outer elements using XmlSerializer

    个人而言,我将使用 XmlReader 而不是 XmlSerializer 来反序列化您的示例 XML,因为它允许构建一个与您正在阅读的 XML 没有严重耦合的模型。

    【讨论】:

      猜你喜欢
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 2014-02-15
      • 1970-01-01
      • 2010-11-25
      • 1970-01-01
      • 2011-05-12
      相关资源
      最近更新 更多