使用什么 XML 属性?

What XML attribute to use?

我正在使用 XmlSerializer,我想实现 XML 树:

<request>
   <ix>ID</ix>
      <content>
            <name>NAMEVALUE</name>
            <visits>INT</visits>
         <dateRequested>yyyy-MM-dd</dateRequested>
      </content>
</request>

型号:

[XmlRoot(ElementName = "request")]
    public class RequestModel
    {
        [XmlElement("ix")]
        [JsonProperty("ix")]
        public int ID { get; set; }
        [XmlElement("name")]
        [JsonProperty("name")]
        public string Name { get; set; }
        [XmlElement("visits")]
        [JsonProperty("visits")]
        public int? Visits { get; set; }
        [XmlElement("date")]
        [JsonProperty("date")]
        public DateTime Date { get; set; }
    }

我应该使用什么属性来接收 XMl 树中的 <content> 组?

我的序列化器:

IEnumerable<RequestJSONModel> getModels = _context.Requests.ToList();
            foreach (var item in getModels)
            {
                RequestModel requestModel = new RequestModel();
                Content contentModel = new Content();
                //serialize
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(RequestModel));
                var serializedItem = "";
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add("", "");
                using (StringWriter writer = new Utf8StringWriter())
                {
                    xmlSerializer.Serialize(writer, xmlModel, ns);
                    serializedItem = writer.ToString(); // Your XML
                }
                serializedItem = serializedItem.Replace("\r\n", string.Empty);
            }

如果使用jdweng的解决方案,如何解析public class RequestModelpublic class Content

试试这个

using 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)
        {
            RequestModel model = new RequestModel() {
                ix = 123,
                content = new Content() {
                    Name = "NAMEVALUE",
                    Visits = 456,
                    dateRequested = "2018-12-16"
                }
            };

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(FILENAME, settings);

            XmlSerializer serializer = new XmlSerializer(typeof(RequestModel));

            serializer.Serialize(writer, model);
        }
    }
    [XmlRoot(ElementName = "request")]
    public class RequestModel
    {
        [XmlElement("ix")]
        public int ix { get; set; }

        [XmlElement("content")]
        public Content content { get; set; }

    }
    [XmlRoot(ElementName = "content")]
    public class Content
    {
        [XmlElement("name")]
        public string Name { get; set; }
        [XmlElement("visits")]
        public int? Visits { get; set; }

        private DateTime Date { get; set; }

        [XmlElement("dateRequested")]
        public string dateRequested
        {
            get { return Date.ToString("yyyy-MM-dd"); }
            set { Date = DateTime.ParseExact(value, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); }
        }

    }
}