1
我遇到了使用.net XMLSerializer对象和XML命名空间的问题。我对XMLSerializer的使用经验非常有限,而且我之前没有使用XML。我试图像下面这样:.net XMLSerializer多个命名空间问题
<Record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Property1>blah</Property1>
<oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/">>
<dc:Property2>asdsd</dc:Property2>
<dc:Property3>asdasasdda</dc:Property3>
</oai_dc:dc>
</Record>
不过,我可以在瞬间获得最佳的是(注意命名空间声明的位置)
<Record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/">
<Property1>blah</Property1>
<oai_dc:dc>
<dc:Property2>asdsd</dc:Property2>
<dc:Property3>asdasasdda</dc:Property3>
</oai_dc:dc>
</Record>
的命名空间中被宣布根元素,而不是我喜欢的dc元素。我明白,要么在根中声明命名空间,要么在使用命名空间的元素中应该是可以接受的。
我附加了XMLSerializerNamespaces如下:
Dim XMLNamespaces As New XmlSerializerNamespaces()
XMLNamespaces.Add("oai_dc", "http://www.openarchives.org/OAI/2.0/oai_dc/")
XMLNamespaces.Add("dc", "http://purl.org/dc/elements/1.1/")
XMLNamespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance")
XMLSerializer.Serialize(XMLWriter, inputObject, XMLNamespaces)
和我使用的对象如下:
<XmlRoot("Record")> _
Public Class Record
<XmlElement("Property1")> _
Public Property1 As String
<XmlElement("dc", Namespace:="http://www.openarchives.org/OAI/2.0/oai_dc/")> _
Public DCMetadata As DublinCore
Public Sub New()
DCMetadata = New DublinCore()
End Sub
End Class
<XmlRoot("dc", Namespace:="http://www.openarchives.org/OAI/2.0/oai_dc/")> _
Public Class DublinCore
<XmlElement("Property2", Namespace:="http://purl.org/dc/elements/1.1/")> _
Public Property2 As String
<XmlElement("Property3", Namespace:="http://purl.org/dc/elements/1.1/")> _
Public Property3 As String
Public Sub New()
End Sub
End Class
有没有什么办法来迫使XMLSerializer的输出命名空间对树下的元素进行声明?
感谢您的任何输入,
安迪