[Mono-list] Questions about writing an XML document

Loren Bandiera lorenb at mmgsecurity.com
Tue May 9 07:30:53 EDT 2006


Hi,

I want to write an XML document but there are some parts I'm not sure
how to go about doing. Specifically namespaces and schemas. Take the
following example XML:

<?xml version="1.0" encoding="UTF-8"?>
<doc xmlns="http://whatever.org/XMLSchema/foo"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://whatever.org/XMLSchema/foo.xsd">
     <foo>bar</foo>
</doc>

To create it, I've tried doing something like this:

XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.AppendChild (xmlDoc.CreateNode
(XmlNodeType.XmlDeclaration,"",""));

XmlElement docElement = xmlDoc.CreateElement ("doc");
docElement.SetAttribute ("xmlns", "http://whatever.org/XMLSchema/foo");
docElement.SetAttribute ("xmlns:xsi",
"http://www.w3.org/2001/XMLSchema-instance");
docElement.SetAttribute ("xsi:schemaLocation",
"http://whatever.org/XMLSchema/foo.xsd");
xmlDoc.AppendChild (docElement);

XmlElement fooElement = xmlDoc.CreateElement ("foo");
docElement.AppendChild (fooElement);

This just fails with an ArguementException saying "Cannot use prefix
with an empty namespace". That kind of tells me I'm not going in the
right direction but I haven't had luck finding any proper examples.

I've seen the XmlNamespaceManager class and tried to use it. I can add
namespaces but when I write out the document, they are not listed at
all.

If anyone has any suggestions that would be great.

-- 
Loren Bandiera <lorenb at mmgsecurity.com>
MMG Security, Inc.



More information about the Mono-list mailing list