简体   繁体   English

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

[英]Deserializing XML that has <li></li>

I have an XML file that contains elements that have lists of items: 我有一个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>

I am using Visual Studio C# XmlReader.Deserialize() to deserialize the XML into a class that I would like to look like this: 我正在使用Visual Studio C#XmlReader.Deserialize()将XML反序列化为一个类,我希望看起来像这样:

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

Where each element of ingredients and instructions would be the text between li tags. 成分和说明的每个要素都是li标签之间的文字。

Worse comes to worse, I would accept ingredients and instructions each being a single string that I could then parse out the li tags. 更糟糕的是,我会接受成分和说明,每个都是一个字符串,然后我可以解析li标签。

Any suggestions on how to accomplish this? 有关如何完成此任务的任何建议?

looks like you made a slight error in your code. 看起来你在代码中犯了一个小错误。 you close instructions with /ingredients. 你关闭/成分说明。

this should work to properly deserialize to a class 这应该适用于正确反序列化到类

[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;
        }
    }
}

First you have an error, you should read the close-tag of <instructions> . 首先你有一个错误,你应该阅读<instructions>的close标签。

Well, your xml : 好吧,你的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>

You should use property for your class : 你应该为你的班级使用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; }
}

I use a generator to build this class. 我用一个生成器来构建这个类。 The generator add some attributes to your properties. 生成器为您的属性添加一些属性。 Learn about them here . 在这里了解它们。

Yup, you had an error in your xml, it should look like: 是的,你的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>

Also, your class is supposed to be: 此外,你的班级应该是:

[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; }
}

And finally, code: 最后,代码:

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

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

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