反序列化具有
  • 的 XML

    本文关键字:li XML 反序列化 | 更新日期: 2023-09-27 18:37:02

    >我有一个XML文件,其中包含具有项目列表的元素:

    <recipe>
    <title>Recipe 1</title>
    <ingredients><li>eggs</li><li>milk</li><li>etc...</li></ingredients>
    <instructions><li>break eggs</li><li>spill milk</li><li>enjoy!</li></ingredients>
    </recipe>
    

    我正在使用Visual Studio C# XmlReader.Deserialize()将XML反序列化为一个类,我希望它看起来像这样:

    public class recipe
    {
    string title;
    string ingredients[];
    string instructions[];
    }
    

    其中成分和说明的每个元素都是 li 标签之间的文本。

    更糟糕的是,我会接受成分和说明,每个都是一个字符串,然后我可以解析出 li 标签。

    关于如何完成此操作的任何建议?

    反序列化具有 <li></li> 的 XML

    看起来您在代码中犯了一个轻微的错误。 使用/成分关闭说明。

    这应该可以正确地反序列化为类

    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class recipe
    {
        private string titleField;
        private string[] ingredientsField;
        private string[] instructionsField;
        public string title
        {
            get
            {
                return this.titleField;
            }
            set
            {
                this.titleField = value;
            }
        }
        [System.Xml.Serialization.XmlArrayItemAttribute("li", IsNullable = false)]
        public string[] ingredients
        {
            get
            {
                return this.ingredientsField;
            }
            set
            {
                this.ingredientsField = value;
            }
        }
        [System.Xml.Serialization.XmlArrayItemAttribute("li", IsNullable = false)]
        public string[] instructions
        {
            get
            {
                return this.instructionsField;
            }
            set
            {
                this.instructionsField = value;
            }
        }
    }
    

    首先你有一个错误,你应该阅读 <instructions> 的关闭标签。

    好吧,你的xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <recipe>
        <title>Recipe 1</title>
        <ingredients>
           <li>eggs</li>
           <li>milk</li>
           <li>etc...</li>
        </ingredients>
        <instructions>
           <li>break eggs</li>
           <li>spill milk</li>
           <li>enjoy!</li>
        </instructions>
    </recipe>
    

    您应该为类使用property

    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public class recipe
    {
        public string title { get; set; }
        [System.Xml.Serialization.XmlArrayItemAttribute("li", IsNullable = false)]
        public string[] ingredients { get; set; }
        [System.Xml.Serialization.XmlArrayItemAttribute("li", IsNullable = false)]
        public string[] instructions { get; set; }
    }
    

    我使用生成器来构建此类。生成器会向属性添加一些属性。在此处了解它们。

    是的,你的 xml 中有一个错误,它应该看起来像:

    <recipe>
    <title>Recipe 1</title>
    <ingredients>
        <li>eggs</li><li>milk</li><li>etc...</li>
    </ingredients>
    <instructions>
        <li>break eggs</li><li>spill milk</li><li>enjoy!</li>
    </instructions>
    </recipe>
    

    此外,您的类应该是:

    [XmlRoot("recipe")]
    public class Receipe
    {
        [XmlElement("title")]
        public string Title { get; set; }
        [XmlArray("ingredients")]
        [XmlArrayItem("li")]
        public string[] Ingridients { get; set; }
        [XmlArray("instructions")]
        [XmlArrayItem("li")]
        public string[] Instructions { get; set; }
    }
    

    最后,代码:

    var serializer = new XmlSerializer(typeof(Receipe));
    var reader = new StringReader(xml);
    var receipe = (Receipe)serializer.Deserialize(reader);
    reader.Close();