gpt4 book ai didi

c# - XmlSerializer 和复杂的前缀

转载 作者:行者123 更新时间:2023-12-04 17:01:47 27 4
gpt4 key购买 nike

我正在尝试解决如何生成一些 Xml,它使用 XmlSerializer 的一组类中的前缀不仅仅是微不足道的使用。我还希望能够读取相同的 Xml(这相当简单,但随着我使用更复杂的属性似乎消失了。

具体用例是来自电子书的 opf 文件。

这是我要生成的 Xml 的示例

<?xml version='1.0' encoding='utf-8'?>
<package xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="uuid_id">
<metadata xmlns:xsi=""dchttp://www.w3.org/2001/XMLSchema-instance" xmlns:opf="http://www.idpf.org/2007/opf" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:calibre="http://calibre.kovidgoyal.net/2009/metadata" xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:publisher>Baen Publishing Enterprises</dc:publisher>
<meta name = "calibre:series_index" content="1"/>
<dc:rights>2002 by Lois McMaster Bujold.</dc:rights>
<dc:language>en-us</dc:language>
<dc:creator opf:file-as="Bujold., Lois McMaster " opf:role= "aut" > Lois McMaster Bujold.</dc:creator>
<meta name = "calibre:timestamp" content= "2010-05-07T17:42:06.859000+00:00" />
<dc:title>Miles Errant</dc:title>
<meta name = "cover" content= "RW_0743435583_Cover" />
<dc:date>2002-09-01T04:00:00+00:00</dc:date>
<dc:contributor opf:role= "bkp" > calibre(0.6.51)[http://calibre.kovidgoyal.net]</dc:contributor>
<dc:identifier opf:scheme = "ISBN" > 0 - 7434 - 3558 - 3 </dc:identifier >
<dc:identifier id = "uuid_id" opf:scheme = "uuid" > c7e276b6 - 1a12 - 4130 - a7ac - b2febb9730fb </dc:identifier >
<dc:subject > Science Fiction </dc:subject >
</metadata >
</package>

相应的类如下所示:-
public class package
{
public metadata metadata { get; set; }
}

public class metadata
{
[XmlElement(Namespace = "http://purl.org/dc/elements/1.1/", Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
public string title { get; set; }
[XmlElement(Namespace = "http://purl.org/dc/elements/1.1/", Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
public string language { get; set; }
[XmlElement(Namespace = "http://purl.org/dc/elements/1.1/", Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
public string creator { get; set; }
[XmlElement(Namespace = "http://purl.org/dc/elements/1.1/", Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
public string publisher { get; set; }
[XmlElement(Namespace = "http://purl.org/dc/elements/1.1/", Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
public string rights { get; set; }
[XmlElement(Namespace = "http://purl.org/dc/elements/1.1/", Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
public List<identifier> identifier { get; set; }
}

public class identifier
{
[XmlAttribute(Namespace = "http://purl.org/dc/elements/1.1/")]
public string id { get; set; }
[XmlAttribute(Namespace = "http://www.idpf.org/2007/opf")]
public string scheme { get; set; }
[XmlText]
public string Identifier { get; set; }
}

和编写代码(当前)是
    public static Stream WriteToStream(package p)
{
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add("dc", "http://purl.org/dc/elements/1.1/");
namespaces.Add("", "http://www.idpf.org/2007/opf");
XmlSerializer serialiser = new XmlSerializer(typeof(package));


Stream s = new MemoryStream();
serialiser.Serialize(s, p, namespaces);
s.Position = 0;
return s;
}

这会产生以下结果。
<?xml version="1.0"?>
<package>
<metadata>
<title xmlns="http://purl.org/dc/elements/1.1/">Miles Errant</title>
<language xmlns="http://purl.org/dc/elements/1.1/">en-us</language>
<creator xmlns="http://purl.org/dc/elements/1.1/"> Lois McMaster Bujold.</creator>
<publisher xmlns="http://purl.org/dc/elements/1.1/">Baen Publishing Enterprises</publisher>
<rights xmlns="http://purl.org/dc/elements/1.1/">2002 by Lois McMaster Bujold.</rights>
<identifier d3p1:scheme="ISBN" xmlns:d3p1="http://www.idpf.org/2007/opf" xmlns="http://purl.org/dc/elements/1.1/"> 0 - 7434 - 3558 - 3 </identifier>
<identifier id="uuid_id" d3p1:scheme="uuid" xmlns:d3p1="http://www.idpf.org/2007/opf" xmlns="http://purl.org/dc/elements/1.1/"> c7e276b6 - 1a12 - 4130 - a7ac - b2febb9730fb </identifier>
</metadata>
<manifest />
</package>

如果我将 dc 命名空间添加到根元素,那么我会得到 dc 前缀,但是我可以更长时间地反序列化样本。

如果这比 XmlSerializer 要处理的更复杂,那么我可以手动编写文件,但我宁愿通过标记而不是代码来完成。

为长篇道歉。

非常感谢。

伊恩

最佳答案

看起来这里的困惑只是前两个元素的命名空间;在本地,我有:

[XmlRoot("package", Namespace = "http://www.idpf.org/2007/opf")]
public class Package
{
[XmlElement("metadata", Namespace = "http://www.idpf.org/2007/opf")]
public PackageMetadata Metadata { get; set; }
}

现在可以正确序列化和反序列化。

关于c# - XmlSerializer 和复杂的前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52179006/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com