gpt4 book ai didi

c# - 在 C# 中创建此 XML

转载 作者:太空宇宙 更新时间:2023-11-03 20:34:59 27 4
gpt4 key购买 nike

我以前使用过 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>

编码也可以是found here online .

最佳答案

您是否尝试过使用 XMLWriter

我认为这是您正在寻找的部分:

XmlTextWriter maintains a namespace stack corresponding to all the namespaces defined in the current element stack. Using XmlTextWriter you can declare namespaces manually.

w.WriteStartElement("root");
w.WriteAttributeString("xmlns", "x", null, "urn:1");
w.WriteStartElement("item","urn:1");
w.WriteEndElement();
w.WriteStartElement("item","urn:1");
w.WriteEndElement();
w.WriteEndElement();

The above C# code produces the following output. XmlTextWriter promotes the namespace declaration to the root element to avoid having it duplicated on the two child elements. The child elements pick up the prefix from the namespace declaration.

<root xmlns:x="urn:1">   
<x:item/>
<x:item/>
</x:root>

关于c# - 在 C# 中创建此 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5421820/

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