【问题标题】:XML array with custom attribute and element names具有自定义属性和元素名称的 XML 数组
【发布时间】:2018-04-20 00:13:38
【问题描述】:

我想在 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());
            }

        string desiredOutput =
                @"<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>";            
    }


[XmlRoot("library")]    
public class Library
        {
        public MyBooks MyBooks { get; set; }

        [XmlElement("my.books")] 
        public List<Book> Books { get; set; }

        public Library()
    {

    }

    public Library(MyBooks myBooks = null)
    {
        MyBooks = myBooks;
    }
}


[XmlType("my.books")]
public class MyBooks
{
     [XmlAttribute("genre")]
        public string Genre { get; set; }

     [XmlElement("book")]
        public List<Book> Booklist { get; set; }

      public MyBooks(string genre, List<Book> booklist = null)
        {
            Genre = genre;
            Booklist = booklist;
        }

          public MyBooks()
        {

        }   
}   

public class Book
    {
        [XmlAttribute("title")]
        public string Title { get; set; }
        [XmlAttribute("author")]
        public string Author { get; set; }

        public Book() { }

        public Book(string title, string author)
        {
        Title = title;
        Author = author;    
        }
    }
}

输出是:

<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”属性,但在这里不起作用。

有没有办法在这个元素上应用自定义名称属性?

【问题讨论】:

  • 我别无选择,由于某种原因,我使用的 xml 配置具有这样的元素名称。是遗留产品配置,不能修改,但需要序列化...

标签: c# xml


【解决方案1】:

您的属性似乎位于错误的属性上。

试试这个:

[System.Xml.Serialization.XmlElement("my.books")]
public MyBooks MyBooks { get; set; }

public List<Book> Books { get; set; }

我现在得到这个输出:

<?xml version="1.0" encoding="utf-16"?>
<library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <my.books genre="classic">
    <book title="1984" author="George Orwell" />
    <book title="Robinson Crusoe" author="Daniel Defoe" />
    <book title="Oliver Twist" author="Mary Shelly" />
  </my.books>
</library>

写得很好!

【讨论】:

  • 哇,感谢你的眼睛!我花了一整天,找不到这是我的错误。你又救了我一天:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-24
  • 1970-01-01
  • 1970-01-01
  • 2019-04-23
相关资源
最近更新 更多