用 C# 创建此 XML

发布于 2024-10-25 11:56:46 字数 1926 浏览 5 评论 0原文

我之前曾使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月朦胧 2024-11-01 11:56:46

您是否尝试过使用 XMLWriter

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

XmlTextWriter维护一个命名空间
对应所有的栈
当前定义的命名空间
元素堆栈。使用 XmlTextWriter 您
可以手动声明命名空间。

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();

上面的 C# 代码生成
以下输出。 XmlTextWriter
将名称空间声明提升为
根元素以避免拥有它
在两个子元素上重复。
子元素选取前缀
来自命名空间声明。

<根 xmlns:x="urn:1">   
>  
>  

Have you tried using the XMLWriter?

I think this is the part you are looking for:

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>
2024-11-01 11:56:46

不用担心创建命名空间声明。只需确保您创建的每个元素都位于正确的命名空间中即可。 XmlDocument 将为您创建命名空间声明。所以:

string pdnsdcUri = "http://purl.org/mla/pnds/pndsdc/";
string dcUri = "http://purl.org/dc/elements/1.1/"
...
XmlDocument d = new XmlDocument();
XmlElement description = d.CreateElement("pdnsdc", "description", pdnsdcUri);
d.AddChild(description);
XmlElement identifier = d.CreateElement("dc", "identifier", dcUri);
description.AddChild(identifier);

等等。创建一个包含由前缀键控的命名空间的 Dictionary 通常更容易,然后执行如下操作:

XmlElement foo = d.CreateElement("prefix", "name", namespaces[prefix]);

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:

string pdnsdcUri = "http://purl.org/mla/pnds/pndsdc/";
string dcUri = "http://purl.org/dc/elements/1.1/"
...
XmlDocument d = new XmlDocument();
XmlElement description = d.CreateElement("pdnsdc", "description", pdnsdcUri);
d.AddChild(description);
XmlElement identifier = d.CreateElement("dc", "identifier", dcUri);
description.AddChild(identifier);

and so on. It's usually easier to create a Dictionary<string, string> containing the namespaces keyed by prefix, and then do something like:

XmlElement foo = d.CreateElement("prefix", "name", namespaces[prefix]);
禾厶谷欠 2024-11-01 11:56:46

您需要使用XmlNamespaceManager

XmlDocument document = new XmlDocument();
XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(document.NameTable);
xmlNamespaceManager.AddNamespace("dc","http://purl.org/dc/elements/1.1/");
...

You need to use XmlNamespaceManager:

XmlDocument document = new XmlDocument();
XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(document.NameTable);
xmlNamespaceManager.AddNamespace("dc","http://purl.org/dc/elements/1.1/");
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文