I have spent the last few days reading here on Stack Overflow, blogs, and MSDN articles. I am clearly lacking some basic understanding of how namespaces, Linq, and XML work and am in need of help. If I had more hair to pull out, it would be in my hand right now :-)
Using C# and Linq to XML, I open the following opf.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<package version="2.0" xmlns="http://www.idpf.org/2007/opf" unique-identifier="">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
</metadata>
<manifest></manifest>
<spine toc="ncx"></spine>
<guide></guide>
</package>
I open this file with the following code:
File.Copy(_opfFile, opfFile, true);
XDocument opfDoc = XDocument.Load(opfFile);
Here is where I get completely lost, unfortunately. What I need to do is generate elements under each of the major nodes. For the metadata
node, I need to create elements with namespaces and non-namespaces. For the remainder of the file, I just need to add regular nodes that do not make use of namespaces.
Below is a sampling of the output I want to achieve. I am sure if you can help me with the metdata
and manifest
nodes, I can figure out how to update the rest of the nodes.
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns:ibooks="http://www.idpf.org/2007/opf" unique-identifier="BookId" version="3.0" prefix="ibooks: http://www.idpf.org/2007/opf" xmlns="http://www.idpf.org/2007/opf">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
<dc:title>Untitled</dc:title>
<meta refines="#contributor" property="role" scheme="marc:relators">bkp</meta>
</metadata>
<manifest>
<item id="toc" href="toc.xhtml" media-type="application/xhtml+xml" properties="nav"/>
</manifest>
<spine toc="ncx"></spine>
<guide></guide>
</package>
Could you please provide some basic C#/Linq code to:
- Find and add
metadata
items requiring the namesapce (ns) of "dc" as in the example above.
- Add a
metadata
item that does not use a namespace as in the example above.
- Add a new
manifest
item as in the example above.
I would share the code I have written but it is all over the place and I have lots of commented code. What I can tell you is I constantly end up null exception errors.
Thank you in advance.
When reverse-engineering XML that uses multiple namespaces, I find the following debugging utilities to be useful:
public static class XObjectExtensions
{
public static IEnumerable<string> DumpXmlAttributeNames(this XObject obj)
{
if (obj is XAttribute)
return new[] { ((XAttribute)obj).Name.ToString() };
else if (obj is XElement)
{
return ((XElement)obj).Attributes().Select(a => a.Name.ToString());
}
return Enumerable.Empty<string>();
}
public static IEnumerable<string> DumpXmlElementNames(this XElement root)
{
if (root == null)
return Enumerable.Empty<string>();
return root.DescendantsAndSelf().Select(el => string.Format("{0} \"{1}\"; Attribute names: {2}",
new string(' ', el.AncestorsAndSelf().Count()), el.Name.ToString(), String.Join(", ", el.DumpXmlAttributeNames().Select(s => "\"" + s + "\""))));
}
public static IEnumerable<string> DumpXmlElementNames(this XDocument root)
{
if (root == null)
return Enumerable.Empty<string>();
return root.Root.DumpXmlElementNames();
}
}
Using them, one can see easily see the correct namespaces for the elements and attributes for your desired XML:
"{http://www.idpf.org/2007/opf}package"; Attribute names: "{http://www.w3.org/2000/xmlns/}ibooks", "unique-identifier", "version", "prefix", "xmlns"
"{http://www.idpf.org/2007/opf}metadata"; Attribute names: "{http://www.w3.org/2000/xmlns/}dc", "{http://www.w3.org/2000/xmlns/}opf"
"{http://purl.org/dc/elements/1.1/}title"; Attribute names:
"{http://www.idpf.org/2007/opf}meta"; Attribute names: "refines", "property", "scheme"
"{http://www.idpf.org/2007/opf}manifest"; Attribute names:
"{http://www.idpf.org/2007/opf}item"; Attribute names: "id", "href", "media-type", "properties"
"{http://www.idpf.org/2007/opf}spine"; Attribute names: "toc"
"{http://www.idpf.org/2007/opf}guide"; Attribute names:
Thus you need to add the "title"
element in the "http://purl.org/dc/elements/1.1/"
namespace and the others in the "http://www.idpf.org/2007/opf"
namespace:
var dc = (XNamespace)"http://purl.org/dc/elements/1.1/";
var opf = (XNamespace)"http://www.idpf.org/2007/opf";
var meta = opfDoc.Root.Element(opf + "metadata");
meta.Add(new XElement(dc + "title", "Untitled"));
meta.Add(new XElement(opf + "meta",
new XAttribute("refines", "#contributor"),
new XAttribute("property", "role"),
new XAttribute("scheme", "marc:relators"), "bkp"));
var manifest = opfDoc.Root.Element(opf + "manifest");
manifest.Add(new XElement(opf + "item",
new XAttribute("id", "toc"),
new XAttribute("href", "toc.xhtml"),
new XAttribute("media-type", "application/xhtml+xml"),
new XAttribute("properties", "nav")));