我正在尝试找出如何使用XmlSerializer从一组类中琐碎地使用一些前缀的Xml。我还希望能够读取相同的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要处理的复杂得多,那么我可以手动编写文件,但我宁愿通过标记而不是代码来完成。
长篇致歉。
非常感谢。
伊恩
答案 0 :(得分:1)
看起来 就像这里的混乱只是前两个元素上的名称空间;在本地,我有:
[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; }
}
现在可以正确地序列化和反序列化了。