带有列表和其他对象的xml序列化器

时间:2019-01-08 12:45:57

标签: c# xml

我一直在这里寻找解决方案,但是我在以前的答案中没有找到它。 我需要使用序列化程序创建以下xml

<?xml version="1.0" encoding="Windows-1252"?>
<documents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <document>
    <Keys>
      <drawer>GraphicData</drawer>
      <somedata>otherData</somedata>
    </Keys>
    <otherGenericProperties>
      <Data>GenericData 2</Data>
    </otherGenericProperties>
    <Repeat>
          <FileInfo mimeType="application/pdf" HREF="PdfFile.pdf" />
    </Repeat>
    <Repeat>
          <FileInfo mimeType="application/pdf" HREF="PdfFile2.pdf" />
      </Repeat>
 </document>
</documents>

数据由几个类组成

namespace test
{
    public class Documents
    {
        [XmlElement("Keys")] // this is used so the destination name can change in the future
        public Keys keys { get; set; }
    }


    public class document
    {
        [XmlElement("Keys")] // this is used so the destination name can change in the future
        public Keys keys { get; set; }

        [XmlElement("Repeat")]   // This does not work 
        List<Repeat> listRepeat { get; set; }
    }

    public class Keys
    {
        [XmlElement("drawer")]
        public string drawer { get; set; }
    }

    public class Repeat
    {
        [XmlElement("fileInfo")]
        public FileInfo fileInfo { get; set; }
    }

    public class FileInfo
    {
        public string FileInfo { get; set; }
        [XmlAttribute("mimeType")]
        public string mimeType { get; set; }
        [XmlAttribute("mimeType")]
        public string HREF { get; set; }
    }
}

序列化器:

     XmlSerializer serializer = new XmlSerializer(typeof(Documents));
    using (StreamWriter writer = new StreamWriter(saveBestand, false, xmlEncoding))
    {
        serializer.Serialize(writer, icm, namespaces);
    }

我确实像示例一样需要xm,并且xml名称也可以通过使用xmlElement之类的合同进行更改。重复元素不能以某种方式放置在示例中看到的水平上。有人有解决方案吗?

2 个答案:

答案 0 :(得分:0)

您有两个选择。

选项1 使用xsd.exe使用所需的xml生成类。

xsd yourxmlfile.xml
xsd yourxmlfile.xsd /classes

这将生成可用于序列化或反序列化的类。

选项2: 粘贴特殊选项以生成c#类。 这不涉及使用命令行。

通过以下网址推荐博客:https://dennymichael.net/2014/05/30/convert-xml-to-csharp-classes/comment-page-1/

enter image description here

答案 1 :(得分:0)

似乎您对数据类的设计不佳。请尝试以下解决方案。

public class OtherGenericProperties
{
    [XmlElement("Data")]
    public string Data { get; set; }
}
[XmlRoot("documents")]
public class Documents
{
    [XmlElement("document")]
    public Document Document { get; set; }
}
public class Document 
{
    [XmlElement("Keys")] // this is used so the destination name can change in the future
    public Keys Keys { get; set; }

    [XmlElement("otherGenericProperties")]
    public OtherGenericProperties OtherGenericProperties { get; set; }

    [XmlElement("Repeat")]   // This does not work 
    public List<Repeat> ListRepeat { get; set; }
}
public class Keys
{
    [XmlElement("drawer")]
    public string Drawer { get; set; }

    [XmlElement("somedata")]
    public string SomeData { get; set; }
}
public class Repeat
{
    [XmlElement("FileInfo")]
    public FileInfo FileInfo { get; set; }
}
public class FileInfo
{
    [XmlAttribute("mimeType")]
    public string MimeType { get; set; }

    [XmlAttribute("HREF")]
    public string Href { get; set; }
}

序列化:

   internal static void Test()
    {
        var doc = new Documents
        {
            Document = new Document
            {
                Keys = new Keys
                {
                    Drawer = "GraphicData",
                    SomeData = "otherData"
                },
                OtherGenericProperties = new OtherGenericProperties { Data = "GenericData 2" },
                ListRepeat = new List<Repeat>
                {
                    new Repeat { FileInfo =new FileInfo { Href = "PdfFile.pdf", MimeType = "application/pdf" } },
                    new Repeat { FileInfo = new FileInfo { Href = "PdfFile2.pdf", MimeType = "application/pdf" } }
                }
            }
        };

        XmlSerializer serializer = new XmlSerializer(typeof(Documents));
        using (var f = new StreamWriter("D:\\doc.xml", false, Encoding.GetEncoding("Windows-1252")))
        {
            serializer.Serialize(f, doc);
            f.Flush();
        }
    }

输出:

<?xml version="1.0" encoding="Windows-1252"?>
<documents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <document>
    <Keys>
      <drawer>GraphicData</drawer>
      <somedata>otherData</somedata>
    </Keys>
    <otherGenericProperties>
      <Data>GenericData 2</Data>
    </otherGenericProperties>
    <Repeat>
      <FileInfo mimeType="application/pdf" HREF="PdfFile.pdf" />
    </Repeat>
    <Repeat>
      <FileInfo mimeType="application/pdf" HREF="PdfFile2.pdf" />
    </Repeat>
  </document>
</documents>