XmlSerializer和复杂前缀
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XmlSerializer和复杂前缀相关的知识,希望对你有一定的参考价值。
我正在尝试研究如何生成一些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; }
}
现在正确序列化和反序列化。
以上是关于XmlSerializer和复杂前缀的主要内容,如果未能解决你的问题,请参考以下文章
leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和](代码片段