【问题标题】:XmlSerializer to convert to XmlElement or stringXmlSerializer 转换为 XmlElement 或字符串
【发布时间】:2017-12-08 14:09:22
【问题描述】:

我将开发人员注释存储在项目中的 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. 有没有更好的方法来完成我想做的事情?

【问题讨论】:

    标签: c# xml xmlserializer


    【解决方案1】:

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

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

    【讨论】:

    • @Yuck...因为您要返回一个字符串...我假设更新 xml 文件只是附加到末尾或插入到父项中的问题,新元素作为字符串当前文件?
    • 如果您需要使用 XML,您应该使用 XElement,它可以通过调用 XElement.Parse(xml); 从生成的字符串 xml 中加载。
    • @Yuck...玩这个我看到它创建了 Xml 节点标头,其编码属性设置为“UTF-16”,...是否有过载,所以我将其更改为 UTF-8 而不是手动执行(在其他代码中)?
    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 2011-10-17
      • 2011-04-11
      • 1970-01-01
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-02
      • 1970-01-01
      相关资源
      最近更新 更多