XmlSerializer转换为XmlElement或字符串

时间:2017-12-08 14:09:22

标签: c# xml xmlserializer

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

我将我的课程设置为序列化

[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>

我不希望Project属性序列化的原因是Note元素位于父元素(NotesFor)中。

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

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

这样就留下了两个问题

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

2 个答案:

答案 0 :(得分:0)

据我所知,您的数据足够小,可以将其存储在xml文件中。 Xml文件存储在文件系统中,甚至您使用XmlDocument或XDocument加载并保存xml文件,整个文件将被重新写入文件系统。

我认为使用对象(NoteForDto&amp; NoteDto)和XmlSerializer来编写文件会更好。因此,您不必使用X(ml)文档复杂性。 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>

答案 1 :(得分:0)

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

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();