Reputation: 1940
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.
Upvotes: 1
Views: 392
Reputation: 96722
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]);
Upvotes: 1
Reputation: 14863
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>
Upvotes: 3
Reputation: 81660
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/");
...
Upvotes: 1