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

问题描述

我完全是C#和Xamarin的新手。首先,我想阅读此示例XMl文件并在ListView中显示。然后,我必须从此xml编辑一些值。谁能给我榜样,我该怎么做?

<breakfast_menu>
    <food>
        <name>Belgian Waffles</name>
        <price>$5.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>$7.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>$8.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>$4.50</price>
        <description>Thick slices made from our homemade sourdough bread</description>
        <calories>600</calories>
    </food>
    <food>
        <name>Homestyle Breakfast</name>
        <price>$6.95</price>
        <description>Two eggs,bacon or sausage,toast,and our ever-popular hash browns</description>
        <calories>950</calories>
    </food>
</breakfast_menu>

解决方法

1。转换XML AS类

在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文件获取列表并将其绑定到listview。

> 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 :从Assets文件夹中读取。

 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,price = item.price
                    });
                }
            }

enter image description here