亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

具有自定義屬性和元素名稱的XML數組

具有自定義屬性和元素名稱的XML數組

C#
元芳怎么了 2021-03-30 14:12:54
我想在C#中序列化/反序列化xml文檔,例如:<library>    <my.books genre =""classic"">         <book title = ""1984"" author=""George Orwell"" />         <book title = ""Robinson Crusoe"" author=""Daniel Defoe"" />         <book title = ""Frankenstein"" author=""Mary Shelly"" />    </my.books></library>";有兩件重要的事情:元素“ my.books”必須具有自定義名稱(而不是屬性名稱)my.books元素必須具有屬性(“體裁”)。這是我的代碼(示例在https://dotnetfiddle.net/bH5WVX上):   using System;   using System.Xml;   using System.Xml.Linq;   using System.Collections.Generic;   using System.ComponentModel;   using System.Xml.Serialization;   using System.IO;   public class Program   {    public static void Main()    {        Library lib = new Library(myBooks: new MyBooks(            genre: "classic",            booklist: new List<Book>{                new Book("1984", "George Orwell"),                new Book("Robinson Crusoe", "Daniel Defoe"),                new Book("Oliver Twist", "Mary Shelly"),            }));          XmlSerializer formatter = new XmlSerializer(typeof(Library));            using (StringWriter sw = new StringWriter())            {                formatter.Serialize(sw, lib);                Console.Write(sw.ToString());            }輸出為:<library>  <MyBooks genre="classic">    <book title="1984" author="George Orwell" />    <book title="Robinson Crusoe" author="Daniel Defoe" />    <book title="Oliver Twist" author="Mary Shelly" />  </MyBooks></library>唯一的問題是我不能強制元素“ MyBooks”使用名稱“ my.books”我僅找到有關此主題的一篇相關文章-http : //www.codemeit.com/xml/c-xmlserializer-add-an-attribute-to-an-array-element.html,它建議使用“ XmlType”屬性在課堂上,但在這里不起作用。有什么方法可以在此元素上應用自定義名稱屬性嗎?
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 171 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號