簡體   English   中英

如何反序列化 xml 文件的子節點

[英]How to deserialize child nodes of xml file

我使用 Xamarin Android 創建了一個應用程序。 計划是從 Assets 文件夾中打開 Clients.xml 的只讀副本,並在內部存儲中創建副本以供將來編輯。 默認文件有一個客戶端根節點和六個客戶端子節點。

我已經達到了讀取資產並創建本地文件的程度,但僅包含第一個子節點。 我不能把它放到一個子節點數組中。

我需要做什么才能讀取所有子節點?

讀取資產並創建本地文件。

        // Get storage path
        path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);

        // Create clients file on first build
        string filename = System.IO.Path.Combine(path, "Clients.xml");
        if (!File.Exists(filename))
        {
            clients clients;

            // Load read-only asset from file
            var xmlSerialSettings = new XmlSerializer(typeof(clients));
            using (StreamReader sr = new StreamReader(Android.App.Application.Context.Assets.Open("Clients.xml"), true))
            {
                clients = (clients)xmlSerialSettings.Deserialize(sr);
            }
            // Save local version to edit
            var xsw = new XmlSerializer(typeof(clients));
            using (StreamWriter sw = new StreamWriter(filename, false))
            {
                xsw.Serialize(sw, clients);
            }
        }

客戶.xml

<?xml version="1.0" encoding="utf-8" ?>
<clients>
    <client id="1">
        <name>Bob</name>
        <address>Home</address>
        <postcode>CO1</postcode>
        <email>bob@home.com</email>
        <landline></landline>
        <mobile>07784</mobile>
    </client>
    <client id="2">
        <name>...</name>
        <address>...</address>
        <postcode>...</postcode>
        <email>...</email>
        <landline>...</landline>
        <mobile>...</mobile>
    </client>
    ....
</clients>

客戶端.cs

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace QuoteBuilder
{
    [Serializable, XmlRoot("clients")]
    public class clients
    {
        [XmlElement("client")]
        public client client { get; set; }

        public clients()
        {

        }

        public clients(client client)
        {
            this.client = client;
        }
    }

    public class client
    {
        public string name { get; set; }
        public string address { get; set; }
        public string postcode { get; set; }
        public string email { get; set; }
        public string landline { get; set; }
        public string mobile { get; set; }

        public client()
        {

        }

        public client(string name, string address, string postcode, string email, string landline, string mobile)
        {
            this.name = name;
            this.address = address;
            this.postcode = postcode;
            this.email = email;
            this.landline = landline;
            this.mobile = mobile;
        }
    }
}

在您的客戶 class 中,您定義了一個成員 - 客戶。 它不是一個集合,當您將 xml 文件反序列化到客戶端 class 時,它只反序列化一個節點(第一個)。 使用 List 或其他 collections 里面:

    [Serializable, XmlRoot("clients")]
    public class Clients
    {
        [XmlElement("client")]
        public List<Client> client { get; set; } // Collection

        public Clients()
        {

        }

        public Clients(List<Client> client)
        {
            this.client = client;
        }
    }

還對類使用 CamelCase 命名,對變量使用小寫命名 - 它更具可讀性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM