I'm using Linq for XML in .NET 3.5 for the first time, and I'm having some trouble with namespaces. Namely, the XElement is printed like this : <opf:metadata>
when I just want it to say <metadata>
.
Here is the code:
XNamespace opfNamespace = "http://www.idpf.org/2007/opf";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace dcterms = "http://purl.org/dc/terms/";
XNamespace dc = "http://purl.org/dc/elements/1.1/";
opfRoot = new XElement(opfNamespace + "package",
new XAttribute("version", "2.0"),
new XAttribute("unique-identifier", "uuid_id"));
XElement metadata = new XElement(opfNamespace + "metadata",
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XAttribute(XNamespace.Xmlns + "opf", opfNamespace),
new XAttribute(XNamespace.Xmlns + "dcterms", dcterms),
new XAttribute(XNamespace.Xmlns + "dc", dc),
new XElement(dc + "language", "pt-BR"));
opfRoot.Add(metadata);
And here is the result:
<?xml version="1.0" encoding="utf-8"?>
<package version="2.0" unique-identifier="uuid_id" xmlns="http://www.idpf.org/2007/opf">
<opf:metadata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:opf="http://www.idpf.org/2007/opf" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:language>pt-BR</dc:language>
</opf:metadata>
</package>
I expected it the metadata element to come without the opf: "opf:metadata"
If I remove the opfNameSpace from {opfNameSpace + "metadata"}, I get a blank xmlns namespace (xmlns="") in the metadata element.
I looked at this thread:
How can I write xml with a namespace and prefix with XElement?
and this one:
XElement default namespace on attributes provides unexpected behaviour
But they didn't solve my problem. Any ideas?