c# - 在 C# 中创建此 XML

标签 c# .net xml xmldocument

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

相关文章:

c# - 实例化多个 GameObject 预制件并删除原始文件后,Unity 中缺少引用异常

c# - "The given key was not present in the dictionary"nHibernate C#

c# - 如何使用使用 sha1ecdsa 的公钥根据签名验证数据?

c# - 查询为空时的最大返回值

c# - XMLSerializer 更改元素的名称

c# - 将 Action<.., .., T > 作为没有预定义数量的 Action 参数的参数传递

c# - 如何验证 TCP 数据包是否已在 C# 中收到 ACK?

c# - 尝试使用 C# 运行命令

python - 显示共享库的所有 native 路径

XML 到 YAML 转换