用 C# 创建此 XML
我之前曾使用 XmlDocument
生成基本的 Xml,但是我很难通过代码重新创建以下 XML。主要问题似乎是将名称空间添加到描述部分。
如何创建以下示例 XML 文件?
<?xml version="1.0"?>
<pndsdc:description
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:pndsterms="http://purl.org/mla/pnds/terms/"
xmlns:pndsdc="http://purl.org/mla/pnds/pndsdc/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://purl.org/mla/pnds/pndsdc/
http://www.ukoln.ac.uk/metadata/pns/pndsdcxml/2005-06-13/xmls/pndsdc.xsd"
>
<dc:identifier encSchemeURI="http://purl.org/dc/terms/URI">http://example.org/my/docs/12345/</dc:identifier>
<dc:title xml:lang="en">Everything you wanted to know about identity, but were afraid to ask</dc:title>
<dc:description xml:lang="en">The article provides a summary of the 2003 White Paper on identity cards for the UK
with a critique from the perspective of several national and international civil liberties organisations.</dc:description>
<dc:subject>Identity cards</dc:subject>
<dc:subject>Civil liberties</dc:subject>
<dc:subject>Human rights</dc:subject>
<dc:type encSchemeURI="http://purl.org/dc/terms/DCMIType" valueURI="http://purl.org/dc/dcmitype/Text">Text</dc:type>
<dcterms:license valueURI="http://creativecommons.org/licenses/by-nc-nd/2.0/uk/" />
<dcterms:rightsHolder>The National Campaign Against Identity Cards</dcterms:rightsHolder>
<dcterms:spatial encSchemeURI="http://purl.org/dc/terms/TGN">World, Europe, United Kingdom</dcterms:spatial>
</pndsdc:description>
代码也可以在线找到。
I have used XmlDocument
before to generate basic Xml before, however I am struggling to recreate the following XML via code. The main problem seems to be around adding the namespaces to the description section.
How do I create the following example XML file?
<?xml version="1.0"?>
<pndsdc:description
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:pndsterms="http://purl.org/mla/pnds/terms/"
xmlns:pndsdc="http://purl.org/mla/pnds/pndsdc/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://purl.org/mla/pnds/pndsdc/
http://www.ukoln.ac.uk/metadata/pns/pndsdcxml/2005-06-13/xmls/pndsdc.xsd"
>
<dc:identifier encSchemeURI="http://purl.org/dc/terms/URI">http://example.org/my/docs/12345/</dc:identifier>
<dc:title xml:lang="en">Everything you wanted to know about identity, but were afraid to ask</dc:title>
<dc:description xml:lang="en">The article provides a summary of the 2003 White Paper on identity cards for the UK
with a critique from the perspective of several national and international civil liberties organisations.</dc:description>
<dc:subject>Identity cards</dc:subject>
<dc:subject>Civil liberties</dc:subject>
<dc:subject>Human rights</dc:subject>
<dc:type encSchemeURI="http://purl.org/dc/terms/DCMIType" valueURI="http://purl.org/dc/dcmitype/Text">Text</dc:type>
<dcterms:license valueURI="http://creativecommons.org/licenses/by-nc-nd/2.0/uk/" />
<dcterms:rightsHolder>The National Campaign Against Identity Cards</dcterms:rightsHolder>
<dcterms:spatial encSchemeURI="http://purl.org/dc/terms/TGN">World, Europe, United Kingdom</dcterms:spatial>
</pndsdc:description>
Code can also be found here online.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过使用
XMLWriter
?我认为这就是您正在寻找的部分:
Have you tried using the
XMLWriter
?I think this is the part you are looking for:
不用担心创建命名空间声明。只需确保您创建的每个元素都位于正确的命名空间中即可。
XmlDocument
将为您创建命名空间声明。所以:等等。创建一个包含由前缀键控的命名空间的
Dictionary
通常更容易,然后执行如下操作:Don't worry about creating namespace declarations. Just make sure that every element that you create is in the right namespace. The
XmlDocument
will create the namespace declarations for you. So:and so on. It's usually easier to create a
Dictionary<string, string>
containing the namespaces keyed by prefix, and then do something like:您需要使用
XmlNamespaceManager
:You need to use
XmlNamespaceManager
: