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

时间:2015-01-11 14:07:05

标签: c# xml

我有一个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标签。

有关如何完成此任务的任何建议?

3 个答案:

答案 0 :(得分:2)

看起来你在代码中犯了一个小错误。你关闭/ ingredients的说明。

这应该适用于正确反序列化到类

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

答案 1 :(得分:1)

首先出现错误,您应该阅读<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; }
}

我使用generator来构建此类。生成器为您的属性添加一些属性。了解他们here

答案 2 :(得分:0)

是的,您的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();