【问题标题】:c# xml multiple nested Arrays Deserializes时间:2019-01-01 标签:c#xmlmultiplenestedArraysDeserializes
【发布时间】:2021-07-24 22:27:23
【问题描述】:

我试过这个:https://www.c-sharpcorner.com/blogs/deserialize-xml-with-array-node

我的问题

例子:

<?xml version="1.0" encoding="UTF-8"?>
<notes>
<note>
  <to>
    <email>juan@gmail.com</email>
    <email>patrick@gmail.com</email>
    <email>Rose@gmail.com</email>
  </to>
  <from>Jose@hotmail.com</from>
  <heading>this is the email heading</heading>
</note>
</notes>

如果我有多个 XmlArray,我该怎么办?

【问题讨论】:

  • 可以分享一下示例吗,多个xml数组是什么意思?
  • 如果我有多个 XmlArray,我该怎么办? - 请您 edit 提出您的问题,通过分享 minimal reproducible example 显示您的代码和你卡在哪里?

标签: c# xml


【解决方案1】:

您的情况与链接不同,因为您在根注释的子节点有一个数组,而链接在根节点的孙子节点有一个数组。请参阅下面的代码:

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(Notes));
            Notes notes = (Notes)serializer.Deserialize(reader);
        }
    }
    [XmlRoot("notes")]
    public class Notes
    {
        [XmlElement("note")]
        public List<Note> note { get; set; }
    }
    public class Note
    {
        [XmlElement("to")]
        public To to { get; set; }
        public string from { get; set; }
        public string heading { get; set; }
    }
    public class To
    {
        [XmlElement()]
        public List<string> email { get; set; }
    }


}

【讨论】:

    猜你喜欢
    • 2018-01-03
    • 1970-01-01
    • 2014-11-14
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多