XmlSerializer 转换为 XmlElement 或字符串

XmlSerializer to convert to XmlElement or string

我将开发人员注释存储在项目中的 XML 文件中,以消除将它们存储在 SQL 实例上以减少数据库调用。

我已将 class 设置为序列化

[Serializable]
public class NoteDto 
{
    [NonSerialized]
    private string _project;

    public string Date { get; set; }
    public string Time { get; set; }
    public string Author { get; set; }
    public string NoteText { get; set; }
    public string Project
    {
        get => _project;
        set => _project = value;
    }       
}

创建这个元素

<Note>
  <Date></Date>
  <Time></Time>
  <NoteText></NoteText>
  <Author></Author>
</Note>

我不希望项目 属性 序列化的原因是注释元素位于父元素 (NotesFor) 中。

<NotesFor id="project value here">
  // note element here
</NotesFor>

我需要项目字符串来搜索节点,其中 NotesFor 元素 id = 项目 然后将创建的元素附加到其子元素的末尾。

所以还有两个问题

  1. 我是创建一个 XElement 并追加到当前节点还是创建一个字符串 附加到 Xml 文件中的节点?我不经常使用 Xml 所以我 不确定更新 Xml 文件的标准协议是什么。
  2. 有没有更好的方法来完成我想做的事情?

据我了解,您的数据足够小,可以将其存储在 xml 个文件中。 Xml 文件存储在文件系统中,即使您使用 XmlDocument 或 XDocument 加载和保存 xml 文件,整个文件也会被重新写入文件系统。

我认为最好使用对象(NoteForDto & NoteDto)和XmlSerializer 来写入文件。因此,您不必处理 X(ml)Document 的复杂性。 Here is the example
您的代码示例:

    public class NoteDto
    {

        private string _project;

        public string Date { get; set; }
        public string Time { get; set; }
        public string Author { get; set; }
        public string NoteText { get; set; }

        [XmlIgnore]
        public string Project
        {
            get => _project;
            set => _project = value;
        }
    }

    [XmlRoot(ElementName = "NotesFor")]
    public class NotesForDto
    {
        [XmlAttribute(AttributeName="id")]
        public string ProjectID { get; set; }

        [XmlElement(ElementName ="Note")]            
        public List<NoteDto> NoteList { get; set; }
    }

    public static void Main(string[] args)
    {
        var notes = new NotesForDto
        {
            ProjectID = "SampelProject",
            NoteList = new List<NoteDto>
           {
               new NoteDto
               {
                   Author="Author1",
                   Date= "Date1",
                   NoteText="NoteText1",
                   Project="SampleProject",
                   Time="Time1"
               },
               new NoteDto
               {
                   Author="Author2",
                   Date= "Date2",
                   NoteText="NoteText2",
                   Project="SampleProject",
                   Time="Time2"
               }
           }
        };            

        XmlSerializer ser = new XmlSerializer(typeof(NotesForDto));

        using (var tw = File.Open("notes.xml",FileMode.Create))
        {
            ser.Serialize(tw,notes);
        }                
    }

输出(notes.xml)

<?xml version="1.0"?>
<NotesFor xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="SampelProject">
  <Note>
    <Date>Date1</Date>
    <Time>Time1</Time>
    <Author>Author1</Author>
    <NoteText>NoteText1</NoteText>
  </Note>
  <Note>
    <Date>Date2</Date>
    <Time>Time2</Time>
    <Author>Author2</Author>
    <NoteText>NoteText2</NoteText>
  </Note>
</NotesFor>

您不需要将 class 标记为可序列化。

public class NoteDto 
{
    public string Date { get; set; }
    public string Time { get; set; }
    public string Author { get; set; }
    public string NoteText { get; set; }
    [XmlIgnore]
    public string Project { get; set; }
}

您可以使用以下代码序列化它,我已将其作为扩展方法实现:

public static string ToXml<T>(this T thing)
{
    if (thing == null)
        return null;

    var builder = new StringBuilder();

    new XmlSerializer(typeof(T)).Serialize(new StringWriter(builder), thing);

    return builder.ToString();
}

像这样使用:

var note = new NoteDto
{
    Date = "1/1/2018",
    Time = "4:00 PM",
    Author = "Some Guy",
    NoteText = "My Note",
    Project = "A Project"
};
var xml = note.ToXml();