在 ListView 中加载 Xml 文件并编辑一些值

Load Xml file in ListView and edit some values

我对 C# 和 Xamarin 完全陌生。首先,我想阅读这个示例 XMl 文件并在 ListView 中显示。然后,我必须从这个 xml 中编辑一些值。谁能举例说明我该怎么做?

<breakfast_menu>
    <food>
        <name>Belgian Waffles</name>
        <price>.95</price>
        <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
        <calories>650</calories>
    </food>
    <food>
        <name>Strawberry Belgian Waffles</name>
        <price>.95</price>
        <description>Light Belgian waffles covered with strawberries and whipped cream</description>
        <calories>900</calories>
    </food>
    <food>
        <name>Berry-Berry Belgian Waffles</name>
        <price>.95</price>
        <description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
        <calories>900</calories>
    </food>
    <food>
        <name>French Toast</name>
        <price>.50</price>
        <description>Thick slices made from our homemade sourdough bread</description>
        <calories>600</calories>
    </food>
    <food>
        <name>Homestyle Breakfast</name>
        <price>.95</price>
        <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
        <calories>950</calories>
    </food>
</breakfast_menu>

1.将 XML 转换为 类

在 Visual Studio> 编辑菜单> 选择性粘贴> 粘贴 XML 为 类

// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class breakfast_menu
{

    private breakfast_menuFood[] foodField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("food")]
    public breakfast_menuFood[] food
    {
        get
        {
            return this.foodField;
        }
        set
        {
            this.foodField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class breakfast_menuFood
{

    private string nameField;

    private string priceField;

    private string descriptionField;

    private ushort caloriesField;

    /// <remarks/>
    public string name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField = value;
        }
    }

    /// <remarks/>
    public string price
    {
        get
        {
            return this.priceField;
        }
        set
        {
            this.priceField = value;
        }
    }

    /// <remarks/>
    public string description
    {
        get
        {
            return this.descriptionField;
        }
        set
        {
            this.descriptionField = value;
        }
    }

    /// <remarks/>
    public ushort calories
    {
        get
        {
            return this.caloriesField;
        }
        set
        {
            this.caloriesField = value;
        }
    }
}

2。转换 XML AS 列表 从 XML 文件中获取列表并绑定到列表视图。

>Xamarin.Forms: 读取自 Embedded resource

 var list = new List<breakfast_menuFood>();
        var assembly = Assembly.GetExecutingAssembly();
        var resourceName = "App1.breakfast2.xml";
        XmlSerializer serializer = new XmlSerializer(typeof(List<breakfast_menu>));

        using (Stream stream = assembly.GetManifestResourceStream(resourceName))
        using (StreamReader reader = new StreamReader(stream))
        {
            XmlSerializer serializer2 = new XmlSerializer(typeof(breakfast_menu));
            var breakfast = (breakfast_menu)serializer2.Deserialize(reader);
            foreach (var item in breakfast.food)
            {
                list.Add(new breakfast_menuFood
                {
                    calories = item.calories,
                    description = item.description,
                    name = item.name,
                    price = item.price
                });
            }
        }

>Xamarin.Android: 从资产文件夹读取。

 using (StreamReader streamReader = new StreamReader(Assets.Open("breakfast.xml")))
            {
                //xmlString = streamReader.ReadToEnd();                

                XmlSerializer serializer = new XmlSerializer(typeof(breakfast_menu));
                var school = (breakfast_menu)serializer.Deserialize(streamReader);

                var list = new List<breakfast_menuFood>();

                foreach (var item in school.food)
                {
                    list.Add(new breakfast_menuFood
                    {
                        calories = item.calories,
                        description = item.description,
                        name = item.name,
                        price = item.price
                    });
                }
            }